Close

Java Collections - Comparator.compare() Examples

Java Collections Java Java API 


Interface:

java.util.Comparator

java.util.ComparatorComparatorLogicBig

Method:

int compare(T o1, T o2)

This method returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second. It is used to compare two objects for sorting/ordering.

Note that Comparator is a functional interface and int compare(T o1, T o2) is the only abstract method. Other declared methods are either default or static.


Examples


package com.logicbig.example.comparator;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class CompareExample {

public static void main(String... args) {

//Comparator is a functional interface
Comparator<Integer> c = (o1, o2) -> {
if (o1 == o2) {
return 0;
} else if (o1 == null) {
return -1;
} else if (o2 == null) {
return 1;
} else {
return Integer.compare(o1, o2);
}
};

int result = c.compare(1, 4);
System.out.println(result);

result = c.compare(4, 1);
System.out.println(result);

result = c.compare(4, 4);
System.out.println(result);

//Alternatively
result = Integer.compare(1, 4);
System.out.println(result);

//sorting a list
List<Integer> list = Arrays.asList(1, 3, 2, 5, 4, 7);
Collections.sort(list, c);
System.out.println(list);

//or
list = Arrays.asList(1, 3, 2, 5, 4, 7);
Collections.sort(list, Integer::compareTo);
System.out.println(list);
}
}

Output

-1
1
0
-1
[1, 2, 3, 4, 5, 7]
[1, 2, 3, 4, 5, 7]




In this example class Page implements Comparable interface. The difference between interfaces, Comparable and Comparator is; Comparable is implemented by a class to specify how this object should be ordered when sorted as an item in a collection, whereas, a Comparator specifies how to compare two objects to order them in a collection.

package com.logicbig.example.comparator;

import java.util.*;

public class CompareExample2 {

public static void main(String... args) {
List<Page> list = createExamplePages();

System.out.printf("before sort: %s%n", list);
Collections.sort(list, Page::compare);
System.out.printf("after sort: %s%n", list);

SortedSet<Page> set = new TreeSet<>(createExamplePages());
System.out.printf("sorted set: %s%n", set);
}

private static List<Page> createExamplePages() {
return Arrays.asList(
Page.create(3, "content 3"),
Page.create(2, "content 2"),
Page.create(1, "content 1"));
}

private static class Page implements Comparable<Page> {
private int index;
private String content;

public Page(int index, String content) {
this.index = index;
this.content = content;
}

@Override
public int compareTo(Page other) {
return compare(this, other);
}


public static int compare(Page o1, Page o2) {
if (o1 == o2) {
return 0;
} else if (o1 == null) {
return -1;
} else if (o2 == null) {
return 1;
} else {
return Integer.compare(o1.index, o2.index);
}
}


@Override
public String toString() {
return "Page{" +
"index=" + index +
", content='" + content + '\'' +
'}';
}

public static Page create(int index, String content) {
return new Page(index, content);
}
}
}

Output

before sort: [Page{index=3, content='content 3'}, Page{index=2, content='content 2'}, Page{index=1, content='content 1'}]
after sort: [Page{index=1, content='content 1'}, Page{index=2, content='content 2'}, Page{index=3, content='content 3'}]
sorted set: [Page{index=1, content='content 1'}, Page{index=2, content='content 2'}, Page{index=3, content='content 3'}]




See Also