Stop(Id, Name)
is a java class, and i want to store these stop objects in a java.util.Set
and those objects should be sorted according to the Id
of Stop
. this is my comparator
public class StopsComparator implements Comparator{
@Override
public int compare(Object o1, Object o2) {
// TODO Auto-generated method stub
Stop stop1 = (Stop)o1;
Stop stop2 = (Stop)o2;
return stop1.getStopId().compareTo(stop2.getStopId());
}
}
private Set<Stop> stops = new TreeSet<Stop>(new StopsComparator());
but its not giving correct result?
Does Stop implement an equals method that works on the same field as your comparator? If not then that will lead to problems. You also might want to switch to have your object implement Comparable (although that wouldn't fix the problem you're seeing).
Once you implement an equals()
method, then you should also implement a hashCode()
method that works on the same field.
Findbugs would have probably told you these things. Its extremely useful.
The following code works for me -
public class Stop {
private Long id;
private String name;
public Stop(Long id, String name) {
this.id = id;
this.name = name;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "Stop{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
private static class StopComparator implements Comparator<Stop> {
public int compare(Stop o1, Stop o2) {
return o1.getId().compareTo(o2.getId());
}
}
public static void main(String[] args) {
Set<Stop> set = new TreeSet<Stop>(new StopComparator());
set.add(new Stop(102L, "name102"));
set.add(new Stop(66L, "name66"));
set.add(new Stop(72L, "name72"));
System.out.println(set);
}
}
prints -
[Stop{id=66, name='name66'}, Stop{id=72, name='name72'}, Stop{id=102, name='name102'}]
Ofc you need to implement equals
and hashcode
so that class behaves consistently in each Set
implementation, but for TreeSet
this should work as is since TreeSet
relies on compareTo
method while performing add
, remove
or contains
operations (instead of equals
like HashSet
).
This is from the Comparator
docs:
The ordering imposed by a comparator c on a set of elements S is said to be consistent with equals if and only if c.compare(e1, e2)==0 has the same boolean value as e1.equals(e2) for every e1 and e2 in S.
Caution should be exercised when using a comparator capable of imposing an ordering inconsistent with equals to order a sorted set (or sorted map). Suppose a sorted set (or sorted map) with an explicit comparator c is used with elements (or keys) drawn from a set S. If the ordering imposed by c on S is inconsistent with equals, the sorted set (or sorted map) will behave "strangely." In particular the sorted set (or sorted map) will violate the general contract for set (or map), which is defined in terms of equals.
I would recommend to try implementing equals
and hashCode
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.