Using Comparable for multiple dynamic fields of VO in java

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

comparing and thenComparing gives compile error

Java needs to know a type of all variables. In many lambdas it can infer a type, but in your first code snippet, it cannot guess the type of s. I think the standard way to solve that problem would be to declare it explicitly: Comparator<String> c = Comparator.comparing((String s) -> s.split(“\\s+”)[0]) .thenComparingInt(s -> Integer.parseInt(s.split(“\\s+”)[1])); … Read more

How do I write a compareTo method which compares objects?

This is the right way to compare strings: int studentCompare = this.lastName.compareTo(s.getLastName()); This won’t even compile: if (this.getLastName() < s.getLastName()) Use if (this.getLastName().compareTo(s.getLastName()) < 0) instead. So to compare fist/last name order you need: int d = getFirstName().compareTo(s.getFirstName()); if (d == 0) d = getLastName().compareTo(s.getLastName()); return d;

Comparing the values of two generic Numbers

This should work for all classes that extend Number, and are Comparable to themselves. By adding the & Comparable you allow to remove all the type checks and provides runtime type checks and error throwing for free when compared to Sarmun answer. class NumberComparator<T extends Number & Comparable> implements Comparator<T> { public int compare( T … Read more

Why doesn’t java.lang.Number implement Comparable? [duplicate]

It’s worth mentioning that the following expression: new Long(10).equals(new Integer(10)) is always false, which tends to trip everyone up at some point or another. So not only can you not compare arbitrary Numbers but you can’t even determine if they’re equal or not. Also, with the real primitive types (float, double), determining if two values … Read more

When to use Comparable and Comparator

Use Comparable if you want to define a default (natural) ordering behaviour of the object in question, a common practice is to use a technical or natural (database?) identifier of the object for this. Use Comparator if you want to define an external controllable ordering behaviour, this can override the default ordering behaviour.

How to implement the Java comparable interface?

You just have to define that Animal implements Comparable<Animal> i.e. public class Animal implements Comparable<Animal>. And then you have to implement the compareTo(Animal other) method that way you like it. @Override public int compareTo(Animal other) { return Integer.compare(this.year_discovered, other.year_discovered); } Using this implementation of compareTo, animals with a higher year_discovered will get ordered higher. I … Read more