Comparator Example For BuildIn Data Type : Collection « Apache Common « Java






Comparator Example For BuildIn Data Type

import org.apache.commons.collections.ComparatorUtils;
import org.apache.commons.collections.comparators.BooleanComparator;
import org.apache.commons.collections.comparators.FixedOrderComparator;

import java.util.Arrays;
import java.util.Comparator;

public class ComparatorExampleForBuildInDataType {

  private static Comparator boolComp;
  private static Comparator fixedComp;

  private static Boolean boolParams[] = {new Boolean(true), new Boolean(true),
                                         new Boolean(false), new Boolean(false)};
  private static String  stringParams[] = {"Russia", "Canada", "USA", "Australia", "India"};
  
  public static void main(String args[]) {
    ComparatorExampleForBuildInDataType example = new ComparatorExampleForBuildInDataType();
    example.createComparators();

    Arrays.sort(boolParams, boolComp); 

    example.printArray(boolParams);

    Arrays.sort(stringParams);

    example.printArray(stringParams);

    Arrays.sort(stringParams, fixedComp);

    example.printArray(stringParams);
  }

  private void createComparators() {
    boolComp = ComparatorUtils.booleanComparator(true);
    fixedComp = new FixedOrderComparator(stringParams);
  }

  private void printArray(Object[] array) {
    for(int i = 0; i < array.length; i++)
      System.err.println(array[i]);
  }

}
           
       








ApacheCollectionComparatorExampleForBuildInDataType.zip( 514 k)

Related examples in the same category

1.Collection Bag
2.Transformer Example
3.Collection BidiMap
4.Collection Buffer
5.Collection Closure
6.Comparator Example For User Defined Class
7.Cookie Bag 2
8.Factory Example 1
9.HashMap Example 1
10.List Example 1
11.MapHeaven 1
12.Multi Key Example 1
13.MultiKey Example 2
14.Set Example 1
15.Set Example 2
16.Bean Comparator ( Sorting based on Properties of class )Bean Comparator ( Sorting based on Properties of class )