Code:
Map<Integer, HashSet<String>> test = new TreeMap<>();
test.put(1, new HashSet<>());
test.put(2, new HashSet<>());
test.put(3, new HashSet<>());
test.put(4, new HashSet<>());
test.get(1).add("1");
test.get(2).add("2");
test.get(3).add("2");
test.get(4).add("3, 33");
//get value of treemap and get rid of the duplicate by using distinct and printout
//at the end
test.values().stream().distinct().forEach(i -> System.out.println(i));
output:
[1]
[2]
[3, 33]
My question is how I can printout the key and value at the same time without having duplicate value?
Expected Result:
1= [1]
2= [2]
3= [3, 33]
I even try below code, yet it gives me the treemap with the duplicate values:
Code:
List<Map.Entry<Integer, HashSet<String>>> list = new ArrayList<>();
list.addAll(test.entrySet());
list.stream().distinct().forEach( i -> System.out.println(i));
Output:
1=[1]
2=[2]
3=[2]
4=[3, 33]
3= [3, 33]
? What happened to4
? Which key should remain for2
? – Gilmour[3, 33]
should be mapped to4
. You'll have to define how the mapping should behave. – Gilmour