You are requesting a Java class for beginners. There are many examples on the net about comparators, trees, and everything else in Java. You definitely need to spend time and read everything you see, but many of these examples are crystal clear. If you are trying to learn something, and it does not work for you, do not spend more time on it. Just google again, even if it's the 15th or 20th explanation that finally works for you. This is very common. Just donโt read anything until you understand it.
Of course, you have a class for storing your string, which implements Comparable, as @regulus suggests, except for using a name instead of a label :) Save the character in the class too for future reference or if you want to use it for secondary comparison (after comparing names) . This will give your elements a natural order. When you instantiate each object ...
Paste them into the Java TreeSet instance. Here is an example of its use:
import java.util.TreeSet; import java.util.Iterator; public class IterateThroughElementsOfTreeSetExample { public static void main(String[] args) {
This would be very fast, because it sorted when you insert the keys.
source share