Sean
Sean

Reputation: 1313

Java - How to make Comparable Array of Strings

I feel this is a simple question, but I cannot find the write format online. How would I go about making a Comparable array of strings?

Like this:

Comparable<String> = {"A", "C", "J", "O", "Z"};

Or do I start it like:

Comparable<String> = new Comparable<String>(); 

Similar to an arraylist, I am not to sure.

I am using the Comparable interface so I can declare two lists, one of strings one of ints, and I may sort them using a specific sorting algorithm, all my current problem is how to initialize the array

Thanks

Upvotes: 1

Views: 4665

Answers (3)

Kunda
Kunda

Reputation: 463

The OP's intent is not clear, but if the purpose of the question is how to create an array that is Comparable - since it's an interface that array does not implement, and since array cannot be overridden - it's simply not possible.

However, one can use a Comparator for a specific type for the purpose of comparing two instances of that type.

Most of the classes in Google Guava's com.google.common.primitives package have a static lexicographicalComparator() method returning a Comparator for arrays of the primitive type the class handles.

Guava currently does not have a similar solution for Object arrays, but if you can use an Iterable instead the static Comparators.lexicographical(Comparator) or the Ordering.lexicographical() on an existing Ordering might help.

Following what I understood as the OP's intent, let's assume we have two arrays:

String[] arr1 = {"A", "C", "J", "O", "Z"};
String[] arr2 = {"A", "C", "J", "O", "X"}; // different last element

We can now compare these two arrays using Ordering.lexicographical():

        int comparison = Ordering.natural()
                .lexicographical().compare(Arrays.asList(arr1), Arrays.asList(arr2));

The result will be positive, since arr1 is considered greater than arr2, because the first element in arr1 that was not equal to its corresponding element in arr2 was greater according to Ordering.natural().

Upvotes: 1

ajb
ajb

Reputation: 31699

Comparable<T> is an interface, so you can't directly declare anything to be of that type. The normal idiom for Comparable is that if you're declaring your own class to be comparable, you declare it like this:

class MyClass implements Comparable<MyClass> {
    // ... other fields, methods

    @Override
    public int compareTo(MyClass other) {
        ... something that returns <0, 0 or >0
    }
}

If you look at the language's definition of String here, you'll see that String already implements Comparable<String>. So a String is already comparable and already has a compareTo method. You don't need to do anything else to make it comparable.

To set up an array, the syntax is

String[] myArray = {"A", "C", "J", "O", "Z"};

To set up an ArrayList (which can grow or shrink),

ArrayList<String> myList = new ArrayList<>(Arrays.asList("A", "C", "J", "O", "Z"));

Upvotes: 1

Michael
Michael

Reputation: 2773

You do not need to create an instance of Comparable. Because the String class already implements this interface, you can simply call the method .compareTo(String otherString)

ArrayList<String> myList = new ArrayList<String>();
myList.add("hello");
myList.add("hello");
System.out.println(myList.get(0).compareTo(myList.get(1))); //prints out 0

You can just populate the list like usual

Upvotes: 1

Related Questions