fork download
  1. import java.util.ArrayList;
  2. import java.util.HashSet;
  3. import java.util.List;
  4. import java.util.Set;
  5. import java.util.stream.Collectors;
  6. import java.util.stream.Stream;
  7.  
  8. public class Main {
  9. public static class Students {
  10. int id;
  11. String name;
  12.  
  13. public Students(final int id, final String name) {
  14. this.id = id;
  15. this.name = name;
  16. }
  17.  
  18. public int getId() {
  19. return this.id;
  20. }
  21.  
  22. public String getName() {
  23. return this.name;
  24. }
  25. }
  26.  
  27. public static void main(String[] args) {
  28. List<Students> list1 = new ArrayList<>();
  29. list1.add(new Students(1, "Josh"));
  30. list1.add(new Students(2, "Jacob"));
  31. list1.add(new Students(3, "Jane"));
  32. List<Students> list2 = new ArrayList<>();
  33. list2.add(new Students(1, "Josh"));
  34. list2.add(new Students(4, "Jorge"));
  35. final Set<Integer> ids = new HashSet<>();
  36. final List<Students> unique = Stream.of(list1, list2).flatMap(List::stream).filter(s -> ids.add(s.getId()))
  37. .collect(Collectors.toList());
  38. unique.forEach(s -> System.out.println(s.getId() + "-" + s.getName()));
  39. }
  40. }
Success #stdin #stdout 0.12s 36868KB
stdin
Standard input is empty
stdout
1-Josh
2-Jacob
3-Jane
4-Jorge