Apply Distinctive Function on TreeMap

the 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)); 

exit:

 [1] [2] [3, 33] 

My question is, how can I print the key and value at the same time without having a duplicate value?

Expected Result:

  1= [1] 2= [2] 3= [3, 33] 

I even try to execute the code below, but it gives me a treemap with duplicate values:

the code:

  List<Map.Entry<Integer, HashSet<String>>> list = new ArrayList<>(); list.addAll(test.entrySet()); list.stream().distinct().forEach( i -> System.out.println(i)); 

Conclusion:

 1=[1] 2=[2] 3=[2] 4=[3, 33] 
+1
source share
3 answers
 test.entrySet().stream() .collect( Collectors.toMap( Map.Entry::getValue, x -> x, (a, b) -> a ) ).values() .forEach(System.out::println); 

Edit:

Explanation: this snippet will take a stream of records and put them in the value map for input when discarding duplicates (see javadoc for Collectors # toMap ). Then it takes the values โ€‹โ€‹of this map as a collection. The result is a collection of record elements that are different from Map.Entry::getValue .

Edit 2:

From your comments, I think I understand what you are trying to do. You use this TreeSet as the 1st list and you want the keys to be destroyed when duplicate values โ€‹โ€‹are deleted. It's right? Perhaps you can explain why you are doing this, and not just use a list.

Streams are not suitable for this approach, so it will not be very good, but here you go: Stream values, exclude duplicates, collect in a list, and then flip the list back to the map.

 test.values().stream() .distinct() .collect( Collectors.collectingAndThen( Collectors.toList(), lst -> IntStream.range(0, lst.size()).boxed().collect( Collectors.toMap(i -> i + 1, i -> lst.get(i)) ) ) ).entrySet().forEach(System.out::println); output: 1=[1] 2=[2] 3=[3, 33] 
0
source

Your question is a bit confused as you say you want to use the key for different values, but duplicate values โ€‹โ€‹obviously have duplicate keys. It is not clear why you expect key 2 for value 2 in your example, since value 2 present twice in the original map with keys 2 and 3 .

The following code will collect all the keys for duplicates:

 test.entrySet().stream().collect(Collectors.groupingBy( Map.Entry::getValue, Collectors.mapping(Map.Entry::getKey, Collectors.toList()))) .forEach((value,keys) -> System.out.println(keys+"\t= "+value)); 

He will print:

  [1] = [1] [2, 3] = [2] [4] = [3, 33] 

for your sample map. You need to select key 2 from the list of keys [2, 3] , if you have a rule for selection.

+2
source
  Map<Integer, HashSet<String>> test = new TreeMap<>(); test.put(1, new HashSet<String>()); test.put(2, new HashSet<String>()); test.put(3, new HashSet<String>()); test.put(4, new HashSet<String>()); test.get(1).add("1"); test.get(2).add("2"); test.get(3).add("2"); test.get(4).add("3, 33"); int count = 0; HashSet<String> distinctValues = new HashSet<>(); test.entrySet().stream().forEach(entry -> { HashSet<String> entryValues = new HashSet<>(); entryValues.addAll(entry.getValue()); // ignore any values you've already processed entryValues.removeAll(distinctValues); if (!entryValues.isEmpty()) { System.out.println(++count + " = " + entryValues); distinctValues.addAll(entryValues); } }); 
0
source

Source: https://habr.com/ru/post/1202815/


All Articles