Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I am using Spark.
In spark after doing a collect on RDDs of
scala.Tuple2<K,V>
I get List of
scala.Tuple2<K,V>
. I want to convert it to a
HashMap<K,V>
in Java.
I can iterate over the list and add it to my HashMap but I am looking for an elegant way to do this. Thanks!
scala> import scala.collection.JavaConverters._
scala> val tuples = List((1, 2), (2, 3), (4, 5))
scala> tuples.toMap.asJava
res1: java.util.Map[Int,Int] = {1=2, 2=3, 4=5}
I think the most elegant way in java is to use stream
and Collectors
.
You can achieve this way:
List<Tuple2<String, String>> list = new ArrayList<>();
list.add(new Tuple2<>("first", "second"));
list.add(new Tuple2<>("third", "four"));
list.add(new Tuple2<>("five", "six"));
list.add(new Tuple2<>("seven", "eight"));
list.add(new Tuple2<>("nine", "ten"));
System.out.println("List of Tuple2s:" + list);
//convert list of tupples to Map with one line
Map<String, String> resultMap = list.stream()
.collect(Collectors.toMap(Tuple2::_1, Tuple2::_2));
System.out.println("Map of Tuples2s: "+resultMap);
Output:
List of Tuple2s:[(first,second), (third,four), (five,six), (seven,eight), (nine,ten)]
Map of Tuples2s: {nine=ten, third=four, seven=eight, five=six, first=second}
But what about duplicate keys?? When we add another item to list like: list.add(new Tuple2<>("first", "ten"));
exception occures:
Exception in thread "main" java.lang.IllegalStateException: Duplicate
key second at
java.util.stream.Collectors.lambda$throwingMerger$0(Collectors.java:133)
at java.util.HashMap.merge(HashMap.java:1253)
If you are not sure whether you can have duplicates you can do:
Map<String, String> resultMap = list.stream()
.collect(Collectors.toMap(Tuple2::_1, Tuple2::_2,
(x, y) -> {
System.out.println("duplicate key!");
return x;
and avoid overwriting item in Map
.
Output:
List of Tuple2s:[(first,second), (third,four), (five,six), (seven,eight), (nine,ten), (first,ten)]
duplicate key!
Map of Tuples2s: {nine=ten, third=four, seven=eight, five=six, first=second}
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.