Collection Frameworks in Java

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Collection Frameworks in Java

What is Collection in Java


A Collection represents a single unit of objects, i.e., a group.

What is a framework in Java

o It provides readymade architecture.


o It represents a set of classes and interfaces.

What is a Collection Framework in Java


The Java Collections Framework (JCF) is a set of interfaces and classes that
provide a way to work with collections of objects in Java. It provides a lot of
functionality for manipulating and processing collections, making it easier to write
efficient and effective code.
Here are some key points about the Java Collections Framework:
Interfaces: The JCF provides several interfaces that define the behavior of
collections, such as Collection, List, Set, Map, and Queue.
Classes: The JCF provides several classes that implement these interfaces, such as
ArrayList, LinkedList, HashSet, HashMap, and LinkedList.
Methods: The JCF provides a wide range of methods for manipulating and
processing collections, such as add, remove, contains, size, and isEmpty.
Efficient: The JCF is highly optimized for performance, making it efficient to use.
Flexible: The JCF provides a wide range of interfaces and classes, making it
flexible to use.
Easy to use: The JCF is easy to use, with a simple and intuitive API.
Here is a list of some of the key concepts and interfaces in the Java Collections
Framework:

Collection: The root interface of the JCF, which provides a way to work with
collections of objects.
List: A type of collection that allows elements to be stored in a specific order.
Set: A type of collection that does not allow duplicate elements.
Map: A type of collection that stores key-value pairs.
Queue: A type of collection that allows elements to be added and removed in a
specific order.
Here is a list of some of the key classes in the Java Collections Framework:

ArrayList: A resizable array-based implementation of the List interface.


LinkedList: A doubly-linked list implementation of the List interface.
HashSet: A set implementation that uses a hash table to store elements.
HashMap: A map implementation that uses a hash table to store key-value pairs.
Here is a sample code snippet that demonstrates how to use the Java Collections
Framework:

import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Hello");
list.add("World");
System.out.println(list);
}
}

This code creates an ArrayList object and adds two elements to it. It then prints
the contents of the list to the console.
Methods of Collection interface
There are many methods declared in the Collection interface. They are as follows:

No. Method Description

1 public boolean add(E e) It is used to insert an element in this


collection.

2 public boolean It is used to insert the specified


addAll(Collection<? extends E> c) collection elements in the invoking
collection.

3 public boolean remove(Object It is used to delete an element from the


element) collection.

4 public boolean It is used to delete all the elements of


removeAll(Collection<?> c) the specified collection from the
invoking collection.

5 default boolean It is used to delete all the elements of


removeIf(Predicate<? super E> the collection that satisfy the specified
filter) predicate.

6 public boolean It is used to delete all the elements of


retainAll(Collection<?> c) invoking collection except the specified
collection.

7 public int size() It returns the total number of elements


in the collection.

8 public void clear() It removes the total number of


elements from the collection.

9 public boolean contains(Object It is used to search an element.


element)

10 public boolean It is used to search the specified


containsAll(Collection<?> c) 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 empty.

15 default Stream<E> It returns a possibly parallel Stream


parallelStream() 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> It generates a Spliterator over the


spliterator() specified elements in the collection.

18 public boolean equals(Object It matches two collections.


element)

19 public int hashCode() It returns the hash code number of the


collection.

You might also like