/* Demonstration of Comparator. Use to compare strings based on length. this is a function object as it has no data; it is merely a function (or set of functions) we can pass around */ public class StringLengthComparator implements Comparator { public int compare(String a,String b) { return a.length()-b.length(); } public static void main(String[] args) { StringLengthComparator comp=new StringLengthComparator(); String a="short"; String b="really long"; System.out.println("Comparing '"+a+"' & '"+b+"': "+comp.compare(a,b)); } }