0% found this document useful (0 votes)
2 views10 pages

Collections Framework Java

The document provides an overview of the Java Collections Framework, detailing the Collections class, its methods, and the core interfaces such as Collection, List, Set, and Queue. It explains the internal workings of HashSet and HashMap, including their storage mechanisms and performance characteristics. Additionally, it covers the PriorityQueue class and its behavior in terms of element prioritization and dynamic sizing.

Uploaded by

Dipali Gangarde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views10 pages

Collections Framework Java

The document provides an overview of the Java Collections Framework, detailing the Collections class, its methods, and the core interfaces such as Collection, List, Set, and Queue. It explains the internal workings of HashSet and HashMap, including their storage mechanisms and performance characteristics. Additionally, it covers the PriorityQueue class and its behavior in terms of element prioritization and dynamic sizing.

Uploaded by

Dipali Gangarde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

COLLECTIONS FRAMEWORK

Collections class in Java is one of the utility classes in Java Collections Framework.
The java.util package contains the Collections class in Java. Java Collections class is used with
the static methods that operate on the collections or return the collection. All the methods
of this class throw the NullPointerException if the collection or object passed to the
methods is null.
Java Collections Example
Examples of Collections Classes in Java are mentioned below:
• Adding Elements to the Collections → Collections.addAll(l, "Fruits", "Bat", "Ball");
• Sorting a Collection→ Collections.sort(l);
• Searching in a Collection → Collections.binarySearch(l, "Dog")
• Copying Elements → Collections.copy(l1, l2);
• Disjoint Collection → It returns true if the two collections do not have any element
in common.
Collections.disjoint(l1, l2));

The Collection interface in Java is a core member of the Java Collections


Framework located in the java.util package. It is one of the root interfaces of
the Java Collection Hierarchy. The Collection interface is not directly implemented by
any class. Instead, it is implemented indirectly through its sub-interfaces like List, Queue,
and Set.
For Example, the ArrayList class implements the List interface, a sub-interface of the
Collection interface.
Any group of individual objects that are represented as a single unit is known as a Java
Collection of Objects. In Java, a separate framework named the “Collection Framework” has
been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it.
In Java, the Collection interface (java.util.Collection) and Map interface (java.util.Map) are
the two main “root” interfaces of Java collection classes.

What is a Framework in Java?


→A framework is a set of classes and interfaces which provide a ready-made architecture. In
order to implement a new feature or a class, there is no need to define a framework.
However, an optimal object-oriented design always includes a framework with a collection of
classes such that all the classes perform the same kind of task.

In above fig, interface List(as it cannot create object on it’s own) , set has created a
references of the class arraylist, hashset, linkedlist, etc. to create object

LIST VS SET →
ARRAYLIST –

LINKED LIST –

ArrayList vs LinkedList →
• Using ListIterator, we can traverse the list in both forward and backward directions.

METHODS Of LIST INTERFACE -

Method

add(int index, element)

addAll(int index,
Collection collection)

size()

clear()

remove(int index)

remove(element)
Method

get(int index)

set(int index, element)

indexOf(element)

lastIndexOf(element)

equals(element)

hashCode()

isEmpty()

contains(element)

containsAll(Collection
collection)

sort(Comparator comp)

Internal Working of a HashSet


All the classes of the Set interface are internally backed up by Map. HashSet uses HashMap
for storing its object internally. You must be wondering that to enter a value in HashMap we
need a key-value pair, but in HashSet, we are passing only one value.
Storage in HashMap: Actually the value we insert in HashSet acts as a key to the map Object
and for its value, java uses a constant variable. So in the key-value pair, all the values will be
the same.
The Queue interface provides several methods for adding, removing, and inspecting
elements in the queue. Here are some of the most commonly used methods:
• add(element): Adds an element to the rear of the queue. If the queue is full, it
throws an exception.
• offer(element): Adds an element to the rear of the queue. If the queue is full, it
returns false.
• remove(): Removes and returns the element at the front of the queue. If the queue
is empty, it throws an exception.
• poll(): Removes and returns the element at the front of the queue. If the queue is
empty, it returns null.
• element(): Returns the element at the front of the queue without removing it. If the
queue is empty, it throws an exception.
• peek(): Returns the element at the front of the queue without removing it. If the
queue is empty, it returns null.

PriorityQueue in Java
The PriorityQueue class in Java is part of the java.util package. It is known that
a Queue follows the FIFO(First-In-First-Out) Algorithm, but the elements of the Queue are
needed to be processed according to the priority, that’s when the PriorityQueue comes into
play.
• The PriorityQueue is based on the Priority Heap.
• The elements of the priority queue are ordered according to the natural ordering, or
by a Comparator provided at queue construction time, depending on which
constructor is used.
• No Null Elements are available in PriorityQueue it will throw NullPointerException in
such a case.
• Size of the Priority Queue is dynamic , means will increase or decrease as required.
• In the below priority queue, an element with a maximum ASCII value will have the
highest priority.

HashMap in Java
In Java, HashMap is part of the Java Collections Framework and is found in
the java.util package. It provides the basic implementation of the Map interface in
Java. HashMap stores data in (key, value) pairs. Each key is associated with a value, and you
can access the value by using the corresponding key.
• Internally uses Hashing (similar to Hashtable in Java).
• Not synchronized (unlike Hashtable in Java) and hence faster for most of the cases.
• Allows to store the null keys as well, but there should be only one null key object,
and there can be any number of null values.
• Duplicate keys are not allowed in HashMap, if you try to insert the duplicate key, it
will replace the existing value of the corresponding key.
• HashMap uses keys in the same way as an Array uses an index.
• HashMap allows for efficient key-based retrieval, insertion, and removal with an
average O(1) time complexity.

You might also like