• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Ranch Hand
Posts: 71
1
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 16077
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
Ranch Hand
Posts: 157
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Map<Character, List<String>> map = list.stream().collect(Collectors.groupingBy(s -> s.charAt(0)));
 
Pierre-Yves Saumont
Author
Ranch Hand
Posts: 157
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops, once again, I should have refreshed the page before posting my answer! Sorry for spoiling :-(
 
Master Rancher
Posts: 2066
75
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 71
1
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • 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: 21181
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
Master Rancher
Posts: 2066
75
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 21181
87
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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?
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!