Collections class:

Collections class defines several utility methods for Collection objects like sorting, searching, reversing etc

1. Sorting elements of List:

Collections class defines the following two sort() methods

public static void sort(List l)

to sort based on default natural sorting order.
In this case List should compulsory contains homogenous and Comparable objects otherwise we will get runtime exception saying ClassCastException.
List should not contain null otherwise we will get NullPointerException.

public static void sort(List l, Comparator c)

to sort based on customized sorting order


//Demo program for sorting elements of list according to default natural sorting order.
import java.util.*;
class CollectionsSortDemo{
    public static void main(String[] args){
        ArrayList al = new ArrayList();
        al.add("Z");
        al.add("A");
        al.add("K");
        al.add("N");
        //al.add(new Integer(10)); //ClassCastException
        //al.add(null); //NullPointerException
        System.out.println("Before Sorting : " + al); //[Z, A, K, N]
        Collections.sort(al);
        System.out.println("After Sorting : " + al); //[A, K, N, Z]
    }
}

Output:
Before Sorting : [Z, A, K, N]
After Sorting : [A, K, N, Z]


//Demo program for sorting elements of list according to customized sorting order.
import java.util.*;
class CollectionsSortDemo{
    public static void main(String[] args){
        ArrayList al = new ArrayList();
        al.add("Z");
        al.add("A");
        al.add("K");
        al.add("N");
        System.out.println("Before Sorting : " + al); //[Z, A, K, N]
        Collections.sort(al, new MyComparator());
        System.out.println("After Sorting : " + al); //[Z. L, K, A]
    }
}
class MyComparator implements Comparator{
    public int compare(Object obj1Object obj2){
        String s1 = (String)obj1;
        String s2 = obj2.toString();
        return s2.compareTo(s1);
    }
}

Output:
Before Sorting : [Z, A, K, N]
After Sorting : [Z, N, K, A]


2. Searching elements of List:

Collections class defines the following the binary search methods

public static int binarySearch(List l, Object target)

If the List is sorted according to default natural sorting order, we have to use this method.

public static int binarySearch(List l, Object target, Comparator c)

We have to use this method, if the List is sorted according to customized sorting order.


Conclusions:

1. The above search methods internally use binary search algorithms
2. Successful search returns index.
3. Unsuccessful search returns insertion point. Insertion point is the location where we can place target element in the sorted list.
4. Before calling binarySearch() method compulsory List should be sorted otherwise we will get unpredictable results.
5. if the Lost is sorted according to Comparator, then at the time of search operation also, we have to pass same Comparator object otherwise we will get unpredictable results.


import java.util.*;
class CollectionsSearchDemo{
    public static void main(String[] args){
        ArrayList al = new ArrayList();
        al.add("Z");
        al.add("A");
        al.add("M");
        al.add("K");
        al.add("a");
        System.out.println("Before Sorting : " + al); //[Z, A, M, K, a]
        Collections.sort(al);
        System.out.println("After Sorting : " + al); //[A, K, M, Z, a]
        System.out.println(Collections.binarySearch(al, "Z")); //3
        System.out.println(Collections.binarySearch(al, "J")); //-2
    }
}

Output:
Before Sorting : [Z, A, M, K, a]
After Sorting : [A, K, M, Z, a]
3
-2


import java.util.*;
class CollectionsSearchDemo{
    public static void main(String[] args){
        ArrayList al = new ArrayList();
        al.add(15);
        al.add(0);
        al.add(20);
        al.add(10);
        al.add(5);
        System.out.println("Before Sorting : " + al); //[15, 0, 20, 10, 5]
        Collections.sort(al, new MyComparator());
        System.out.println("After Sorting : " + al); //[20, 15, 10, 5, 0]
        System.out.println(Collections.binarySearch(al, 10new MyComparator())); //2
        System.out.println(Collections.binarySearch(al, 13new MyComparator())); //-3
        System.out.println(Collections.binarySearch(al, 17)); //unpredictable
    }
}
class MyComparator implements Comparator{
    public int compare(Object obj1Object obj2){
        Integer i1 = (Integer)obj1;
        Integer i2 = (Integer)obj2;
        return i2.compareTo(i1);
    }
}

Output:
Before Sorting : [15, 0, 20, 10, 5]
After Sorting : [20, 15, 10, 5, 0]
2
-3
-6

Note:
For the List of n elements in the case of binarySearch() method
Successful search result range 0 to n-1
Unsuccessful search result range -(n+1) to -1
Total result range -(n+1) to n-1

For Example
3 elements [A, K, Z]
-1 -2 -3 -4
A K Z
0 1 2
Successful search result range 0 to 2
Unsuccessful search result range -4 to -1
Total result range -4 to 2


3. Reversing elements of List:

Collections class defines the following reverse methods to reverse elements of List

public static void reverse(List l)
import java.util.*;
class CollectionsReverseDemo{
    public static void main(String[] args){
        ArrayList al = new ArrayList();
        al.add(15);
        al.add(0);
        al.add(20);
        al.add(10);
        al.add(5);
        System.out.println("Before reversing : " + al); //[15, 0, 20, 10, 5]
        Collections.reverse(al);
        System.out.println("After reversing : " + al); //[5, 10, 20, 0, 15]
    }
}

Output:
Before reversing : [15, 0, 20, 10, 5]
After reversing : [5, 10, 20, 0, 15]


reverse() vs reverseOrder()

We can use reverse() method to reverse order of elements of List Whereas we can use reverseOrder() method to get revered Comparator Comparator c1 = Collections.reverseOrder(Comparator c)
c1 --> Descending order
c --> Ascending order


Arrays class:

Arrays class is a utility class to define several utility methods for Array objects.

1. Sorting elements of Array:

Arrays class defines the following sort methods to sort elements of primitive and Object type arrays.

public static void sort(primitive[] p)

to sort according to natural sorting order.

public static void sort(Object[] p)

to sort according to natural sorting order.

public static void sort(Object[] o, Comparator c)

to sort according to Customized sorting order.

import java.util.Arrays;
import java.util.Comparator;
class ArraysSortDemo{
    public static void main(String[] args){
        int[] a = {10520116};
        System.out.println("Primitive Array before sorting");
        for(int a1 : a){
            System.out.println(a1); //10, 5, 20, 11, 6
        }
        Arrays.sort(a);
        System.out.println("Primitive Array after sorting");
        for(int a1 : a){
            System.out.println(a1); //5, 6, 10, 11, 20
        }
        String[] s = {"A""Z""B"};
        System.out.println("Object Array before sorting");
        for(String a2 : s){
            System.out.println(a2); //A, Z, B
        }
        Arrays.sort(s);
        System.out.println("Object Array after sorting");
        for(String a2 : s){
            System.out.println(a2); //A, B, Z
        }
        Arrays.sort(s, new MyComparator());
        System.out.println("Object Array after sorting");
        for(String a1 : s){
            System.out.println(a1); //Z, B, A
        }
    }
}
class MyComparator implements Comparator{
    public int compare(Object obj1Object obj2){
        String s1 = obj1.toString();
        String s2 = obj2.toString();
        return s2.compareTo(s1);
    }
}

Output:
Primitive Array before sorting
10
5
20
11
6
Primitive Array after sorting
5
6
10
11
20
Object Array before sorting
A
Z
B
Object Array after sorting
A
B
Z
Object Array after sorting
Z
B
A
Note: we can sort primitive arrays based on default sorting order where as we can sort object arrays either based on default natural sorting order or customized sorting order.


2. Searching elements of Array:

Arrays class defines the following binarySearch methods.

public static int binarySearch(primitive[] p, primitive target)
public static int binarySearch(Object[] a, Object target)
public static int binarySearch(Object[] a, Object target, Comparator c)

All rules of Arrays class binarySearch methods same as Collections class binarySearch methods.

import java.util.*;
import static java.util.Arrays.*;
class ArraysSearchDemo{
    public static void main(String[] args){
        int[] a = {10520116};
        Arrays.sort(a);
        System.out.println(Arrays.binarySearch(a, 6)); //1
        System.out.println(Arrays.binarySearch(a, 14)); //-5
        String[] s = {"A""Z""B"};
        Arrays.sort(s);
        System.out.println(Arrays.binarySearch(s, "Z")); //2
        System.out.println(Arrays.binarySearch(s, "S")); //-3
        Arrays.sort(s, new MyComparator());
        System.out.println(Arrays.binarySearch(s, "Z"new MyComparator())); //0
        System.out.println(Arrays.binarySearch(s, "S"new MyComparator())); //-2
        System.out.println(Arrays.binarySearch(s, "N")); //unpredictable
    }
}
class MyComparator implements Comparator{
    public int compare(Object obj1Object obj2){
        String s1 = obj1.toString();
        String s2 = obj2.toString();
        return s2.compareTo(s1);
    }
}

Output:
1 -5 2 -3 0 -2 -4


Conversion of Array to List:

public static List asList(Object[] arr)

Strictly speaking this method wont create an independent List Object.
For the existing array we are getting List view
String s = {"A", "Z", "B"};
List l = Arrays.asList(s);
By using Array reference if we perform any change automatically that change will be reflected to the List. Similarly, by using List reference if we perform any change that change will be reflected automatically to the Array.

By using List reference, we can’t perform any operation which varies the size otherwise we will get runtime exception saying UnsupportedOperationException .

l.add("M"); //UnsupportedOperationException
l.remove(1); //UnsupportedOperationException
l.set(1"N");

By using List reference, we are not allowed to replace with heterogenous objects otherwise we will get runtime exception saying ArrayStoreException

l.set(1new Integer(10)); //ArrayStoreException
import java.util.*;
class ArraysAsListDemo{
    public static void main(String[] args){
        String[] s = {"A""Z""B"};
        List l = Arrays.asList(s);
        System.out.println(l); //[A, Z, B]
        s[0] = "K";
        System.out.println(l); //[K, Z, B]
        l.set(1"L");
        for(String s1 : s){
            System.out.println(s1); //[K, L, B]
        }
        //l.add("Kalyan"); //UnsupportedOperationException
        //l.remove(2); //UnsupportedOperationException
        //l.set(1, new Integer(10)); //ArrayStoreException
    }
}

Output:
[A, Z, B]
[K, Z, B]
K
L
B