fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.time.LocalDate;
  4. import java.time.Month;
  5. import java.util.Arrays;
  6. import java.util.Comparator;
  7. import java.util.List;
  8. import java.util.TreeSet;
  9. import java.util.stream.Collectors;
  10.  
  11. /* Name of the class has to be "Main" only if the class is public. */
  12. class Ideone
  13. {
  14. private static final List<Unique> uniqueList = Arrays.asList(
  15. new Unique("a", null)
  16. ,new Unique("b", null)
  17. ,new Unique("b", LocalDate.of(2016, Month.SEPTEMBER, 20))
  18. ,new Unique("c", LocalDate.of(2016, Month.SEPTEMBER, 19))
  19. ,new Unique("c", LocalDate.of(2016, Month.SEPTEMBER, 20))
  20. );
  21.  
  22. public static void main(String[] args)
  23. {
  24. System.out.println("list ordered, with duplicates: ");
  25. uniqueList.stream().sorted().forEach(System.out::println);
  26.  
  27. System.out.println("=================");
  28. System.out.println("list ordered, duplicates removed: ");
  29. Comparator<Unique> uniqueComp = (a, b) -> a.getName().compareTo( b.getName() );
  30. TreeSet<Unique> sortedFilterd = uniqueList.stream().sorted().collect(Collectors.toCollection(() -> new TreeSet<Unique>(uniqueComp)));
  31. sortedFilterd.forEach(System.out::println);
  32. }
  33. public static class Unique implements Comparable<Unique>
  34. {
  35. private String name;
  36. private LocalDate dateOfDeletion;
  37.  
  38. public Unique(String name, LocalDate dateOfDeletion)
  39. {
  40. this.name = name;
  41. this.dateOfDeletion = dateOfDeletion;
  42. }
  43.  
  44. public String getName() {
  45. return name;
  46. }
  47.  
  48. public void setName(String name) {
  49. this.name = name;
  50. }
  51.  
  52. public LocalDate getDateOfDeletion() {
  53. return dateOfDeletion;
  54. }
  55.  
  56. public void setDateOfDeletion(LocalDate dateOfDeletion) {
  57. this.dateOfDeletion = dateOfDeletion;
  58. }
  59.  
  60. @Override
  61. public String toString() {
  62. return "Unique [name=" +name + ", dateOfDeletion="+dateOfDeletion+"],";
  63. }
  64.  
  65. /**
  66. * order based on name ascending, and date descending
  67. */
  68. @Override
  69. public int compareTo(Unique rhs) {
  70. Comparator<Unique> reverseDateComparator = (a, b) -> {
  71. LocalDate thisDodel = a.getDateOfDeletion() == null ? LocalDate.now().plusDays(1) : a.getDateOfDeletion();
  72. LocalDate thatDodel = b.getDateOfDeletion() == null ? LocalDate.now().plusDays(1) : b.getDateOfDeletion();
  73. return thatDodel.compareTo(thisDodel);
  74. };
  75. return Comparator.comparing(Unique::getName).thenComparing(reverseDateComparator).compare(this, rhs);
  76. }
  77. }
  78. }
Success #stdin #stdout 0.09s 711680KB
stdin
Standard input is empty
stdout
list ordered, with duplicates: 
Unique [name=a, dateOfDeletion=null],
Unique [name=b, dateOfDeletion=null],
Unique [name=b, dateOfDeletion=2016-09-20],
Unique [name=c, dateOfDeletion=2016-09-20],
Unique [name=c, dateOfDeletion=2016-09-19],
=================
list ordered, duplicates removed: 
Unique [name=a, dateOfDeletion=null],
Unique [name=b, dateOfDeletion=null],
Unique [name=c, dateOfDeletion=2016-09-20],