Collections Framework in Java
Collections Framework in Java
Topperworld.in
Collection freamework
❖ 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.
©Topperworld
Java Programming
©Topperworld
Java Programming
There are many methods declared in the Collection interface. They are as
follows:
©Topperworld
Java Programming
❖ 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()
©Topperworld
Java Programming
❖ Collection Interface
• The Collection Interface is the root or the foundation on which the
Collections Framework is built. It is a general interface that has the
declaration:
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.
©Topperworld
Java Programming
There are three classes implemented by the list interface and they are given
below.
Syntax:
1.ArrayList
©Topperworld
Java Programming
str.add(<content>);
str.add(<content>);
str.add(<content>);
Example:
import java.util.*;
str.add("Topper");
str.add("World");
str.add("Rocks");
©Topperworld
Java Programming
ArrayList<Data-Type> obj =
Example:
import java.util.*;
public class TW {
©Topperworld
Java Programming
2. LinkedList
©Topperworld
Java Programming
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");
©Topperworld
Java Programming
list.remove(5);
list.remove("C#");
Output:
Size at the beginning 0
Vector
©Topperworld
Java Programming
In this method, the size is not specified so the default size of Vectors is 10.
©Topperworld
Java Programming
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);
©Topperworld
Java Programming
Output:
Size at the beginning 0
[19, 88, 1]
4. Stack
©Topperworld
Java Programming
Example:
import java.util.*;
//creating a Stack
//push elements
s.push(99);
s.push(28);
s.push(17);
s.push(74);
s.push(1);
©Topperworld
Java Programming
//the size remains the same as peek does not remove the element
Output:
Popped element 1
Top-most element 74
Queue Interface
©Topperworld
Java Programming
PriorityQueue:
©Topperworld
Java Programming
Example:
import java.util.*;
pq.add(99);
pq.add(18);
pq.add(27);
pq.add(34);
©Topperworld
Java Programming
Output:
Size at the beginning 0
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.
©Topperworld
Java Programming
1.HashSet
©Topperworld
Java Programming
3. Creating a HashSet with a specified size and fill ratio It is used to create a
HashSet with a given size and fill ratio.
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");
©Topperworld
Java Programming
System.out.println(str);
}}
Output:
2.LinkedHashSet
©Topperworld
Java Programming
©Topperworld
Java Programming
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);
©Topperworld
Java Programming
3.SortedSet Interface
4.TreeSet
©Topperworld
Java Programming
Example:
import java.util.*;
class TopperWorld {
// Creating a TreeSet
ts.add("India");
ts.add("USA");
ts.add("Britain");
System.out.println(ts);
©Topperworld
Java Programming
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.
©Topperworld
Java Programming
Example:
import java.util.*;
// Creating a HashMap
hm.put(1, 1.9);
hm.put(2, 2.8);
hm.put(3, 3.7);
©Topperworld