Collections in Java
• The Collection 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.
• A Collection represents a single unit of objects, i.e., a group.
• Java Collection means a single unit of objects. Java Collection
framework provides many interfaces (Set, List, Queue, Deque) and
classes (ArrayList, Vector, LinkedList, PriorityQueue, HashSet,
LinkedHashSet, TreeSet).
• The Collection framework represents a unified architecture for storing
and manipulating a group of objects. It has:
1. Interfaces and its implementations, i.e., classes
2. Algorithm
• The java.util package contains all the classes and interfaces for the
Collection framework.
Iterator interface
• Iterator interface provides the facility of iterating the
elements in a forward direction only.
No. Method Description
1 public boolean It returns true if the iterator has more elements
hasNext() 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.
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()
• What is the use of the List interface?
• The List interface in Java is an ordered collection of
elements. It maintains the insertion order and allows
duplicate values to be stored within.
Differentiate between Collection and
Collections
Collection Collections
The collection is an interface. Collections is a class.
It represents a group of objects as a single entity. It defines various utility methods for collection objects.
The collection is the root interface of the Java Collection Collections is a general utility class.
framework.
This interface is used to derive the collection data This class contains static methods to manipulate data
structures. structure.
Differentiate between List and Set.
List Set
An ordered collection of elements An unordered collection of elements
Preserves the insertion order Doesn't preserves the insertion order
Duplicate values are allowed Duplicate values are not allowed
Any number of null values can be stored Only one null values can be stored
ListIterator can be used to traverse the List in any ListIterator cannot be used to traverse a Set
direction
Contains a legacy class called vector Doesn't contains any legacy class
Java Map Interface
• A map contains values on the basis of key, i.e. key and
value pair. Each key and value pair is known as an
entry. A Map contains unique keys.
• A Map is useful if you have to search, update or delete
elements on the basis of a key.
Java Map Hierarchy
• 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
Class Description
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 and SortedMap. It maintains
ascending order.
1.Map map=new HashMap();
2. //Adding elements to map
3. map.put(1,"Amit");
4. map.put(5,"Rahul");
5. map.put(2,"Jai");
6. map.put(6,"Amit");
7. //Traversing Map
8. Set set=map.entrySet();//Converting to Set so that we can traverse
9. Iterator itr=set.iterator();
10. while(itr.hasNext()){
11. //Converting to Map.Entry so that we can get key and value separately
12. Map.Entry entry=(Map.Entry)itr.next();
13. System.out.println(entry.getKey()+" "+entry.getValue());
1.public class HashMapExample1{
2. public static void main(String args[]){
3. HashMap<Integer,String> map=new HashMap<Integer,String>();//
Creating HashMap
4. map.put(1,"Mango"); //Put elements in Map
5. map.put(2,"Apple");
6. map.put(3,"Banana");
7. map.put(4,"Grapes");
8.
9. System.out.println("Iterating Hashmap...");
10. for(Map.Entry m : map.entrySet()){
11. System.out.println(m.getKey()+" "+m.getValue());
12. }
13.}
Iterating Over a Map (using Collection views)
Map<String, String> mapCountryCodes = new HashMap<>();
mapCountryCodes.put("1", "USA");
mapCountryCodes.put("44", "United Kingdom");
mapCountryCodes.put("33", "France");
mapCountryCodes.put("81", "Japan");
Set<String> setCodes = mapCountryCodes.keySet();
Iterator<String> iterator = setCodes.iterator();
while (iterator.hasNext()) {
String code = iterator.next();
String country = mapCountryCodes.get(code);
System.out.println(code + " => " + country);
}
Java Hashtable class
• Java Hashtable class implements a hashtable, which
maps keys to values. It inherits Dictionary class and
implements the Map interface.
• Points to remember
• A Hashtable is an array of a list. Each list is known as a
bucket. The position of the bucket is identified by
calling the hashcode() method. A Hashtable contains
values based on the key.
• Java Hashtable class contains unique elements.
• Java Hashtable class doesn't allow null key or value.
• Java Hashtable class is synchronized.
Hashtable class declaration
• Let's see the declaration for java.util.Hashtable class.
1.public class Hashtable<K,V> extends Dictionary<K
,V> implements Map<K,V>, Cloneable, Serializable
• Hashtable class Parameters
• Let's see the Parameters for java.util.Hashtable class.
• K: It is the type of keys maintained by this map.
• V: It is the type of mapped values.
Constructors of Java
Hashtable class
Constructor Description
Hashtable() It creates an empty hashtable having the initial
default capacity and load factor.
Hashtable(int capacity) It accepts an integer parameter and creates a
hash table that contains a specified initial
capacity.
Hashtable(int capacity, float loadFactor) It is used to create a hash table having the
specified initial capacity and loadFactor.
Hashtable(Map<? extends K,? extends V> t) It creates a new hash table with the same
mappings as the given Map.
Java Hashtable Example
1. import java.util.*;
2. class Hashtable1{
3. public static void main(String args[]){
4. Hashtable<Integer,String> hm=new Hashtable<Integer,String>();
5.
6. hm.put(100,"Amit");
7. hm.put(102,"Ravi");
8. hm.put(101,"Vijay");
9. hm.put(103,"Rahul");
10.
11. for(Map.Entry m:hm.entrySet()){
12. System.out.println(m.getKey()+" "+m.getValue());
13. }
14. }
15.}
Java Hashtable Example:
putIfAbsent()
1. import java.util.*;
2. class Hashtable4{
3. public static void main(String args[]){
4. Hashtable<Integer,String> map=new Hashtable<Integer,String>();
5. map.put(100,"Amit");
6. map.put(102,"Ravi");
7. map.put(101,"Vijay");
8. map.put(103,"Rahul");
9. System.out.println("Initial Map: "+map);
10. //Inserts, as the specified pair is unique
11. map.putIfAbsent(104,"Gaurav");
12. System.out.println("Updated Map: "+map);
13. //Returns the current value, as the specified pair already exist
14. map.putIfAbsent(101,"Vijay");
15. System.out.println("Updated Map: "+map);
16. }
17.}