Java Map Generic
Java arrays maintain an ordered collection of things, and the index can be used to access the data (an integer). Unlike HashMap, which stores data as a Key/Value pair. We can store the items or values using a hash map, and we can access those values using indexes or keys of any type, including Integer, String, Double, Character, and user-defined datatypes.
A HashMap uses Key -> Value as the source of its mappings.
Since Java 1.2, HashMap has been a component of Java. It uses the Java.util API. The map interface.
What is the definition of Generic Map and what does it differ from HashMap?
The notion behind the term "generic" is simply that any user-defined type, such as an integer, double, string, or another, may be used as a parameter for methods, classes, or interfaces. For instance, generics are used by all of Java's built-in collections, including ArrayList, HashSet, HashMap, etc.
Generic Map can be summed up as follows in basic English:
Map< K, V > map = new HashMap< K, V >();
Where generic type parameter given with in declaration of a HashMap is specified by K and V. In the following syntax, K and V can be replaced with any type, such as an Integer, String, Float, Character, or any user-defined type, to indicate that we can populate the HashMap as we choose.
Example :
We can initialize it as follows if key is of type String as well as the matching value seems to be of type Integer:
Map< String , Integer > map = new HashMap< String ,Integer >();
Only String instances can now be used as keys and Integer instances can be used as values in the map.
A Generic Map Can Be accessed:
The functions put() and get() can be used to accomplish this.
- put(): used to update the HashMap with a new key/value pair.
- get(): used to obtain the value associated with a specific key.
Example:
Map< Integer, String > map = new HashMap<>();
// 123 and the value are added.
// corresponding to it on the map as abc
map.put( 123, "abc");
map.put(65, "b");
map.put(2554 , "Generic");
map.get(65);
Output:
b
Iterating A generic map:
For iteration, the map has two collections. KeySet() and values are two examples.
Example:
By using iterator() method
Map<Integer, Integer> map = new HashMap<>;
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:
utilizing a fresh for-loop, a for-each loop, or a generic for loop
//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 demonstrate how to use a map:
import java.io.*;
import java.util.*;
class GenericMap {
public static void main(String[] args)
{
// To create array of strings
String arr[]
= { "generic", "code", "map", "program",
"code", "site", "map", "generic",
"java", "generic", "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);
}
}
System.out.println(map.get("generic"));
System.out.println(map.get("code"));
}
}
Output :
3
2
Using Java Generics, Map Different Value Types:
Let's say for the purpose of illustration that you need to offer some sort of application environment that enables you to bind data of any type to certain keys. An easy non-type safe implementation with String keys and a HashMap backing might appear as follows:
public class Scope {
private final Map<String,Object> values = new HashMap<>();
public void put( String key, Object value ) {
values.put( key, value );
}
public Object get( String key ) {
return values.get( key );
}
[...]
}
How to use this Scope in a program is demonstrated by the following snippet:
Scope scope = new Scope();
Runnable runnable = . . .
Scope.put( “key” , runnable);
// iterations of computations late
Runnable value = ( Runnable )scope.get( “key” );
This is the scope of a program to demonstrate’s the following snippet.