fork download
  1. import java.util.Set;
  2. import java.util.function.Function;
  3. import java.util.stream.Collectors;
  4.  
  5. class Person {
  6. private String id;
  7.  
  8. public Person(String id) {
  9. this.id = id;
  10. }
  11.  
  12. public String getId() {
  13. return id;
  14. }
  15. }
  16.  
  17. public class Main {
  18. public static void main(String[] args) {
  19. Set<Person> initialSet = Set.of(new Person("123"), new Person("456"));
  20. Set<Person> currentSet = Set.of(new Person("456"));
  21.  
  22. Function<Person, String> idMapper = Person::getId;
  23.  
  24. boolean result = areEntriesRemoved(currentSet, initialSet, idMapper);
  25. System.out.println(result); // This will print true
  26. }
  27.  
  28. private static <T> boolean areEntriesRemoved(Set<T> currentSet, Set<T> initialSet, Function<T, String> idMapper) {
  29. Set<String> currentIds = currentSet.stream().map(idMapper).collect(Collectors.toSet());
  30. return initialSet.stream()
  31. .map(idMapper)
  32. .anyMatch(id -> !currentIds.contains(id));
  33. }
  34. }
  35.  
Success #stdin #stdout 0.1s 56216KB
stdin
Standard input is empty
stdout
true