xxxxxxxxxx
// Java program to demonstrate
// the working of Map interface
import java.util.*;
class GFG {
public static void main(String args[])
{
// Initialization of a Map
// using Generics
Map<Integer, String> hm1
= new HashMap<Integer, String>();
// Inserting the Elements
hm1.put(new Integer(1), "Geeks");
hm1.put(new Integer(2), "Geeks");
hm1.put(new Integer(3), "Geeks");
System.out.println("Initial Map " + hm1);
hm1.put(new Integer(2), "For");
System.out.println("Updated Map " + hm1);
}
}
xxxxxxxxxx
// Java Program to Illustrate the LinkedHashmap Class
// Importing required classes
import java.util.*;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an empty LinkedHashMap
Map<String, Integer> map = new LinkedHashMap<>();
// Inserting pair entries in above Map
// using put() method
map.put("vishal", 10);
map.put("sachin", 30);
map.put("vaibhav", 20);
// Iterating over Map
for (Map.Entry<String, Integer> e : map.entrySet())
// Printing key-value pairs
System.out.println(e.getKey() + " "
+ e.getValue());
}
}
xxxxxxxxxx
// Java Program to Demonstrate
// Working of Map interface
// Importing required classes
import java.util.*;
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
// Creating an empty HashMap
Map<String, Integer> hm
= new HashMap<String, Integer>();
// Inserting pairs in above Map
// using put() method
hm.put("a", new Integer(100));
hm.put("b", new Integer(200));
hm.put("c", new Integer(300));
hm.put("d", new Integer(400));
// Traversing through Map using for-each loop
for (Map.Entry<String, Integer> me :
hm.entrySet()) {
// Printing keys
System.out.print(me.getKey() + ":");
System.out.println(me.getValue());
}
}
}
xxxxxxxxxx
// Java Program to illustrate the Hashmap Class
// Importing required classes
import java.util.*;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an empty HashMap
Map<String, Integer> map = new HashMap<>();
// Inserting entries in the Map
// using put() method
map.put("vishal", 10);
map.put("sachin", 30);
map.put("vaibhav", 20);
// Iterating over Map
for (Map.Entry<String, Integer> e : map.entrySet())
// Printing key-value pairs
System.out.println(e.getKey() + " "
+ e.getValue());
}
}
xxxxxxxxxx
// Java Program to Illustrate TreeMap Class
// Importing required classes
import java.util.*;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an empty TreeMap
Map<String, Integer> map = new TreeMap<>();
// Inserting custom elements in the Map
// using put() method
map.put("vishal", 10);
map.put("sachin", 30);
map.put("vaibhav", 20);
// Iterating over Map using for each loop
for (Map.Entry<String, Integer> e : map.entrySet())
// Printing key-value pairs
System.out.println(e.getKey() + " "
+ e.getValue());
}
}
xxxxxxxxxx
// Java program to demonstrate
// the working of Map interface
import java.util.*;
class GFG {
public static void main(String args[])
{
// Initialization of a Map
// using Generics
Map<Integer, String> hm1
= new HashMap<Integer, String>();
// Inserting the Elements
hm1.put(new Integer(1), "Geeks");
hm1.put(new Integer(2), "For");
hm1.put(new Integer(3), "Geeks");
hm1.put(new Integer(4), "For");
// Initial Map
System.out.println(hm1);
hm1.remove(new Integer(4));
// Final Map
System.out.println(hm1);
}
}
xxxxxxxxxx
// Java program to demonstrate
// the working of Map interface
import java.util.*;
class GFG {
public static void main(String args[])
{
// Initialization of a Map
// using Generics
Map<Integer, String> hm1
= new HashMap<Integer, String>();
// Inserting the Elements
hm1.put(new Integer(1), "Geeks");
hm1.put(new Integer(2), "For");
hm1.put(new Integer(3), "Geeks");
for (Map.Entry mapElement : hm1.entrySet()) {
int key
= (int)mapElement.getKey();
// Finding the value
String value
= (String)mapElement.getValue();
System.out.println(key + " : "
+ value);
}
}
}