• Post Reply Bookmark Topic Watch Topic
  • New Topic

Set HashMap Objects using Streams

 
vivek dhiman
Ranch Hand
Posts: 153
Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • 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.
 
Knute Snortum
Sheriff
Posts: 3272
84
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
Carey Brown
Bartender
Posts: 2258
28
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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.






 
Stephan van Hulst
Bartender
Posts: 6583
84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey, you can use a method reference instead:
 
Carey Brown
Bartender
Posts: 2258
28
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. Forgot about that.
 
Campbell Ritchie
Marshal
Posts: 52581
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about groupingBy?
 
Stephan van Hulst
Bartender
Posts: 6583
84
  • Mark post as helpful
  • send pies
  • 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: 52581
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree. I obviously didn't read it proberly. If you want a List<Map<Something>> groupingBy won't work.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!