0

Comparator is an interface, and I believe it can't be instantiated,but please see the code snippet below

Collections.sort( list, new Comparator<Map.Entry<String, Integer>>()
                { 
                             //Compare method
                        });

Here comparator interface has been instantiated,is this the correct way to code. Some one please help. Thanks

Lij
  • 9
  • 2

5 Answers5

3

This one of the correct ways to implement an interface and this implementation is called anonymous class.

Note that you're implementing an instance of Comparator that cannot be referred as a variable.

Another note: You will have to provide implementation of the

public int compareTo(Map.Entry<String, Integer> entry1, 
                     Map.Entry<String, Integer> entry2) { .. }

method, otherwise it won't compile.

Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
1

This is an acceptable way to code. The compiler will make your implementation of the Comparator interface into a class. Because you don't provide a name for the class, this is called an anonymous class.

I have no issue with the use of anonymous classes, but I prefer to use a variable, for an instance of an anonymous class. This gives you the opportunity to supply a name that says what kind of comparison your comparator will do. In this case, I might write something like this.

Comparator<Map.Entry<String, Integer>> byValueDescendingThenKey = 
        new Comparator<Map.Entry<String, Integer>>() {

    @Override
    public int compare(Map.Entry<String, Integer> entry1, Map.Entry<String, Integer> entry2) {
        int valueComparison = entry1.getValue().compareTo(entry2.getValue());
        if (valueComparison != 0) {
            return -valueComparison;
        } else {
            return entry1.getKey().compareTo(entry2.getKey());
        }
    }
};

Collections.sort(list,byValueDescendingThenKey);

Notice how if there is a nice descriptive name for the variable that references the Comparator, the line that does the sorting is much clearer.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
0

The anonymous instance of Comparator must satisfy the interface by implementing the compare method. The Comparator is a SAM (Single Abstract Method) interface, which allows it to be instantiated via an anonymous class.

Collections.sort( list, new Comparator<Map.Entry<String, Integer>>()
{ 
    public int compare(Map.Entry<String,Integer> entry1, Map.Entry<String,Integer> entry2) {

    }
});
Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
0
    Comparator<Map.Entry<String, Integer>> comparator = new Comparator<Map.Entry<String, Integer>>() {

        @Override
        public int compare(Entry<String, Integer> arg0, Entry<String, Integer> arg1) {
            // logic here
            return 0;
        }

    };

This is how you implement the Comparator interface. Obviously you won't return 0. This construct is called an anonymous inner class. It makes sense if you are only going to use this implementation in one place.

Adam Arold
  • 29,285
  • 22
  • 112
  • 207
0

1) You may implement the Comparator in a separate class and refer to it as a second argument in the Collection.sort() method

2) Or for the second argument, you can put the implementation "in place" i.e.

Collections.sort( (Collection) argument,  new Comparator<Map.Entry<String, Integer>>(){ /*your implementation*/}  );

which is done through an anonymous class (your case).

DayaMoon
  • 357
  • 2
  • 7