Java Arrays store items in an ordered collection and the values can be accessed using the index(an integer). Whereas HashMap stores as a Key/ Value pair. Using HashMap, we can store the items or values and these values can be accessed by indexes/ keys of any type be it Integer, String, Double, Character, or any user-defined datatype.
The Mappings in a HashMap are from Key → Value
HashMap is a part of Java since Java 1.2. It implements java.util.Map interface.
What is Generic Map and how is it different from the term HashMap?
The term generic simply is the idea of allowing the type (Integer, Double, String, etc. or any user-defined type) to be the parameter to methods, class, or interface. For eg, all the inbuilt collections in java like ArrayList, HashSet, HashMap, etc. use generics.
Generic Map in simple language can be generalized as:
Map< K, V > map = new HashMap< K, V >();
Where K and V are used to specify the generic type parameter passed in the declaration of a HashMap. We can add any type be it Integer, String, Float, Character, or any user-defined type in place of K and V in the above syntax to specify that we can initialize the HashMap of our wish.
Example:
Suppose if the key is of type String and the corresponding value is of type Integer, then we can initialize it as,
Map< String , Integer > map = new HashMap< String ,Integer >();
The map can now only accept String instances as key and Integer instances as values.
Accessing A Generic Map
This can be done by using put() and get() function.
1. put(): Used to add a new key/value pair to the Hashmap.
2. get(): Used to get the value corresponding to a particular key.
Example :
Map< Integer, String > map = new HashMap<>();
// adding the key 123 and value
// corresponding to it as abc in the map
map.put( 123, "abc");
map.put(65, "a");
map.put(2554 , "GFG");
map.get(65);
Output:
a
Iterating A Generic Map
Map has two collections for iteration. One is keySet() and the other is values().
Example: Using iterator() method
Map<Integer, Integer> map = new HashMap<>;
//adding key, value pairs to the Map
// iterate keys.
Iterator<Integer> key = map.keySet().iterator();
while(key.hasNext()){
Integer aKey = key.next();
String aValue = map.get(aKey);
}
Iterator<Integer> value = map.values().iterator();
while(valueIterator.hasNext()){
String aString = value.next();
}
Example: Using new for-loop or for-each loop or generic for loop
Map<Integer, String> map = new HashMap<Integer, String>;
//adding key, value pairs to the Map
//using for-each loop
for(Integer key : map.keySet()) {
String value = map.get(key);
System.out.println("" + key + ":" + value);
}
for(String value : map.values()) {
System.out.println(value);
}
Java Program to illustrate the usage of a map
Java
// Java program to demonstrate
// Generic Map
import java.io.*;
import java.util.*;
class GenericMap {
public static void main(String[] args)
{
// create array of strings
String arr[]
= { "gfg", "code", "quiz", "program",
"code", "website", "quiz", "gfg",
"java", "gfg", "program" };
// to count the frequency of a string and store it
// in the map
Map<String, Integer> map = new HashMap<>();
for (int i = 0; i < arr.length; i++) {
if (map.containsKey(arr[i])) {
int count = map.get(arr[i]);
map.put(arr[i], count + 1);
}
else {
map.put(arr[i], 1);
}
}
// to get and print the count of the mentioned
// string
System.out.println(map.get("gfg"));
System.out.println(map.get("code"));
}
}
Similar Reads
Generics in Java Generics means parameterized types. The idea is to allow a type (like Integer, String, etc., or user-defined types) to be a parameter to methods, classes, and interfaces. Generics in Java allow us to create classes, interfaces, and methods where the type of the data is specified as a parameter. If w
10 min read
Generic Set In Java The Set interface is present in java.util package. It is basically a collection of objects with no duplicate objects that means there can be no two objects in a Set e1 and e2 such that e1.equals(e2) and at most one null element. It is an interface that models the mathematical set. This interface inh
3 min read
Generic For Loop in Java When we know that we have to iterate over a whole set or list, then we can use Generic For Loop. Java's Generic has a new loop called for-each loop. It is also called enhanced for loop. This for-each loop makes it easier to iterate over array or generic Collection classes. In normal for loop, we wri
4 min read
Generic Class in Java Java Generics was introduced to deal with type-safe objects. It makes the code stable.Java Generics methods and classes, enables programmer with a single method declaration, a set of related methods, a set of related types. Generics also provide compile-time type safety which allows programmers to c
4 min read
Templates in C++ vs Generics in Java While building large-scale projects, we need the code to be compatible with any kind of data which is provided to it. That is the place where your written code stands above that of others. Here what we meant is to make the code we write be generic to any kind of data provided to the program regardle
3 min read