Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Java Collections

The document provides an overview of the Java Collections Framework, which includes various interfaces and classes for storing and manipulating groups of objects. It explains key components such as the Collection interface, List interface, Set interface, and Map interface, along with their methods and implementations like ArrayList, HashSet, and HashMap. The document also highlights the functionalities of these collections, including operations like adding, removing, and searching for elements.

Uploaded by

galenightin58
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java Collections

The document provides an overview of the Java Collections Framework, which includes various interfaces and classes for storing and manipulating groups of objects. It explains key components such as the Collection interface, List interface, Set interface, and Map interface, along with their methods and implementations like ArrayList, HashSet, and HashMap. The document also highlights the functionalities of these collections, including operations like adding, removing, and searching for elements.

Uploaded by

galenightin58
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Collections in

Java
Collections in Java
• The Collections in Java is a framework that provides an architecture to store and
manipulate the group of objects

• Java Collections can achieve all the operations that you perform on a data such
as searching, sorting, insertion, manipulation, and deletion

• Java Collections means a single unit of objects

• Java Collection framework provides many interfaces (e.g., Set, List, Queue,
Deque) and classes (e.g., ArrayList, LinkedList, PriorityQueue, HashSet,
LinkedHashSet, TreeSet)
What is a framework in Java
• It provides readymade architecture

• It represents a set of classes and interfaces

• It is optional
What is a Collection framework?
The Collection framework represents a unified architecture for storing and
manipulating a group of objects.

It has:
• Interfaces and its implementations, i.e., classes
• Algorithm
Iterable Interface
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()
Collection
Interface
Collection Interface
• The Collection interface is the interface which is implemented by all the classes
in the collection framework.

• It declares the method that every collection will have

• In other words, we can say that the Collection interface builds the foundation on
which the collection framework depends
Methods of Collection Interface
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 collection
addAll(Collection<? extends elements in the invoking collection.
E> c)
3 public boolean It is used to delete an element from the
remove(Object element) collection.
4 public boolean It is used to delete all the elements of the
removeAll(Collection<?> c) specified collection from the invoking
collection.
5 public int size() It returns the total number of elements in the
collection.
Methods of Collection Interface
No. Method Description

6 public void clear() It removes the total number of elements


from the collection.
7 public boolean It is used to search an element.
contains(Object element)
8 public boolean It is used to search the specified collection in
containsAll(Collection<?> the collection.
c)
9 public Iterator iterator() It returns an iterator.

10 public boolean isEmpty() It checks if collection is empty.


Methods of Iterator Interface
No. Method Description

1 public boolean hasNext() It returns true if the iterator has more


elements otherwise it returns false.
2 public Object next() It returns the element and moves the cursor
pointer to the next element.
3 public void remove() It removes the last elements returned by the
iterator. It is less used.
List Interface
List Interface
• List interface is the child interface of Collection interface

• It inhibits a list type data structure in which we can store the ordered collection
of objects

• It can have duplicate values

• List interface is implemented by the classes ArrayList, LinkedList, Vector, and


Stack

• To instantiate the List interface, we must use:


ArrayList
• The ArrayList class implements the List interface

• It uses dynamic array to store the duplicate element of different data types

• The ArrayList class maintains the insertion order and is non-synchronized

• The elements stored in the ArrayList class can be randomly accessed


LinkedList
• LinkedList implements the Collection Interface

• It uses a doubly linked list internally to store the elements

• It can store the duplicate elements

• It maintains the insertion order and is not synchronized

• In LinkedList, the manipulation is fast because no shifting is required


Set Interface
Set Interface
• Set interface in Java is present in java.util package

• It extends the Collection Interface

• It represents the unordered set of elements which doesn’t allow us to store


duplicate elements

• We can store at most one null value in Set

• Set is implemented by HashSet, LinkedHashSet, and TreeSet.

• Set can be instantiated as:


HashSet
• HashSet class implements Set interface

• It represents the collection that uses a hash table for storage

• Hashing is used to store the elements in the HashSet

• It contains unique items


LinkedHashSet
• LinkedHashSet class represents the LinkedList implementation of Set interface

• It extends the HashSet class and implements the Set interface

• Like HashSet, it also contains unique elements

• It maintains the insertion order and permits null elements


TreeSet
• Java TreeSet class implements the Set interface that uses tree for storage

• Like HashSet, TreeSet also contains unique elements

• However, the access and retrieval time of TreeSet is quite fast

• The elements in TreeSet stored in ascending order


Map Interface
Java Map Interface
• A map contains values based on key i.e., key and value pair

• Each key and value is known as entry

• A Map contains unique keys

• A Map is useful if you must search, update, or delete elements based on a key
Java Map Hierarchy
• There are two interfaces for
implementing Map in Java: Map and
SortedMap, and three classes:
HashMap, LinkedHashMap and
TreeMap

• A Map doesn’t allow duplicate keys,


but you can have duplicate values

• HashMap and LinkedHashMap allow


null keys and values, but TreeMap
doesn’t allow any null key or value

• A Map can’t be traversed, so you need


to convert it into Set using keySet() or
entrySet() method
HashMap / LinkedHashMap / TreeMap
HashMap
• HashMap is the implementation of Map, but it doesn’t maintain any order

LinkedHashMap
• LinkedHashMap is the implementation of Map
• It inherits HashMap class
• It maintains insertion order

TreeMap
• TreeMap is the implementation of Map
• It maintains ascending order
Useful methods of Map interface
No. Method Description

1 V put(Object key, Object It is used to insert an entry in the map.


value)
2 void putAll(Map map) It is used to insert the specified map in the
map.
3 V remove(Object key) It is used to delete an entry for the specified
key.
4 Set keySet() It returns the Set view containing all the
keys.
5 Set<Map.Entry<K,V>> It returns the Set view containing all the keys
entrySet() and values.
Useful methods of Map interface
No. Method Description

6 void clear() It is used to reset the map.

7 boolean This method returns true if some key equal to


containsKey(Object key) the key exists within the map, else return
false.
8 boolean This method returns true if some value equal
containsValue(Object to the value exists within the map, else
value) return false.
9 V get(Object key) This method returns the object that contains
the value associated with the key.
10 V getOrDefault(Object key, It returns the value to which the specified key
V defaultValue) is mapped, or defaultValue if the map
contains no mapping for the key.
Useful methods of Map interface
No. Method Description

11 boolean isEmpty() This method returns true if the map is empty;


returns false if it contains at least one key.
12 int size() This method returns the number of entries in
the map.
Thank
You!

You might also like