• 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
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • 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: 10167
81
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • 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: 14857
334
  • 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: 10167
81
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Marshal
Posts: 77936
373
  • 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: 14857
334
  • 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: 77936
373
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic