X

Play AlgoQuiz

Rate Us :



Share with Friends :

Menu

Collection class :

It is an utility class present in java.util package. It defines several utility methods for collection implemented class objects.


Sorting the elements of a list :

Collections class defines the following methods to sort elements of a List :-


public static void sort(List l) :-

This method can be use to sort according to natural sorting order. In this case it is compulsory that elements should be homogeneous and comparable otherwise we will get ClassCastException. List should not contain null otherwise we will get NullPointerException.


public static void sort(List l , Comparator c) :-

This method can be use to sort elements of a List according to customized sorting order.


Searching the elements of a List :

Collections class defines the following method to search elements of a List :-


public static int binarySearch(List l , Object o) :-

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


public static int binarySearch(List l , Object key , Comparator c) :-

If the list is sorted according to comparator then we have to use this method.


Note :

Internally binarySearch() method uses BinarySearch algorithm. Before calling binarySearch() method it is compulsory for the List to be sorted otherwise we will get unpredictable results.

Successful search return index and unsuccessful search return insertion point.

Insertion point is the location where we can place element in the Sorted List. If the list is sorted according to Comparator then at the time of search also we should pass the same Comparator otherwise we will get unpredictable results.

Example1 :

import java.util.*; class Example { public static void main(String[ ] args) throws Exception { ArrayList l = new ArrayList(); l.add("a"); l.add("l"); l.add("g"); l.add("o"); System.out.println(l); Collections.sort(l); System.out.println(l); System.out.println(Collections.binarySearch(l , "g")); System.out.println(Collections.binarySearch(l , "k")); } } //save as : Example.java //compile as : javac Example.java //run as : java Example

Output :

[a, l, g, o] [a, g, l, o] 1 -3

Example2 :

import java.util.*; class MyComparator implements Comparator { public int compare(Object obj1 , Object obj2) { Integer i1 = (Integer) obj1; Integer i2 = (Integer) obj2; return i2.compareTo(i1); } } class Example { public static void main(String[ ] args) throws Exception { ArrayList l = new ArrayList(); l.add(1); l.add(2); l.add(3); l.add(4); System.out.println(l); Collections.sort(l , new MyComparator()); System.out.println(l); System.out.println(Collections.binarySearch(l , 1 , new MyComparator())); System.out.println(Collections.binarySearch(l , 5 , new MyComparator())); System.out.println(Collections.binarySearch(l , 7)); //Unpredictable Result } } //save as : Example.java //compile as : javac Example.java //run as : java Example

Output :

[1, 2, 3, 4] [4, 3, 2, 1] 3 -1 -5

Note :

For the List containing 'n' elements range of successful search :-

  • Range of successful search : 0 to n-1

  • Range of unsuccessful search : - (n+1) to -1

  • Total range : - (n+1) to n-1

Reversing the elements of a List :

Collections class defines the following reverse method :-

public static void reverse(List l);

Example :

import java.util.*; class Example { public static void main(String[ ] args) throws Exception { ArrayList l = new ArrayList(); l.add(1); l.add(2); l.add(3); l.add(4); System.out.println(l); Collections.reverse(l); System.out.println(l); } } //save as : Example.java //compile as : javac Example.java //run as : java Example

Output :

[1, 2, 3, 4] [4, 3, 2, 1]

Note :

We can use reverse() method to reverse the elements of a List and this method contain List argument.

Collection class defines reverseOrder() method also to return Comparator object for reversing original sorted order and this method contain Comparator argument.

Comparator cc = Collections.reverseOrder(Comparator c);

Arrays class :

It is an utility class present in Util package. It defines several utility methods for Arrays for both primitive Arrays and object type Arrays.


Sorting the elements of Array :

Arrays class define the following methods :-


public static void sort(Primitive[ ] p) :-

This method is use to sort elements of Primitive Array according to natural sorting order.


public static void sort(Object[ ] o) :-

This method is use to sort elements of Object Array according to natural sorting order. In this case it is compulsory that the elements should be homogeneous and comparable otherwise we will get ClassCastException.


public static void sort(Object[ ] a , Comparator c) :-

This method is use to sort elements of Object[ ] according to customized sorting order.


Note :

Primitive Arrays can be sorted only by natural sorting where as object Arrays can be sorted either by natural sorting order or by customized sorting order.

Example :

import java.util.*; class MyComparator implements Comparator { public int compare(Object obj1 , Object obj2) { String s1 = obj1.toString(); String s2 = obj2.toString(); return s2.compareTo(s1); } } class Example { public static void main(String[ ] args) throws Exception { int[ ] a = {30 , 40 , 10 , 20}; System.out.print("Primitive Array before sorting : "); for(int a1 : a) { System.out.print(a1 + " "); } Arrays.sort(a); System.out.println(); System.out.print("Primitive Array after sorting : "); for(int a1 : a) { System.out.print(a1 + " "); } String[ ] s = {"P" , "M" , "D" , "A"}; System.out.println(); System.out.print("Object Array before sorting : "); for(String s1 : s) { System.out.print(s1 + " "); } Arrays.sort(s); System.out.println(); System.out.print("Object Array after sorting : "); for(String s1 : s) { System.out.print(s1 + " "); } Arrays.sort(s , new MyComparator()); System.out.println(); System.out.print("Object Array after sorting by Comparator : "); for(String s1 : s) { System.out.print(s1 + " "); } } } //save as : Example.java //compile as : javac Example.java //run as : java Example

Output :

Primitive Array before sorting : 30 40 10 20 Primitive Array after sorting : 10 20 30 40 Object Array before sorting : P M D A Object Array after sorting : A D M P Object Array after sorting by Comparator : P M D A

Searching the elements of Array :

Arrays class defines the following search methods :-


public static int binarySearch(Primitive() P , Primitive key)
public static int binarySearch(Object() o , Object key)
public static int binarySearch(Object() o , Object key , Comparator c)
Note :

All rules of these binarySearch() method are exactly same as Collections class binarySearch() method.

Example :

import java.util.*; class MyComparator implements Comparator { public int compare(Object obj1 , Object obj2) { String s1 = obj1.toString(); String s2 = obj2.toString(); return s2.compareTo(s1); } } class Example { public static void main(String[ ] args) throws Exception { int[ ] a = {30 , 40 , 10 , 20}; Arrays.sort(a); System.out.println(Arrays.binarySearch(a , 40)); System.out.println(Arrays.binarySearch(a , 5)); String[ ] s = {"P" , "M" , "D" , "A"}; Arrays.sort(s); System.out.println(Arrays.binarySearch(s , "M")); System.out.println(Arrays.binarySearch(s , "N")); Arrays.sort(s , new MyComparator()); System.out.println(Arrays.binarySearch(s , "M" , new MyComparator())); System.out.println(Arrays.binarySearch(s , "N" , new MyComparator())); System.out.println(Arrays.binarySearch(s , "D")); //Unpredictable Result } } //save as : Example.java //compile as : javac Example.java //run as : java Example

Output :

3 -1 2 -4 1 -2 -1

Converting Arrays to List :


public static List asList(Object[ ] o)

By using this method we are not creating an independent List Object but we are just creating List view for the existing Array Object. By using List reference if we perform any operation then the changes will be reflected to the Array reference. Similarly, by using Array reference if we perform any changes then those changes will reflect to the List. By using List reference we can't perform any operation which varies the size (i.e. add & remove) otherwise we will get runtime exception saying UnsupportedOperationException. By using List reference we can perform replacement operation but replacement should be with same type of element only otherwise we will get runtime exception saying ArrayStoreException.

Example :

import java.util.*; class Example { public static void main(String[ ] args) throws Exception { String[ ] s = {"P" , "M" , "D" , "A"}; List l = Arrays.asList(s); System.out.println(l); s[0] = "Q"; System.out.println(l); l.set(1 , "K"); for(String s1 : s) { System.out.println(s1); } //l.add("Algo"); //UnsupportedOperationException //l.remove(l); //UnsupportedOperationException l.set(3 , "S"); //l.set(1 , 10); //ArrayStoreException System.out.println(l); } } //save as : Example.java //compile as : javac Example.java //run as : java Example

Output :

[P, M, D, A] [Q, M, D, A] Q K D A [Q, K, D, S]


X

MiniDoll :

MiniDoll is an application to learn with fun. You can play it to solve some tricky questions related to Java Programming. Either enjoy playing it alone or discuss with your friends to solve it. It is a simple approach to learn by solving questions. This application has different topics of Java and each topic is divided into three phases. Start solving from initial topic to final topic and by the end of every topic you can boost your concept of that topic.