Saturday, 16 August 2014

Collections utility class and it's methods(sort, fill, addAll, binarySearch, frequency, min, max, copy etc.)

Collections utility class is provided in java.util package. It is used to perform some more operation with java collections. It's methods like sort, fill, addAll, binarySearch, frequency, min, max, replaceAll, reverse, swap etc are provides respected specific functionality with collections.
This article uses some important methods of Collections utility class.

Some important methods are used in this code snippet.

public static void main(String[] args) {

List<String> list=new ArrayList<String>();
list.add("one");
list.add("two");
list.add("three");
list.add("four");
list.add("five");

//Add some more elements in the list
        Collections.addAll(list, "hi","fg");
        System.out.println("added some more elements in the list \n "+list);

       //Sort element of a list
        Collections.sort(list);
        System.out.println("sorted list. \n "+list);
        
        //search any element in the list.
        int a=Collections.binarySearch(list, "hi");
        System.out.println("search element get index. \n "+a);
        
        //get list of a particular type, but this type should be available in the list.
        List ll;
        ll = Collections.checkedList(list, String.class);
        System.out.println(ll);
        
       // Copy a list elements in another list.
        List<String> list2=new ArrayList<String>();
list2.add("apple");
list2.add("orange");
list2.add("mango");
        Collections.copy(ll, list2);
        System.out.println(ll);
        
        //Create an empty object of enumeration and Iterator, available but not required.
        Collections.emptyEnumeration();
        Collections.emptyIterator();
        
        //all are used to create a new object of particular type.
        Collections.emptyList();
        Collections.emptyListIterator();
        Collections.emptyMap();
        Collections.emptySet();
        
        //check the frequency of a particular element.
        int aa= Collections.frequency(list2, "mango");
        System.out.println(aa);
       
        //replace all elements of this list with a single object.
        Collections.fill(list2, "newOne");
        System.out.println(list2);
        
       //get last element of list. 
       System.out.println(Collections.max(list));
       
       //get first element of list.
       System.out.println(Collections.min(list));
       
       //replace any element with another value.
       Collections.replaceAll(list, "apple", "grapes");
       System.out.println(list);
       
       //Swap element of list with each other.
       Collections.swap(list, 2, 0);
       System.out.println(list);
      
       //List in reverse order.
       Collections.reverse(list);
       System.out.println(list);
       
       //check if any element is common in both list.
       System.out.println(Collections.disjoint(list2, ll));
       
}

Output:


No comments:

Post a Comment