This document provides an overview of Java collections and common collection interfaces like List, Set, and Map. It describes the core Collection interface and how List, Set, and Map extend it. It also summarizes common implementation classes for each interface like ArrayList, LinkedList, HashSet, TreeSet, HashMap, TreeMap, and LinkedHashMap. The document outlines the purpose of each interface and class and how they can be used to store and retrieve objects in a collection.
3. &
VC
BB
Introduction to java.util package
The java.util package contains the definition of a
number of useful classes providing a broad range of
functionality
Collections Framework belongs to this package. It
consists of interfaces and classes for working with
group of objects.
3
4. &
VC
BB
Collection
Collection is nothing but group of similar data.
It provides us an API to work with data
structures such as trees, lists, sets, maps, arrays
etc.
Collections allow us to add, retrieve update or
delete a set of objects (CRUD operations).
4
5. &
VC
BB
Array vs Collection
Array
o Holds objects of known type.
o Fixed size
Collections
o Generalization of the array concept.
o Set of interfaces defined in Java for storing
object.
o Multiple types of objects.
o Resizable.
5
6. &
VC
BB
Collection Interfaces
Collections are primarily defined through a set
of interfaces.
Supported by a set of classes that implement
the interfaces
6
7. &
VC
BB
Some methods of “Collection” interface -1
o int size()
Returns the number of elements in this collection.
o boolean isEmpty()
Returns true if this collection contains no elements.
o boolean contains(Object element)
Returns true if this collection contains the specified element
o boolean add(Object element)
Ensures that this collection contains the specified element
o Iterator iterator()
Returns an iterator over the elements in this collection.
7
8. &
VC
BB
Iterator (1)
An iterator is an object that allows to traverse
through all the elements of a collection,
regardless of its specific implementation
An iterator is sometimes called a cursor
No need for index
8
9. &
VC
BB
Iterator (2)
Collection collection = ...;
Iterator it= collection.iterator();
while (it.hasNext()) {
Object element = it.next();
// Do something with element
}
9
10. &
VC
BB
List interface
The List interface is an extension of the
Collection interface.
It defines an ordered collection of data and
allows duplicate objects to be added to a list.
The List interface uses an index for ordering
the elements while storing them in a list. List
has methods that allow access to elements
based on their position.
List has three different implementations:
Vector, ArrayList, LinkedList
10
11. &
VC
BB
Common methods of List
Access to elements via indexes, like arrays
add (int, Object), get(int), remove(int), set(int, Object)
Search for elements
indexOf(Object), lastIndexOf(Object)
Specialized
Iterator, call ListIterator
Extraction of sublist
subList(int fromIndex, int toIndex)
11
12. &
VC
BB
Implementation of List
List has three different implementations:
Vector
ArrayList
LinkedList
12
13. &
VC
BB
ArrayList
Implements List interface
Can grow and shrink dynamically.
Unsynchronized.
Values will be stored in the same order as inserted.
Cannot store primitive values such as double
Is best suited for random access without add and
remove operations
13
14. &
VC
BB
Vector
The Vector class is similar to an ArrayList as it
also implements dynamic array
Vector is synchronized; multiple threads
cannot access the vector object concurrently.
Only one thread can access the Vector object at
a specific time.
Values will be stored in the same order as
inserted.
14
15. &
VC
BB
LinkedList
LinkedList uses linked list data structure
internally i.e. it uses nodes.
LinkedList stores values randomly.
LinkedList elements can be accessed
sequentially only.
It consumes lot of memory.
Duplicate values are allowed.
LinkedList is not synchronized.
15
16. &
VC
BB
LinkedList
Common methods:
• addFirst(E obj)
• addLast(E obj)
• getFirst()
• getLast()
• removeFirst()
• removeLast()
Gives better performance on add and remove vs
ArrayList
Gives poorer performance on get and set vs ArrayList
16
17. &
VC
BB
Set interface
Corresponds to the mathematical definition of a
set
The Set interface creates a list of unordered objects.
No duplicates are allowed
Compared to the Collection interface
Interface is identical.
Every constructor must create a collection without
duplicates.
The operation add cannot add an element already in the
set.
The method call set1.equals(set2) works at follows
17
18. &
VC
BB
Set classes
HashSet class implements the Set interface, a
HashSet creates a collection that makes use of a
hash table for data storage
LinkHashSet class creates a list of elements and
maintains the order of elements added to the Set
TreeSet class implements the Set interface and
uses the tree structure for data storage. Objects are
stored in ascending order
18
20. &
VC
BB
Map interface
A map is an object which stores key/value pairs.
Both key and value are objects.
Given a key, you can find its value.
The keys must be unique, but you can have duplicate values.
Some maps allow null keys and null values, others do not.
There are 4 different implementations.
TreeMap.
HashMap.
LinkedHashMap.
HashTable
20
21. &
VC
BB
Some methods
Methods for adding and deleting
put(Object key, Object value)
remove (Object key)
Methods for extraction objects
get (Object key)
Methods to retrieve the keys, the values, and (key,
value) pairs
keySet() // returns its set of keys
values() // returns a Collection of values
entrySet() // returns a set of entry
21
22. &
VC
BB
Implementions of Map Interface
There are 4 different implementations.
TreeMap.
HashMap.
LinkedHashMap.
HashTable
22
23. &
VC
BB
TreeMap
TreeMap can store key/value pairs.
TreeMap uses tree data structure to store the
keys.
The keys are stored in sorted order.
Null keys are not allowed but null values are
allowed.
Different types of keys are not allowed but
different types of values are allowed.
TreeMap is not synchronized.
23
24. &
VC
BB
HashMap
HashMap can store key/value pairs.
HashMap uses hashing mechanism to store the
keys.
The keys will be in unsorted order.
Null keys and null values are allowed.
Different types of keys and values are allowed.
HashMap is not synchronized.
24
25. &
VC
BB
LinkedHashMap
LinkedHashMap can store key/value pairs.
LinkedHashMap uses hashing mechanism to
store the keys.
The keys will be in stored in the same order as
inserted but are unsorted.
Null keys and null values are allowed.
Different types of keys and values are allowed.
LinkedHashMap is not synchronized.
25
26. &
VC
BB
HashTable
HashTable is a legacy class.
HashTable can store key/value pairs.
HashTable uses hashing mechanism to store
the keys.
The keys will be unsorted.
Null keys and null values are not allowed.
Different types of keys and values are allowed.
HashTable is synchronized.
26
27. &
VC
BB
Traversal Interfaces
Collection framework also provides 3 interfaces
to traverse the elements or objects within the
collection:
Enumeration
Iterator
ListIterator
27
28. &
VC
BB
Enumeration
Can be used with only legacy classes(this is
introduced in java 1.0).
Using this interface, we can traverse only in
forward direction.
In this process performance is very slow.
The supported methods are hasMoreElements(),
nextElement().
28
29. &
VC
BB
Iterator
Can be used with all classes of Collection.
It is introduced in java 1.2.
It is better than Enumeration in performance.
It provides more functionalities than
Enumeration.
The supported methods are hasNext(), next(),
remove().
29
30. &
VC
BB
ListIterator
Can only be used with classes which implement
List interface.(Vector, ArrayList and LinkedList).
It is introduced in java 1.2.
The supported methods are hasNext(), next(),
hasPrevious(), previous(), remove(), add().
This interface provides the
best performance and functionality.
We can traverse bidirectionally by using this
interface.
30