Java Collections class

Java Collections class
Syntax:
  java.lang.Object
    java.util.Collections
  • The java.util.Collections class directly extends the Object class.
  • It is a member of Java Collections Framework.
  • Java collection class is used exclusively with static methods that operate on or return collections. 
  • Java Collection class throws a NullPointerException if the collections or class objects provided to them are null.
Fields:
  • EMPTY_LIST:  to get immutable empty List.
  • EMPTY_SET: to get immutable empty Set.
  • EMPTY_MAP: to get immutable empty Map.
Methods

How to declare an object?
  • Example: List list = new ArrayList();  
How to add element in Collection?
  • Example: list.add("C");  
How to add one list to another collection?
boolean addAll(Collection c, T... elements): This method adds all of the provided elements to the specified collection at once. The elements can be provided as a comma-separated list.
  • Example:
List fruits = new ArrayList();
Collections.addAll(list, fruits, "Apples", "Oranges", "Banana");
fruits.forEach(System.out::println);

How to find the maximum from integer collection?
  • Example:
List list = new ArrayList();  
        list.add(46);
        list.add(67);
        list.add(24); 
System.out.println("Maximum: "+Collections.max(list));

How to find minimum from integer collection?
  • Example:
List list = new ArrayList();  
        list.add(46);
        list.add(67);
        list.add(24);
System.out.println("Minimum: "+Collections.min(list));

How to sort elements in Collection?
void sort(List list, Comparator c): This method sorts the provided list according to the natural ordering. We can also pass in s Comparator, if we want some custom ordering.
  • Example:
Collections.sort(fruits);
System.out.println("Sorted according to natural ordering:");
fruits.forEach(System.out::println);
Collections.sort(fruits, Comparator.reverseOrder());
System.out.println("Sorted according to reverse of natural ordering:");
fruits.forEach(System.out::println);

How to implement queue?
Queue asLifoQueue(Deque deque): This method returns a view of Deque as a Last-In-First-Out (LIFO) Queue.
The methods add and remove are mapped to push, pop respectively and so on. This can be useful when we would like to use a method requiring a Queue but we need Lifo ordering.
  • Example:
Deque deque = new LinkedList();
deque.addFirst("Apples");
deque.add("Oranges");
deque.addLast("Bananas");
Queue queue = Collections.asLifoQueue(deque);
System.out.println(queue.poll());
System.out.println(queue.poll());
System.out.println(queue.poll());

How to implement binary search?

int binarySearch(List list, T key): This method searches the key using binary search in the specified list. The list should be sorted by natural ordering, before calling this method, otherwise, the result will be undefined.
  • Example:
Collections.sort(fruits);
System.out.println(Collections.binarySearch(fruits, "Banana"));
System.out.println(Collections.binarySearch(fruits, "Grapes"));

How to check collection with type?
Collection checkedCollection(Collection c, Class type): This method provides a dynamically typesafe view of the provided collection. It is useful to keep an eye on the collection, that any wrongly typed element is not inserted in it.
  • Example:
List list = new ArrayList();
Collections.addAll(list, "one", "two", "three", "four");
Collection checkedList = Collections.checkedCollection(list, String.class);
System.out.println("Checked list content: " + checkedList);
//we can add any type of element to list
list.add(10);
//we cannot add any type of elements to chkList, doing so
//throws ClassCastException
checkedList.add(10);

How to copy collection?
void copy(List dest, List src): This method copies all of the elements from source list to destination list. After this operation is performed, the index of each copied element in the destination list will be identical to its index in the source list.
  • Example:
Collections.copy(list, fruits);
list.forEach(System.out::println);

How to disjoint collection?
boolean disjoint(Collection c1, Collection c2): This method returns true if the two specified collections have no elements in common.
  • Example:
System.out.println(Collections.disjoint(list, fruits));
  • Example:
List vegetables = new ArrayList();
Collections.addAll(vegetables, "Potato", "Cabbage");
System.out.println(Collections.disjoint(vegetables, fruits));

How to fill collection?
void fill(List list, T obj): This method replaces all of the elements of the specified list with the specified element.
  • Example:
Collections.fill(list, "filled with dummy data");
list.forEach(System.out::println);

How to find frequency?
int frequency(Collection c, Object o): This method returns the number of elements in the specified collection which are equal to the specified object.
  • Example:
System.out.println(Collections.frequency(list, "filled with dummy data"));

How to find sub list?
int indexOfSubList(List source, List target): This method returns the starting position of the first occurrence of the specified target list within the specified source list, or -1 if there is no such occurrence.
  • Example:
List fruitsSubList1 = new ArrayList();
Collections.addAll(fruitsSubList1, "Oranges", "Banana");
System.out.println(Collections.indexOfSubList(fruits, fruitsSubList1));

Thanks a lot for query or your valuable suggestions related to the topic.

Previous Post Next Post

Contact Form