• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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: 7113
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: 8943
76
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: 13500
305
  • 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: 8943
76
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Marshal
Posts: 74714
336
  • 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: 13500
305
  • 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: 74714
336
  • 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.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic