Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the Comparator interface

I'm new to java, and I'm not really getting how to use the comparator interface. I have an ArrayList of Items in an Inventory class and an Item class. In the Item class I wrote:

public class Item implements Comparator<Item> {
    //stuff
    ...
    @Override
    public int compare(Item a, Item b) {
        if (a.getID().compareToIgnoreCase(b.getID())>0)
            return 1;
        else if (a.getID().compareToIgnoreCase(b.getID())<0)
            return -1;
        else
            return 0;
    }
}

The getID() method just gives the id, which I have to use to alphabetize the items. I'm not sure if this is right, it made me put the @Override annotation, I'm not sure why. Also I wrote an interface that just says:

 public interface Comparator<Item>
{
    int compare(Item a, Item b);
}

I'm not sure about that bit. Also how do I implement this method to sort the arraylist created in the inventory class?

Thanks, if my question doesn't make sense or needs clarification just let me know.

like image 278
bassandguitar Avatar asked Apr 21 '13 00:04

bassandguitar


People also ask

How does Comparator interface work?

Method 2: Using comparator interface- Comparator interface is used to order the objects of a user-defined class. This interface is present in java. util package and contains 2 methods compare(Object obj1, Object obj2) and equals(Object element). Using a comparator, we can sort the elements based on data members.

What is Comparator interface Java?

Java Comparator is an interface for sorting Java objects. Invoked by “java. util. comparator,” Java Comparator compares two Java objects in a “compare(Object 01, Object 02)” format. Using configurable methods, Java Comparator can compare objects to return an integer based on a positive, equal or negative comparison.

Which method is implemented by Comparator interface?

The comparable interface has a method 'compareTo ()' that needs to be overridden in the class implementing the Comparator interface and whose objects are to be sorted. The Comparator interface is used to sort custom objects that are to be sorted based on any other order.


1 Answers

EDIT: First of all, a couple of things:

  1. The @Override annotation should not be mandatory. If Eclipse wants you to put it on, don't worry.
  2. Don't write your own Comparator interface. Delete that definition NAO and use the one provided by Java. Reinventing the wheel probably violates the Unspoken Code of Computer Programming in about 15 different ways. Use import java.util.Comparator; at the very top of your code (before the public class stuff) to a) use the version given by Java and b) make your code compatible with pretty much everything else that exists in the world.

The Comparator interface is not used to create a class that can put itself in order. This is the Comparable interface.

Both are similar, so I will describe both here.

java.util.Comparator

The Comparator interface, as you already know, has one method: compare. Comparator is generic (uses the angle brackets <>) and takes the type it will compare inside the <>. The thing is that Comparators are used to compare items of other classes. For example, I could create a Comparator for java.lang.Integers that returns the opposite of the "natural order" (how Integers are usually ordered).

Comparators are used mostly to give other objects a way to sort their parameters when they are not in natural order. For example, the java.util.TreeSet class takes a Comparator for its sorting capability.

java.lang.Comparable

Comparable's purpose is to say that an object can be compared. It is also generic and takes the type that it can be compared to. For example, a Comparable<String> can be compared to Strings.

Comparable has one method: compareTo(). Unlike Comparator's compare(), compareTo takes one parameter. It works like compare, except it uses the invoking object as one parameter. So, comparableA.compareTo(comparableB) is the same as comparator.compare(comparableA, comparableB).

Comparable mostly establishes the natural order for objects, and is the default way to compare objects. Comparator's role is to override this natural order when one has different needs for data comparison or sorting.

ArrayList Sorting

To sort a List, you could use the method already available: scroll down to sort on the java.util.Collections class. One method takes a Comparator, the other does not. sort is static; use Collections.sort(...), not Collections c = new Collections(); c.sort(...). (Collections doesn't even have a constructor anyway, so meh.)

like image 68
ameed Avatar answered Sep 23 '22 06:09

ameed