Iterator, Comparator, and Comparable
Learn the difference between a comparator, a comparable, and an iterator.
We'll cover the following...
The Java Collections Framework provides three important primary interfaces: Comparator
, Comparable
, and Iterator
. Comparison plays a significant role in sorting and shuffling algorithms. Iteration is also crucial.
We’ll see a few code snippets where these three interfaces are implemented.
To sort elements, we can use TreeSet
and TreeMap
from the Java Collections Framework. But it’s the Comparator
or the Comparable
interface that precisely defines what sorted order means.
Java code for the Comparator
interface
We can have objects that encapsulate ordering by implementing the Comparator
and Comparable
interfaces. Take a look at the following code snippet:
//How the Comparator and Comparable interfaces are implemented by a class//to sort String and Integer data types provided by List and//ArrayList data structuresimport java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.List;class Account implements Comparator<Account>, Comparable<Account> {private String accountHoldersName;private int accountNumber;Account() {}Account(String name, int number) {accountHoldersName = name;accountNumber = number;}public void setAccountHoldersName(String accountHoldersName) {this.accountHoldersName = accountHoldersName;}public String getAccountHoldersName() {return accountHoldersName;}public void setAccountNumber(int accountNumber) {this.accountNumber = accountNumber;}public int getAccountNumber() {return accountNumber;}//overriding the compareTo() method to sort the name//@param account//@returnpublic int compareTo(Account account) {return (this.accountHoldersName).compareTo(account.accountHoldersName);}//overriding the compare() method to sort the account number//@param account//@param anotherAccount//@returnpublic int compare(Account account, Account anotherAccount) {return account.accountNumber - anotherAccount.accountNumber;}}//ComparatorInterfaceExampleclass Main{public static void main(String[] args) {// list of account objectsList<Account> listOfAccounts = new ArrayList<Account>();listOfAccounts.add(new Account("Sanjib", 203));listOfAccounts.add(new Account("Jason", 205));listOfAccounts.add(new Account("John", 201));listOfAccounts.add(new Account("Hicky", 204));listOfAccounts.add(new Account("Amubrata", 202));// sorting the ArrayListCollections.sort(listOfAccounts);//printing the sorted namesSystem.out.println("Printing the sorted names of account holders: ");for(Account account: listOfAccounts){System.out.print(account.getAccountHoldersName() + ", ");}//sorting the ArrayList with the help of ComparatorCollections.sort(listOfAccounts, new Account());System.out.println(" ");//sorting based on account numbersSystem.out.println("Printing names of account holders based on sorted account numbers in ascending order: ");for(Account account: listOfAccounts)System.out.print(account.getAccountHoldersName() + " : "+ account.getAccountNumber() + ", ");}}
First of all, we sort the names of the account holders. Then, we print the names of account holders based on sorted account numbers in ascending order.
Printing the sorted names of account holders:
Amubrata, Hicky, John, Jason, Sanjib,
Printing names of account holders based on sorted account numbers in ascending order:
John : 201, Amubrata : 202, Sanjib : 203, Hicky : 204, Jason : 205,