fork download
  1. import java.util.*;
  2. class Test {
  3. public static void main(String[] args) {
  4. TreeMap<String, List<String>> lengthMap = new TreeMap<>(Comparator.comparingInt(String::length));
  5.  
  6. List<String> strings = new ArrayList<>(Arrays.asList("The quick brown fox jumps over the lazy dog".split(" ")));
  7. for (String s : strings) {
  8. lengthMap.computeIfAbsent(s, k -> new ArrayList<>()).add(s);
  9. }
  10.  
  11. Set<String> toRemove = lengthMap.keySet();
  12.  
  13. System.out.println("List before: " + strings);
  14. System.out.println("Set to remove: " + toRemove);
  15.  
  16. strings.removeAll(toRemove);
  17. System.out.println("List after:" + strings);
  18. }
  19. }
  20.  
Success #stdin #stdout 0.13s 2184192KB
stdin
Standard input is empty
stdout
List before: [The, quick, brown, fox, jumps, over, the, lazy, dog]
Set to remove: [The, over, quick]
List after:[]