What You Need To Know About Collections Framework in Java

Profile image for Sean Brown
Sean Brown Java Developer
Jan 20, 2022 ‧ 4 min read

Java collections framework is an important part of the Java programming language. It provides a set of classes that are used to store and organize data in Java applications. The framework also provides additional functionalities like searching, sorting, and filtering. 

Collections Framework in Java is a set of interfaces and classes that organize the most commonly used data structures in Java, including arrays, collections, iterators, and algorithms. There are many uses of Collections Framework in Java: 

* Collections Framework provides abstractions for working with collections. This can be used to work with data sets larger than the available memory or to improve application performance without needing to keep permanent copies of the data set. 

* Collections default methods provide a means for performing common operations on general iterable types like arrays or collections. These default methods are more efficient than writing custom methods when the need arises. 

* The java. util package included various collection implementations such as LinkedList and Queue. 

 What is a Framework? 

A framework is a theoretical model that helps in the understanding of some concept. It provides an overarching perspective on a given topic and simplifies it to a certain extent. 

Frameworks can be scientific, social, economic or cultural in nature. They can also be epistemological or ontological in nature - they provide us with propositions about the world as we perceive it. 

Some frameworks are descriptive, rather than prescriptive like recipes for cooking. They give us insights into what something means and how to do it without telling us exactly how to do so. 

 Let’s understand this with an example of adding an element in a hashtable and a vector.  

// Java program to demonstrate
// why collection framework was needed
import java.io.*;
import java.util.*;

class CollectionDemo {

	public static void main(String[] args)
	{
		// Creating instances of the array,
		// vector and hashtable
		int arr[] = new int[] { 1, 2, 3, 4 };
		Vector<Integer> v = new Vector();
		Hashtable<Integer, String> h = new Hashtable();

		// Adding the elements into the
		// vector
		v.addElement(1);
		v.addElement(2);

		// Adding the element into the
		// hashtable
		h.put(1, "geeks");
		h.put(2, "4geeks");

		// Array instance creation requires [],
		// while Vector and hastable require ()
		// Vector element insertion requires addElement(),
		// but hashtable element insertion requires put()

		// Accessing the first element of the
		// array, vector and hashtable
		System.out.println(arr[0]);
		System.out.println(v.elementAt(0));
		System.out.println(h.get(1));

		// Array elements are accessed using [],
		// vector elements using elementAt()
		// and hashtable elements using get()
	}
}

Collection Framework is a design pattern that organizes and displays content in a logical order with levels of importance, like an outline. It makes it easier to scan, digest and understand. 

Advantages of the Collection Framework 

-Provides guides for users to find the content they need by using navigation patterns. 

-Shows different levels of importance for content types, 

-Helps users identify what's new and what needs more attention. 

-Easier information access and retrieval when in a hurry. 

Hierarchy of Collection Framework: 

Methods of Collection interface 

01. Public Boolean add(E e) 

It is used to insert an element in this collection. 

02. Public Boolean addAll(Collection<? extends E> c) 

It is used to insert the specified collection elements in the invoking collection. 

03. Public Boolean remove(Object element) 

It is used to delete an element from the collection 

04. Public Boolean removeAll(Collection<?> c) 

 It is used to delete all the elements of the specified collection from the invoking collection. 

05. Default Boolean removeIf(Predicate<? super E> filter)  

It is used to delete all the elements of the collection that satisfy the specified predicate. 

06. Public Boolean retainAll(Collection<?> c) 

 It is used to delete all the elements of invoking collection except the specified collection. 

07. Public int size() 

 It returns the total number of elements in the collection. 

08. Public void clear()  

It removes the total number of elements from the collection. 

09. Public Boolean contains(Object element) 

It is used to search an element. 

10. Public Boolean containsAll(Collection<?> c) 

It is used to search the specified collection in the collection. 

11. Public Iterator iterator() 

It returns an iterator. 

12. Public Object[] toArray()  

It converts collection into array. 

13. Public <T> T[] toArray(T[] a) 

It converts collection into array. Here, the runtime type of the returned array is that of the specified array. 

14. Public Boolean isEmpty() 

It checks if collection is esource. 

15. Default Stream<E> parallelStream() 

It returns a possibly parallel Stream with the collection as its source. 

16. Default Stream<E> stream() 

It returns a sequential Stream with the collection as its source. 

17. Default Spliterator<E> spliterator() 

It generates a Spliterator over the specified elements in the collection. 

18. Public Boolean equals(Object element) 

It matches two collections.19public int hashCode()It returns the hash code number of the collection. 

Posted on Jan 20, 2022 by:
Profile image for Sean Brown
Sean Brown
Java Developer
AngularJavaAWS

Comments

Profile image for Sean Brown

Java Developer

AngularJavaAWS
1K
Reputation
114
Following
120
Followers