Collections Framework in Java
Collections Framework in Java
❖ framework in Java
• It provides readymade architecture.
• It represents a set of classes and interfaces.
• It is optional.
❖ Collections Framework
• The Collections Framework is defined as a unified architecture for
representing and manipulating collections.
• In Java, the Collections Framework is a hierarchy of interfaces and
classes that provides easy management of a group of objects.
• The java.util package contains the powerful tool of Collections
Framework.
• It was defined in JDK 1.2 version which is one of the most used
frameworks to date.
• It provides a ready-made architecture for interfaces and classes and is
used for storing and manipulating a group of objects.
• All collections frameworks contain interfaces, classes, and algorithms.
❖ Hierarchy of Collection Framework:
There are many methods declared in the Collection interface. They are as
follows:
❖ Iterator interface
• Iterator interface provides the facility of iterating the elements in a
forward direction only.
There are only three methods in the Iterator interface. They are:
❖ Iterable Interface
• The Iterable interface is the root interface for all the collection classes.
The Collection interface extends the Iterable interface and therefore all
the subclasses of Collection interface also implement the Iterable
interface.
• It contains only one abstract method. i.e.,
Iterator<T> iterator()
interface Collection<E>
❖ List Interface
• The list interface extends the collection interface.
• A list is used to store ordered collection of data and it may contain
duplicates. Ordered collection means the order in which the elements
are being inserted and they contain a specific value.
• The elements present, can be accessed or inserted by their position in
the list using zero-based indexing.
• The list interface is implemented by LinkedList, ArrayList, Vectors and
Stack classes. They are an important part of collections in Java.
There are three classes implemented by the list interface and they are given
below.
Syntax:
1.ArrayList
str.add(<content>);
str.add(<content>);
str.add(<content>);
Example:
import java.util.*;
str.add("Topper");
str.add("World");
str.add("Rocks");
ArrayList<Data-Type> obj =
Example:
import java.util.*;
public class TW {
There are two ways in which we can create a linked list using constructors.
It will create a Linked List with all the elements of Collection C. We will use
the LinkedList class and new keyword to create a constructor which contains
the elements of collection.
//creating a LinkedList
//add elements
list.add("Java");
list.add("C++");
list.add("JavaScript");
list.addFirst("C#");
list.addLast("Kotlin");
list.add(2,"Python");
list.remove(5);
list.remove("C#");
Output:
Size at the beginning 0
Vector
In this method, the size is not specified so the default size of Vectors is 10.
Example:
import java.util.*;
//creating a Vector
//add elements
v.add(19);
v.add(88);
v.add(1);
v.add(39);
System.out.println(v);
v.remove(3);
System.out.println(v);
}
Output:
Size at the beginning 0
[19, 88, 1]
4. Stack
Example:
import java.util.*;
//creating a Stack
//push elements
s.push(99);
s.push(28);
s.push(17);
s.push(74);
s.push(1);
//the size remains the same as peek does not remove the element
Output:
Popped element 1
Top-most element 74
Queue Interface
Example:
import java.util.*;
pq.add(99);
pq.add(18);
pq.add(27);
pq.add(34);
Top-most element 18
Removing 18
❖ Set Interface
• The Set interface defines an unordered collection.
• It extends the Collection Interface.
• We cannot store duplicate values in this.
• The Set Interface is implemented by popular classes like HashedSet,
LinkedHashSet, and TreeSet.
Example:
import java.util.*;
//creating a HashSet
//add elements
str.add("Hello");
str.add("Hi");
str.add("Namaste");
str.add("Bonjour");
System.out.println(str);
str.remove("Bonjour");
//display the new HashSet
System.out.println(str);
}}
Output:
2.LinkedHashSet
Example:
import java.util.*;
//creating a HashSet
//add elements
str.add("Hello");
str.add("Hi");
str.add("Namaste");
str.add("Bonjour");
System.out.println(str);
str.remove("Bonjour");
System.out.println(str);
}
Output: Size at the beginning 0
3.SortedSet Interface
4.TreeSet
Example:
import java.util.*;
class TopperWorld {
// Creating a TreeSet
ts.add("India");
ts.add("USA");
ts.add("Britain");
System.out.println(ts);
ts.add("Britain");
System.out.println(ts);
}}
Output:
❖ Map Interface
• A map is an object that stores key and value pairs.
• It contains unique keys as the same key cannot have multiple
mappings.
• Although a part of the Collections Framework, maps are not themselves
collections because they do not implement the Collection Interface.
❖ HashMap
• The HashMap class extends AbstractMap and implements the Map
interface.
• It uses a hash table for storing key-value pairs.
• If we want to access a value in a** hash map**, we must know its key.
Example:
import java.util.*;
// Creating a HashMap
hm.put(1, 1.9);
hm.put(2, 2.8);
hm.put(3, 3.7);