• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Set HashMap Objects using Streams

 
Ranch Hand
Posts: 157
Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have the below code as per the traditional way to iterate and set the object value in hashmap as follows.


Same i want to use with Streams/Lamdas. Below is what I tried which is incomplete. Can someone help in that.

But its adding same object multiple times. Where i as doing wrong. I don't want to initialize the obj inside stream.
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi. I formatted the code a bit and moved the discussion to the Java 8 forum.

I'm not sure you need a spliterator, which is used to with multiple threads, but I'll leave that to the more knowledgeable.
 
Saloon Keeper
Posts: 10781
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm a total noob when it comes to Java8 but I thought I'd take a stab at it. Please feel free to shoot holes in this.






 
Saloon Keeper
Posts: 15616
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Carey, you can use a method reference instead:
 
Carey Brown
Saloon Keeper
Posts: 10781
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
 
Marshal
Posts: 79406
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What about groupingBy?
 
Stephan van Hulst
Saloon Keeper
Posts: 15616
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure if groupingBy() is convenient in this case, because it will result in a mapping to lists, not a list of maps.
 
Campbell Ritchie
Marshal
Posts: 79406
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agree. I obviously didn't read it proberly. If you want a List<Map<Something>> groupingBy won't work.
reply
    Bookmark Topic Watch Topic
  • New Topic