i have a class representing cars:
public class Car implements Comparable<Car> {
String name;
int value;
...
@Override
public int compareTo(Car o) {
return name.compareTo(o.name);
}
}
and another class representing races:
public class Race {
cars = new HashSet<Car>();
...
public Collection<Car> sortByName() {
List<Car> carList = new ArrayList<>(cars);
Collections.sort(carList);
return carList;
}
}
Its my implementation to sorting the Set, i know there is a TreeSet but i dont know how to compare it by TreeSet instead of HashSet, because if i used TreeSet i couldnt find method comprator() in it, can anyone help me if im doing well or if not how to use TreeSet?