Wednesday, October 16, 2013

Collections

Collections is a useful class in Java that can be used with List data structures such as ArrayList, LinkedList, Stack, Vector, etc. The Collections has static methods that you can use to sort the List, search an element of the List, count the number of duplicate elements in the List (frequency), reverse the List, check whether a list contains in another list, and find max and min elements of the List.

To sort the elements in the List, you will use the sort method. This method will take the list object that you want to sort. Naturally the sort method sorts the List in ascending order (from small to big). In the example code below, ten random numbers from 1 to 6 are generated and added to the ArrayList obbject, IntList. The IntList is sorted in ascending order by calling the sort method of the Collections class.

ArrayList<Integer> IntList=new ArrayList<Integer>();
Random random=new Random();
for(int i=0;i<10;i++){
   IntList.add(1+Math.abs(random.nextInt())%6);
}

Collections.sort(IntList);
System.out.println(IntList);


If you want to control the sort orders of the List, you need to implement the Comparator interface. This interface has the compare method that is used to compare elements of the List. The return from this method will instruct the Collections to order the List in ascending or descending order. The following Com class implement is example of implementing the Comparator interface. You can use the Com class to order the List in ascending or descending. The Com class has two constructors. One is the default constructor. By using the default constructor, the List will be sorted in ascending order. The second constructor allows you to specify an order value to the Com class. To sort the List descending order, you will provide a negative number. By supplying the positive number, the List will be sorted in ascending order the same as using the default constructor. If you provide zero, the List will not be sorted.

class Com implements Comparator<Object>{
   int order=1;

  //default constructor
   Com(){

   }


   Com(int order){
    this.order=order;
  }

    public int compare(Object x, Object y){
       if (x.toString().compareTo(y.toString())>0) return order;
       else if (x.toString().compareTo(y.toString())<0) return -1*order;
       else return 0;

   }
}


To use the Com class, you need to create its object and supply it to the sort method of the Collections class.

Collections.sort(IntList, new Com(-1)); //sort List in descending order
System.out.println(IntList)


To search an element of a List object, you will use the binarySearch method. This method uses the binary search algorithm to find the specified element in the List. In List data structures, the element of the List can be located very fast by using the binary search technique when the List is sorted. So, you need to sort the List before using the binarySearch method. The BinarySearch method returns the position of the matched element in the List. If the specified element is not found in the list, it returns -1. The example code below returns the position of the 3 element of the list if it is found.

int pos=Collections.binarySearch(IntList, 3);

In addition to search for a specific element of the List, you can search for a sub list in the List. In the example code below, the sub list, SubList contains two elements--2 and 3. This sub list will be searched in the IntList by using lastIndexOfSubList method. If the sub list, SubList is in the list, IntList, its position will be returned otherwise the -1 value is returned.

ArrayList<Integer> SubList=new ArrayList<Integer>();
SubList.add(2);
SubList.add(3);
int pos=Collections.lastIndexOfSubList(IntList, SubList);



You can count the number of duplicate elements in the List by using the frequency method. This method takes two arguments. One is the List object another is the element that you want to count. The example code below counts the number of the 2 elements that occur in the List.

int count=Collections.frequency(x, 2);

To reserve the List, you will use the reverse method. This method takes only the List object that you want to reverse.

Collections.reverse(IntList);

The Collections also provides the max and min methods to find the maximum and minimum elements of the List. To find the max and min elements of the IntList, you can write as shown below.

int maxele=Collections.max(IntList);
int minele=Collections.min(IntList);

No comments:

Post a Comment