Chapter 4 Java Collections
Chapter 4 Java Collections
Java Collections
Collections Frameworks
•A collection is an object that represents a
group of objects
Compiled by Tsegay M. 2
The components of Java Collections Framework
from the user’s perspective
• Collection Interfaces: form the basis of the
framework
- such as sets, lists and maps
HashMap, TreeMap
Compiled by Tsegay M. 4
Java
Collections
Object
(java.lang)
Collection Map
AbstractCollection AbstractMap
List Set
Dictionary
AbstractList AbstractSet
Map
HashMap
AbstractSequentialList Hashtable
HashSet
LinkedHashMap
ArrayList
SortedMap
LinkedHashSet SortedSet
LinkedList
TreeMap
Vector TreeSet
Stack
Compiled by Tsegay M. 5
Interface: Collection
• The Collection interface public interface Collection {
is the root of the // Basic Operations
collection hierarchy int size();
boolean isEmpty();
boolean contains(Object element);
• Some Collection boolean add(Object element);
implementations allow boolean remove(Object element);
- duplicate elements and Iterator iterator();
others do not
- the elements to be // Bulk Operations
ordered or not boolean containsAll(Collection c);
boolean addAll(Collection c);
boolean removeAll(Collection c);
• JDK doesn't provide any boolean retainAll(Collection c);
direct implementations of void clear();
this interface
- It provides implementations // Array Operations
of more specific sub Object[] toArray();
interfaces like Set and Object[] toArray(Object a[]);
List }
Compiled by Tsegay M. 6
Compiled by Tsegay M. 7
Interface: Set public interface Set {
// Basic Operations
• A Set is a collection int size();
boolean isEmpty();
that cannot contain boolean contains(Object
duplicate elements element);
boolean add(Object element);
represent a set
• HashSet is a good choice for AbstractCollection
public methods
• HashSet class TestJavaCollection7{
are not synchronized
public static void main(String
args[]){
//Creating HashSet and adding HashSet
elements
HashSet<String> set=new
HashSet<String>(); LinkedHashSet SortedSet
set.add("Ravi");
set.add("Vijay");
set.add("Ravi"); TreeSet
set.add("Ajay");
//Traversing elements
Iterator<String>
itr=set.iterator();
while(itr.hasNext()){
System.out.println(itr.next());Compiled by Tsegay M. 9
} } }
Java Sets: LinkedHashSet
Collection
• Hash table and linked list
implementation of the Set
interface AbstractCollection
• LinkedHashSet differs from
Set
HashSet in that the order is
maintained
• Performance is below that of AbstractSet
LinkedHashSet<String> set=new
LinkedHashSet<String>();
set.add("Ravi"); LinkedHashSet SortedSet
set.add("Vijay");
set.add("Ravi");
set.add("Ajay"); TreeSet
Iterator<String>
itr=set.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
} } }
Compiled by Tsegay M. 10
Java Sets: TreeSet
• Stores the elements in a
balanced binary tree Collection
TreeSet<String> set=new
TreeSet<String>();
set.add("Ravi"); LinkedHashSet SortedSet
set.add("Vijay");
set.add("Ravi");
set.add("Ajay");
TreeSet
//traversing elements
Iterator<String> itr=set.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
} } }
Compiled by Tsegay M. 11
Interface: List public interface List extends Collection {
// Positional Access
Object get(int index);
Object set(int index, Object element);
• A List is ordered void add(int index, Object element);
(also called a Object remove(int index);
abstract boolean addAll(int index,
sequence) Collection c);
• Lists can contain
// Search
duplicate elements int indexOf(Object o);
int lastIndexOf(Object o);
• List interface is
implemented by the // Iteration
ListIterator listIterator();
classes ArrayList, ListIterator listIterator(int index);
LinkedList, Vector,
// Range-view
and Stack. List subList(int from, int to);
}
• The user can access
elements by their
integer index
(position)
• To instantiate the
List interface, we
must use :
Compiled by Tsegay M. 12
Collection
• In addition to the
methods of the interface AbstractSequentialList
List ArrayList
• it provides methods to
manipulate the size of the
LinkedList Vector
Compiled by Tsegay M. 14
Java Lists: Vector
Collection
that:
- The
import methods of Vector are
java.util.*;
publicsynchronized
class AbstractSequentialList
TestJavaCollection3{
public static void main(String ArrayList
args[]){
Vector<String> v=new
Vector<String>(); LinkedList Vector
v.add("Ayush");
v.add("Amit"); Stack
v.add("Ashish");
v.add("Garima");
Iterator<String>
itr=v.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
} } } Compiled by Tsegay M. 15
Java Lists: Stack
• The Stack class represents a
Collection
stack.pop();
Iterator<String>
itr=stack.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
Compiled by Tsegay M. 16
} } }
public interface Map {
// Basic Operations
Interface: Map Object put(Object key, Object
value);
Object get(Object key);
• A Map is an object Object remove(Object key);
that maps keys to boolean containsKey(Object
values key);
boolean containsValue(Object
value);
int size();
• Maps cannot contain
boolean isEmpty();
duplicate keys
// Bulk Operations
void putAll(Map t);
• Each key can map to void clear();
at most one value
// Collection Views
public Set keySet();
public Collection values();
• Hashtables are an public Set entrySet();
example of Maps
// Interface for entrySet
elements
public interface Entry {
Object getKey();
Object getValue();
Object
Compiled by TsegaysetValue(Object
M. 17
value);
Interface: Map Hierarchy
Compiled by Tsegay M. 18
Interface: Map
Compiled by Tsegay M. 19
Java Maps: HashMap
• Contains values
based on the key.
• Contains only
unique keys.
• May have one null
key and multiple
null values
• Maintains no order
• Efficient in
inserting (put())
and retrieving
elements (get())
Compiled by Tsegay M. 20
Remove elements
Java HashMap Example
add elements import java.util.*;
public class HashMap2 {
import java.util.*;
public static void main(String
public class HashMapExample1{
args[]) {
HashMap<Integer,String> map=new
public static void
HashMap<Integer,String>();
main(String args[]){
map.put(100,"Amit");
HashMap<Integer,String>
map.put(101,"Vijay");
map=new
map.put(102,"Rahul");
HashMap<Integer,String>();//C
map.put(103, "Gaurav");
reating HashMap
System.out.println("Initial list of
map.put(1,"Mango"); //Put
elements: "+map);
elements in Map
//key-based removal
map.put(2,"Apple");
map.remove(100);
map.put(3,"Banana");
System.out.println("Updated list of
map.put(4,"Grapes");
elements: "+map);
//value-based removal
map.remove(101);
System.out.println("Iterating
System.out.println("Updated list
Hashmap...");
of elements: "+map);
for(Map.Entry m :
//key-value pair based removal
map.entrySet()){
map.remove(102, "Rahul");
System.out.println("Updated list
System.out.println(m.getKey()
of elements: "+map);
+" "+m.getValue());
} }
} Compiled by Tsegay M. 21
}
Java Maps: TreeMap
• Red-Black tree based
Map
implementation of the
SortedMap interface
AbstractMap
SortedMap
and mapping
map.put(101,"Vijay"); System.out.println(m.getKey()+"
"+m.getValue());
map.put(103,"Rahul"); }
map.remove(102);
System.out.println("After invoking
for(Map.Entry remove() method");
m:map.entrySet()){ for(Map.Entry m:map.entrySet())
{
System.out.println(m.getKey
()+" "+m.getValue()); System.out.println(m.getKey()+"
} "+m.getValue());
Compiled by Tsegay M. 23
}
The Iterator Interface
• JCF provides a uniform way to iterate through the
collection elements using the Iterator Interface
Compiled by Tsegay M. 24