0

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?

Omar Hrynkiewicz
  • 502
  • 1
  • 8
  • 21
Charlie Harper
  • 379
  • 4
  • 7
  • 21

2 Answers2

2

Here's a snippet from the TreeSet javadoc:

A NavigableSet implementation based on a TreeMap. The elements are ordered using their natural ordering, or by a Comparator provided at set creation time, depending on which constructor is used.

Natural ordering refers to the order that you enforced by making the Car class implement the Comparable interface. Just add your cars to the TreeSet instance and they will be sorted for you.

nattyddubbs
  • 2,085
  • 15
  • 24
1

Use a Comparator instead of Comparable in Car. Comparator is external to the class it compares, whereas Comparable is internal to that class. A more detailed explanation is here: https://stackoverflow.com/a/4108764/1838970

Now, on to your question. Implement this Comparator:

public class CarComparatorByName implements Comparator<Car> {
    public int compare(Car o1, Car o2) {
        return o1.getName().compareTo(o2.getName());
    }
}

Now make a TreeSet using that Comparator:

Set<Car> cars = new TreeSet<Car>(new CarComparatorByName());

That TreeSet will now use the comparator you created.

Alternatively, as noted in the comments below, you could just keep Car as a Comparable and simply throw them into a TreeSet upon creation. That TreeSet will use the natural sort order defined in Car implements Comparable.

Community
  • 1
  • 1
Clete2
  • 367
  • 1
  • 8
  • Note that `TreeSet` implements [`SortedSet`](http://docs.oracle.com/javase/7/docs/api/java/util/SortedSet.html) which states: *The elements are ordered using their [natural ordering](http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html) [<- links to `Comparable`], or by a `Comparator` typically provided at sorted set creation time.*, which means you don't need an external `Comparator` unless you want to sort using a different algorithm. – Luiggi Mendoza Mar 26 '14 at 19:45
  • Ah yes, you could use either. I would argue that "name" isn't a natural sort order for Cars, but I suppose it depends upon your application. If he keeps his Comparable, he can just throw his existing Set into a TreeSet and it'll be sorted. – Clete2 Mar 26 '14 at 19:46
  • Then this answer can be moved to a comment saying: *I don't think cars should be compared by name only*... – Luiggi Mendoza Mar 26 '14 at 19:47