• 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

Create Map of Character and List of String using Set of String

 
Ranch Hand
Posts: 138
1
jQuery Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am looking to create a map of character as a key and value as a list of string using Java8 stream mechanism.
The input is like, set of strings: ["hi","hills","and","at","not","bad","that","the"]
The output like, map of character and list of string: ['h':["hi","hills"], 'a':["and","at"], 'n':["not"], 'b':["bad"], 't':["that","the"]]

I am able to do it in using earlier java version but stuck with creattion on this on Java8 using stream.

Thanks,
Atul
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post what you have tried to solve the problem?

Hint: One method that will be useful to solve this is Collectors.groupingBy(...)
 
Author
Posts: 161
31
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Map<Character, List<String>> map = list.stream().collect(Collectors.groupingBy(s -> s.charAt(0)));
 
Pierre-Yves Saumont
Author
Posts: 161
31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops, once again, I should have refreshed the page before posting my answer! Sorry for spoiling :-(
 
Bartender
Posts: 5465
212
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, OP can practise the use of two other java 8 thingies:

create a map using Collectors.toMap and using the Map method 'computeIfAbsent' (see the Api)
 
Atul More
Ranch Hand
Posts: 138
1
jQuery Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Thanks Piet, Pierre and Jasper for your response.
I am able to solve it with the help of your inputs.
Here is the sample code:


The WordData class:


Hope this  will help someone.

Thanks,
Atul
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Two things:

1) You don't need to use the map(s -> s.toString()). parseDataSet is a Set<String>, so s is already a String. Note that you could also have used map(Object::toString).

2) Never use new Character(...) or any of the other primitive wrapper class constructors to wrap a primitive. If possible, don't wrap manually but use auto-boxing instead: s -> s.charAt(0). If you really need explicit boxing, use the valueOf method: s -> Character.valueOf(s.charAt(0)). The reason is that valueOf will use a cached instance for specific ranges; at least -128 to 127 (inclusive) for all numeric types, and at least \u0000 to \u007F for characters. FYI, that's the entire ASCII character set (see http://www.asciitable.com/).
 
Piet Souris
Bartender
Posts: 5465
212
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, be very carefull to incorporate fields into the 'equals' method that can be changed at will. As it is now you cannot guatantee that WordDate x equals WordData y need not have the same outcome at every given moment, against the contract of 'equals'.
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can also shorten your equals and hashCode methods by using methods from java.util.Objects:

But be honest - you let your IDE generate those methods, right?
 
Paper jam tastes about as you would expect. Try some on this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic