Java Comparator Class example


Comparator class is used to compare two different objects. Collections’ sorting methods are capable of using this comparator class to sort different objects defined by developers.

Here is an example; for sorting Computer objects according to Ram sizes.

Computer.java

package comparatorexample;

/**
 *
 * @author tunatore
 */
public class Computer {
    private int ID = 0;
    private int Ram = 0;

    /**
     * @return the Ram
     */
    public int getRam() {
        return Ram;
    }

    /**
     * @param Ram the Ram to set
     */
    public void setRam(int Ram) {
        this.Ram = Ram;
    }

    /**
     * @return the ID
     */
    public int getID() {
        return ID;
    }

    /**
     * @param ID the ID to set
     */
    public void setID(int ID) {
        this.ID = ID;
    }
   
}

ComputerComparator.java

package comparatorexample;

import java.util.Comparator;

/**
 *
 * @author tunatore
 */
public class ComputerComparator implements Comparator {

    public int compare(Computer C1, Computer C2) {

        if(C1.getRam() > C2.getRam()) //return 1 if the computer has more RAM than Computer compared
            return 1;
        else if (C1.getRam() < C2.getRam()) //return -1 if the computer has less RAM than Computer compared
            return -1;
        else
            return 0; //return 0 if both computers have same Ram size
    }

}

Main.java

package comparatorexample;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

/**
 *
 * @author tunatore
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        ListcomputerList = new ArrayList();
        for (int i = 0; i < 20; i++) {
            Computer computer = new Computer();
            computer.setID(i);
            int min = 256; //mb
            int max =  16384; //mb
            computer.setRam(((int) (Math.random() * (max – min + 1) ) + min ));
            computerList.add(computer);
        }
        Collections.sort(computerList, Collections.reverseOrder(new ComputerComparator()));
        System.out.println(“###Sorted Computers according to RAM size####”);
        for (Iterator<Computer> it = computerList.iterator(); it.hasNext();) {
            Computer computer = it.next();
            System.out.println(“Computer ID: ” + computer.getID() + ” Computer RAM:” + computer.getRam());
        }
    }

}

Output of the program;

run:
###Sorted Computers according to RAM size####
Computer ID: 11 Computer RAM:15492
Computer ID: 5 Computer RAM:15337
Computer ID: 13 Computer RAM:12810
Computer ID: 15 Computer RAM:12212
Computer ID: 16 Computer RAM:11704
Computer ID: 9 Computer RAM:10911
Computer ID: 10 Computer RAM:10145
Computer ID: 8 Computer RAM:10107
Computer ID: 17 Computer RAM:9739
Computer ID: 18 Computer RAM:8619
Computer ID: 2 Computer RAM:7298
Computer ID: 7 Computer RAM:7107
Computer ID: 19 Computer RAM:5788
Computer ID: 14 Computer RAM:5781
Computer ID: 4 Computer RAM:4221
Computer ID: 12 Computer RAM:3912
Computer ID: 6 Computer RAM:3015
Computer ID: 0 Computer RAM:2968
Computer ID: 1 Computer RAM:2219
Computer ID: 3 Computer RAM:2013
BUILD SUCCESSFUL (total time: 0 seconds)

Download Source

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s