The perfect place for easy learning...

Java Programming

×

Topics List


Java Collection Interface





The Collection interface is the root interface for most of the interfaces and classes of collection framework. The Collection interface is available inside the java.util package. It defines the methods that are commonly used by almost all the collections.

The Collection interface defines the following methods.

Collection interface in java

🔔 The Collection interface extends Iterable interface.

Let's consider an example program on ArrayList to illustrate the methods of Collection interface.

Example
import java.util.*;

public class CollectionInterfaceExample {
	
	public static void main(String[] args) {

		List list_1 = new ArrayList();
		List<String> list_2 = new ArrayList<String>();
		
		list_1.add(10);
		list_1.add(20);
		list_2.add("BTech");
		list_2.add("Smart");
		list_2.add("Class");
		
		list_1.addAll(list_2);		
		System.out.println("Elements of list_1: " + list_1);
		
		System.out.println("Search for BTech: " + list_1.contains("BTech"));		
		System.out.println("Search for list_2 in list_1: " + list_1.containsAll(list_2));		
		
		System.out.println("Check whether list_1 and list_2 are equal: " + list_1.equals(list_2));	
		
		System.out.println("Check is list_1 empty: " + list_1.isEmpty());
		
		System.out.println("Size of list_1: " + list_1.size());
		
		System.out.println("Hashcode of list_1: " + list_1.hashCode());
		
		list_1.remove(0);
		System.out.println(list_1);
		
		list_1.retainAll(list_2);
		System.out.println(list_1);
		
		list_1.removeAll(list_2);
		System.out.println(list_1);
		
		list_2.clear();
		System.out.println(list_2);
	}
}

When we run this code, it produce the following output.

Collection interface in java