I am trying to understand how to work with TreeSet with Comparator implementation. Code is below.
The objects from the numbers Set should be sorted according to the following rules:
*The left border of ranges is always less than the right one, but their values can be negative as well.
The problem is that TreeSet can contain only unique values, so current code cut some objects with the same range in a result output. So it seems that I need to add an additional condition with this comparing(), but I am stuck with what exactly condition should be there. I believe that there is a way to change only getComparator() method here.
import java.util.*;
class LongRange {
public static void main(String[] args) {
Set<LongRange> numbers = new TreeSet<>(LongRange.getComparator());
numbers.add(new LongRange(0, 5));
numbers.add(new LongRange(2, 4));
numbers.add(new LongRange(1, 4));
numbers.add(new LongRange(1, 7));
numbers.add(new LongRange(3, 5));
numbers.add(new LongRange(-10, 1));
numbers.add(new LongRange(-20, -9));
numbers.add(new LongRange(-21, -10));
numbers.forEach(System.out::println);
}
private final long left;
private final long right;
public static Comparator<LongRange> getComparator() {
return Comparator.comparing(a -> Math.subtractExact(a.right, a.left));
}
public LongRange(long left, long right) {
this.left = left;
this.right = right;
}
public long getLeft() {
return left;
}
public long getRight() {
return right;
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (other == null || getClass() != other.getClass()) {
return false;
}
LongRange longRange = (LongRange) other;
return left == longRange.left &&
right == longRange.right;
}
@Override
public int hashCode() {
return Objects.hash(left, right);
}
@Override
public String toString() {
return String.format("%d %d", left, right);
}
}
Summarizing comments from Ole V.V. and applying them for my case, solution is presented below. The tricky thing for me in this case was that I need to declare a explicitly - (LongRange a).
public static Comparator<LongRange> getComparator() {
return Comparator.comparingLong((LongRange a) -> a.right- a.left)
.thenComparingLong((LongRange a) -> a.left);
}