Java Collections class
Syntax:
java.lang.Object
java.util.Collections
How to declare an object?
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.
Collections.addAll(list, fruits, "Apples", "Oranges", "Banana");
fruits.forEach(System.out::println);
How to find the maximum from integer collection?
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?
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.
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.
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.
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.
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.
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.
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.
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.
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.
Collections.addAll(fruitsSubList1, "Oranges", "Banana");
System.out.println(Collections.indexOfSubList(fruits, fruitsSubList1));
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.
- EMPTY_LIST: to get immutable empty List.
- EMPTY_SET: to get immutable empty Set.
- EMPTY_MAP: to get immutable empty Map.
How to declare an object?
- Example: List
list = new ArrayList ();
- Example: list.add("C");
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:
Collections.addAll(list, fruits, "Apples", "Oranges", "Banana");
fruits.forEach(System.out::println);
How to find the maximum from integer collection?
- Example:
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.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:
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.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:
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:
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:
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:
- Example:
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:
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:
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:
Collections.addAll(fruitsSubList1, "Oranges", "Banana");
System.out.println(Collections.indexOfSubList(fruits, fruitsSubList1));
Tags:
Java