Combine keys and values of a HashMap to a Set
问题 I have a HashMap<Integer, Integer> , There can be duplicate values for the unique keys. Is there a way to convert the HashMap to a Set<Integer> which contains unique Integers of both the keys and values. This can definitely be done in two loops by iterating over the keySet() and .values(). I'm would like to know whether this is possible in java 8 streams. 回答1: You can use the stream function to combine both the values and the keys: Map<Integer, Integer> map = ... Set<Integer> total = Stream