Simpler Java-8 solution involving Collectors.toMap
:
Map<Integer, String> mapFromSet = set.stream()
.collect(Collectors.toMap(Entry::getKey, Entry::getValue));
An IllegalStateException
will be thrown if duplicate key is encountered.
More Related Contents:
- Removing duplicates from arraylist using set
- How do I efficiently iterate over each entry in a Java Map?
- How to sort Map values by key in Java?
- How can I initialise a static Map?
- How to directly initialize a HashMap (in a literal way)?
- What’s the difference between ConcurrentHashMap and Collections.synchronizedMap(Map)?
- What is the difference between the HashMap and Map objects in Java?
- Java time-based map/cache with expiring keys [closed]
- What is the difference between Set and List?
- How to convert an Array to a Set in Java
- Picking a random element from a set
- How to calculate the intersection of two sets? [duplicate]
- Case insensitive string as HashMap key
- Different types of thread-safe Sets in Java
- Efficiently compute Intersection of two Sets in Java?
- Get unique values from ArrayList in Java
- Why doesn’t java.util.Set have get(int index)?
- How do I use the new computeIfAbsent function?
- Intersection of java.util.Map
- Java Equivalent to Python Dictionaries
- Remove multiple keys from Map in efficient way?
- Cartesian product of streams in Java 8 as stream (using streams only)
- Why does my TreeSet not add anything beyond the first element?
- How to check whether given string is a word
- How to Iterate over a Set/HashSet without an Iterator?
- In Java 8 how do I transform a Map to another Map using a lambda?
- Sorting the Map in descending order based on the value [duplicate]
- Most elegant way to join a Map to a String in Java 8
- Consumer mapped Class in HashMap
- How to Maintain order of insertion [duplicate]