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
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.
to sort based on customized sorting order
Output:
Before Sorting : [Z, A, K, N]
After Sorting : [A, K, N, Z]
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
If the List is sorted according to default natural sorting order, we have to use this method.
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.
Output:
Before Sorting : [Z, A, M, K, a]
After Sorting : [A, K, M, Z, a]
3
-2
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
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.
to sort according to natural sorting order.
to sort according to natural sorting order.
to sort according to Customized sorting order.
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.
All rules of Arrays class binarySearch methods same as Collections class binarySearch methods.
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 .
By using List reference, we are not allowed to replace with heterogenous objects otherwise we will get runtime exception saying ArrayStoreException
Output:
[A, Z, B]
[K, Z, B]
K
L
B
0 Comments