What is the best way to get min and max value from a list of Comparables that main contain null values?
What’s wrong with Collections.max? And why do you care about null safety? Are you sure you want to allow nulls to be in your Collection?
What’s wrong with Collections.max? And why do you care about null safety? Are you sure you want to allow nulls to be in your Collection?
Read the Java Generics Tutorial. The thing between the angle brackets is a type parameter – Comparable is a generic class, and in this case the angle brackets mean that the class is comparable to other BigIntegers. For a little more clarification in this case, have a look at the Javadocs for Comparable in 1.5. … Read more
You can provide a Comparator for comparing any type you wish, Comparable or otherwise. For Arrays and Collections you use Arrays.sort(array, myComparator); Collections.sort(list, myComparator); Even sorted collections like TreeSet can take a custom Comparator e.g. Collections.sort(books, new Comparator<Book>() { public int compare(Book b1, Book b2) { return if b1 is greater return +1, if b2 … Read more
You cannot do operator overloading in Java. This means you are not able to define custom behaviors for operators such as +, >, <, ==, etc. in your own classes. As you already noted, implementing Comparable and using the compareTo() method is probably the way to go in this case. Another option is to create … Read more
Kotlin’s stdlib offers a number of useful helper methods for this. First, you can define a comparator using the compareBy() method and pass it to the sortedWith() extension method to receive a sorted copy of the list: val list: List<Foo> = … val sortedList = list.sortedWith(compareBy({ it.a }, { it.b }, { it.c })) Second, … Read more
1)You should write two Comparator for sorting on age and name separately, and then use the Collections.sort(List,Comparator). Something like this: class StudentVO { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int … Read more
To make classes comparable, you only need to implement __lt__ and decorate the class with functools.total_ordering. You should also provide an __eq__ method if possible. This provides the rest of the comparison operators so you don’t have to write any of them yourself.
Here’s a simple but realistic example of what can happen if a comparison method is inconsistent with equals. In the JDK, BigDecimal implements Comparable but its comparison method is inconsistent with equals. For example: > BigDecimal z = new BigDecimal(“0.0”) > BigDecimal zz = new BigDecimal(“0.00”) > z.compareTo(zz) 0 > z.equals(zz) false This is because … Read more
Comparable doesn’t allow null simply because: a.compareTo(b) == -b.compareTo(a) for all objects a and b where !a.equals(b). More specifically: a.equals(b) ? b.equals(a) && a.compareTo(b) == 0 && b.compareTo(a) == 0 && a.hashCode() == b.hashCode() : !b.equals(a) && a.compareTo(b) != 0 && a.compareTo(b) == -b.compareTo(a) must evaluate to true to satisfy the relevant contracts. So null … Read more
What you need to do is implement a custom Comparator. And then use: Collections.sort(yourList, new CustomComparator<YourClass>()); Specifically, you could write: (This will create an Anonymous class that implements Comparator.) Collections.sort(yourList, new Comparator<YourClass>(){ public int compare(YourClass one, YourClass two) { // compare using whichever properties of ListType you need } }); You could build these into … Read more