coderz.py

Keep Coding Keep Cheering!

Collections Class in Java

java thread class

The Collections class belongs to the Java Collections Framework. The package java.util.Collections is the package containing the Collections class. The Collections class mainly uses static methods that operate on or return collections. If the collection or object supplied to the methods is null, all the methods in this class throw the NullPointerException.

Declaration:
public class Collections extends Object
Collections Methods:
MethodDescription
addAll()It adds all of the elements supplied to the specified collection.
binarySearch()It looks through the list for the provided item and returns its position in a sorted list.
copy()It replicates every element from one list to another.
disjoint()If the two provided collections have no elements in common, it returns true.
emptyEnumeration()It returns an enumeration with no elements.
emptyIterator()It returns an Iterator that has no elements.
emptyList()It returns a List that has no elements.
emptyListIterator()It returns a List Iterator that has no elements.
emptyMap()It returns an empty map that is immutable.
emptyNavigableMap()It returns an empty navigable map that is immutable.
emptyNavigableSet()It gives back an empty navigable set that is immutable.
emptySet()It returns the set that has no elements.
emptySortedMap()It returns an immutable empty sorted map.
emptySortedSet()It is used to obtain the sorted set with no elements.
enumeration()It returns the enumeration of the supplied collection.
fill()It is used to replace all the supplied list’s elements with the given elements.
list()It is used to obtain an array list containing the elements returned by the specified enumeration in the order that the enumeration returns them.
max()It is used to obtain the greatest possible value from the provided collection.
min()It is used to find the smallest value in a given collection.
nCopies()It returns an immutable list containing n copies of the provided object.
replaceAll()It is used to replace all occurrences of one item in a list with the other value.
reverse()It is used to reverse the order of the entries in the provided list.
reverseOrder()It is used to obtain the comparator that reverses the normal ordering of a collection.
rotate()It is used to rotate the elements of the supplied list by a certain amount.
shuffle()It reorders the provided list elements at random using the default randomness.
sort()It is used to sort the elements in the supplied collection list in ascending order.
swap()It is used to swap the entries in the supplied list at the defined places.
synchronizedCollection()It returns a synchronized (thread-safe) collection that is backed by the provided collection.
synchronizedList()It is used to get a synchronized (thread-safe) collection backed by the specified list.
synchronizedMap()It is used to get a synchronized (thread-safe) map backed by the specified map.
synchronizedNavigableMap()It is used to get a synchronized (thread-safe) navigable map backed by the specified navigable map.
synchronizedNavigableSet()It is used to get a synchronized (thread-safe) navigable set backed by the specified navigable set.
synchronizedSet()It is used to get a synchronized (thread-safe) set backed by the specified set.
synchronizedSortedMap()It is used to get a synchronized (thread-safe) sorted map backed by the specified sorted map.
synchronizedSortedSet()It is used to get a synchronized (thread-safe) sorted set backed by the specified sorted set.
Example: Adding elements 

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

// Main class
class Coderz {

	// Main driver method
	public static void main(String[] args)
	{
		// Creating a list
		// Declaring object of int type
		List<Integer> items = new ArrayList<>();

		// Adding elements (items) to the list
		items.add(10);
		items.add(20);

		// Add one or more elements
		Collections.addAll(items, 30, 100, 999);
        System.out.println(items);
        
		}
	
}
Output:
[10, 20, 30, 100, 999]
Example: Finding min and max elements

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

// Main class
class Coderz {

	// Main driver method
	public static void main(String[] args)
	{
		// Creating a list
		// Declaring object of int type
		List<Integer> items = new ArrayList<>();

		// Adding elements (items) to the list
		items.add(10);
		items.add(20);

		// Add one or more elements
		Collections.addAll(items, 30, 100, 999);
        System.out.println(items);
        // Find min element
        int min = Collections.min(items);
        // Find max element
        int max = Collections.max(items);
        // Displaying data
        System.out.println("Minimum element : "+ min);
        System.out.println("Maximum element : "+ max);
		}
	
}
Output:
[10, 20, 30, 100, 999]
Minimum element : 10
Maximum element : 999
Example: Sorting List

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

// Main class
class Coderz {

	// Main driver method
	public static void main(String[] args)
	{
		// Creating a list
		// Declaring object of int type
		List<Integer> items = new ArrayList<>();

		// Adding elements (items) to the list
		items.add(10);
		items.add(990);

		// Add one or more elements
		Collections.addAll(items, 3, 4069, 999);
		
        System.out.println("ArrayList= "+items);
           // Sorting according to default ordering
        // using sort() method
        Collections.sort(items);
         System.out.println("Sorted ArrayList= "+items);
        
		}
	
}
Output:
ArrayList= [10, 990, 3, 4069, 999]
Sorted ArrayList= [3, 10, 990, 999, 4069]
Example: Searching in a Collection

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

// Main class
class Coderz {

	// Main driver method
	public static void main(String[] args)
	{
		// Creating a list
		// Declaring object of int type
	List<String> items = new ArrayList<>();
 
        // Adding elements to object
        // using add() method
        items.add("C");
        items.add("JAVA");
        items.add("C++");
        items.add("DBMS");
        items.add("OS");
 
        // Sort the List
        Collections.sort(items);
 
        // BinarySearch on the List
        System.out.println(
            "The index of JAVA is "
            + Collections.binarySearch(items, "JAVA"));
 
        // BinarySearch on the List
        System.out.println(
            "The index of DBMS is "
            + Collections.binarySearch(items, "DBMS"));
        
		}
	
}
Output:
The index of JAVA is 3The index of DBMS is 2
Example: Swapping Elements

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

// Main class
class Coderz {

	// Main driver method
	public static void main(String[] args)
	{
		// Creating a list
		// Declaring object of int type
	List<Integer> items = new ArrayList<>();
 
        // Adding elements to object
        // using add() method
        items.add(10);
        items.add(89);
        items.add(300);
        items.add(12);
        items.add(45);
       System.out.println("List before swapping : "+ items);
       Collections.swap(items,0,4); 
        System.out.println("List after swapping : "+ items);
        
		}
	
}
Output:
List before swapping : [10, 89, 300, 12, 45]
List after swapping : [45, 89, 300, 12, 10]

Note: also read about the Hashtable class in Java Collection Framework

Follow Me

If you like my post, please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Leave a Comment

Your email address will not be published. Required fields are marked *

Advertisement