Map Interface
public interface Map<K,V>
- 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.
- Map is useful, when we have to search, update and delete values based on keys.
- An object that maps keys to values. A map cannot contain duplicate keys. Each key can map to at most one value.
- This interface takes the place of the Dictionary class, which was a totally abstract class rather than an interface.
- The Map interface provides three collection views, which allow a map’s contents to be viewed as a set of keys, collection of values, or set of key-value mappings.
- The Map interface present in java.util package.
- The Map interface is not a subtype of the Collection interface.
- Therefore it behaves a bit differently from the rest of the collection types. A map contains unique keys.
Map.Entry Interface
Entry is the sub interface of Map. So we will be accessed it by Map.Entry name. It returns a collection-view of the map, whose elements are of this class. It provides methods to get key and value.
References:https://www.javatpoint.com/java-map, https://docs.oracle.com/javase/8/docs/api/java/util/Map.html
Java Map Hierarchy
There are two interfaces for implementing Map in java: Map and SortedMap, and three classes: HashMap, LinkedHashMap, and TreeMap. The hierarchy of Java Map is given below:
![Java Map Hierarchy](https://static.javatpoint.com/images/core/java-map-hierarchy.png)
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.(to be discussed)
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. |
For sample programs for map interface implementing class refer:
https://github.com/LathaMurugan-code/Collections
List interface
Arraylist
Arraylist class implements list interface.
To know the advantages of methods we can go through the sample program which shows the same.
Program
package collectionpackage;
import java.util.ArrayList;
public class ArraylistSample {
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList a = new ArrayList();
a.add(23);
//adds integer object
a.add(0.5f);
a.add(“latha”);
System.out.println(“Index of latha- “+a.indexOf(“latha”));
a.add(2,true);
//add object at specified index
System.out.println(“At index 1- “+a.get(1));
//Returns value at specified index
System.out.println(a);
System.out.print(“Is object 23 exists? “);
System.out.println(a.contains(23));
ArrayList a2 = new ArrayList();
a2.addAll(a);
//adds all elements from a
System.out.println(“a2 ArrayList”+a2);
System.out.println(“Arraylist after remove operation”);
a.remove(2);
//remove the object at specified index
a.remove(“latha”);
System.out.println(a);
a.removeAll(a);
//removes whole collection class
System.out.println(a);
System.out.println(“Arraylist empty? “+a.isEmpty());
}
}
Output
Index of latha- 2
At index 1- 0.5
[23, 0.5, true, latha]
Is object 23 exists? true
a2 ArrayList[23, 0.5, true, latha]
Arraylist after remove operation
[23, 0.5]
[]
Arraylist empty? true
Java LinkedList class
![Java LinkedList class hierarchy](https://static.javatpoint.com/images/linkedlist.png)
Java LinkedList class uses a doubly linked list to store the elements. It provides a linked-list data structure. It inherits the AbstractList class and implements List and Deque interfaces.
ArrayList | LinkedList |
---|---|
1) ArrayList internally uses a dynamic array to store the elements. | LinkedList internally uses a doubly linked list to store the elements. |
2) Manipulation with ArrayList is slow because it internally uses an array. If any element is removed from the array, all the bits are shifted in memory. | Manipulation with LinkedList is faster than ArrayList because it uses a doubly linked list, so no bit shifting is required in memory. |
3) An ArrayList class can act as a list only because it implements List only. | LinkedList class can act as a list and queue both because it implements List and Deque interfaces. |
4) ArrayList is better for storing and accessing data. | LinkedList is better for manipulating data. |