JAVA - How To Use HashMap In Java NetBeans
package JavaDB_001;
import java.util.*;
public class Work {
public static void main(String[] args){
//create a HashMap
//Key is String
//value is Integer
HashMap<String,Integer> employer = new HashMap<String,Integer>();
//filling the HashMap with put method
employer.put("employer 1", 34);
employer.put("employer 2", 43);
employer.put("employer 3", 36);
employer.put("employer 5", 55);
employer.put("employer 6", 29);
employer.put("employer 7", 27);
employer.put("employer 8", 30);
employer.put("employer 9", 51);
//displaying only the key of the hashmap
Set<String> EmployerName = employer.keySet();
System.out.println("_______Key_______");
for(String s: EmployerName)
System.out.println(s);
//displaying only the value of the hashmap
Collection<Integer> Age = employer.values();
System.out.println("_______Value_______");
for(Integer i : Age)
System.out.println(i);
//displaying the key and the value of the hashmap
System.out.println("_______Key_And_Value_______");
Set<String> EmpName = employer.keySet();
for(String s: EmpName)
System.out.println("Key => ["+s +"] Value => ["+employer.get(s)+"]");
}
}
///////////////////////////OUTPUT:
_______Key_______
employer 9
employer 5
employer 6
employer 7
employer 8
employer 1
employer 2
employer 3
_______Value_______
51
55
29
27
30
34
43
36
_______Key_And_Value_______
Key => [employer 9] Value => [51]
Key => [employer 5] Value => [55]
Key => [employer 6] Value => [29]
Key => [employer 7] Value => [27]
Key => [employer 8] Value => [30]
Key => [employer 1] Value => [34]
Key => [employer 2] Value => [43]
Key => [employer 3] Value => [36]
Another Source Code:
package javaapp;
import java.util.ArrayList;
import java.util.HashMap;
public class Work {
public static void main(String[] args){
HashMap<Integer, String> map = new HashMap<Integer, String>();
map.put(10, "A");
map.put(20, "B");
map.put(30, "C");
map.put(40, "D");
map.put(50, "E");
map.put(60, "F");
printData(map);
// delete an elemnt from the hashmap
map.remove(60);
printData(map);
System.out.println("");
System.out.println(map);
map.replace(10, "Z");
System.out.println(map);
// see if the map contains a value
boolean statVal = map.containsValue("Z");
System.out.println(statVal);
// see if the map contains a key
boolean statKey = map.containsKey(40);
System.out.println(statKey);
// get value using his key
String val = map.get(100);
System.out.println(val);
// check if the map is empty
boolean isEmpty = map.isEmpty();
System.out.println(isEmpty);
// insert tvalues from hashmap to arraylist
ArrayList<String> values = new ArrayList<String>(map.values());
System.out.println("Values: "+values);
// create another hashmap
HashMap<Integer, String> map2 = new HashMap<Integer, String>();
map2.put(100,"MAP2-1");
map2.put(200,"MAP2-2");
map2.put(300,"MAP2-3");
// insert a map in another map
map.putAll(map2);
printData(map);
// clone the hashmap
Object obj = map.clone();
System.out.println(obj);
/*
If the specified key is not associated with a value
or the value is null associates it with the given value
in this example the key is 111 and value is null
*/
map.put(111, null);
map.putIfAbsent(111, "NewValue");
System.out.println(map);
// get the HashMap Size
int mapSize = map.size();
System.out.println(mapSize);
// clear all elements from the hashmap
map.clear();
mapSize = map.size();
System.out.println(mapSize);
System.out.println("The HashMap Is Clear: "+map);
System.out.println("The HashMap is Empty Now = "+map.isEmpty());
}
// cretate a function to print data from the hashmap
public static void printData(HashMap<Integer,String> list){
for(Integer i : list.keySet()){
System.out.println("Key: "+i+" Value: "+list.get(i));
}
System.out.println("-----------------------");
}
}
//OUTPUT:
Key: 50 Value: E
Key: 20 Value: B
Key: 40 Value: D
Key: 10 Value: A
Key: 60 Value: F
Key: 30 Value: C
-----------------------
Key: 50 Value: E
Key: 20 Value: B
Key: 40 Value: D
Key: 10 Value: A
Key: 30 Value: C
-----------------------
{50=E, 20=B, 40=D, 10=A, 30=C}
{50=E, 20=B, 40=D, 10=Z, 30=C}
true
true
null
false
Values: [E, B, D, Z, C]
Key: 50 Value: E
Key: 20 Value: B
Key: 100 Value: MAP2-1
Key: 40 Value: D
Key: 200 Value: MAP2-2
Key: 10 Value: Z
Key: 300 Value: MAP2-3
Key: 30 Value: C
-----------------------
{50=E, 20=B, 100=MAP2-1, 40=D, 200=MAP2-2, 10=Z, 300=MAP2-3, 30=C}
{50=E, 20=B, 100=MAP2-1, 40=D, 200=MAP2-2, 10=Z, 300=MAP2-3, 30=C, 111=NewValue}
9
0
The HashMap Is Clear: {}
The HashMap is Empty Now = true
See Also : HashMap In Java With Iterator
_______Key_______
employer 9
employer 5
employer 6
employer 7
employer 8
employer 1
employer 2
employer 3
_______Value_______
51
55
29
27
30
34
43
36
_______Key_And_Value_______
Key => [employer 9] Value => [51]
Key => [employer 5] Value => [55]
Key => [employer 6] Value => [29]
Key => [employer 7] Value => [27]
Key => [employer 8] Value => [30]
Key => [employer 1] Value => [34]
Key => [employer 2] Value => [43]
Key => [employer 3] Value => [36]
Another Source Code:
package javaapp;
import java.util.ArrayList;
import java.util.HashMap;
public class Work {
public static void main(String[] args){
HashMap<Integer, String> map = new HashMap<Integer, String>();
map.put(10, "A");
map.put(20, "B");
map.put(30, "C");
map.put(40, "D");
map.put(50, "E");
map.put(60, "F");
printData(map);
// delete an elemnt from the hashmap
map.remove(60);
printData(map);
System.out.println("");
System.out.println(map);
map.replace(10, "Z");
System.out.println(map);
// see if the map contains a value
boolean statVal = map.containsValue("Z");
System.out.println(statVal);
// see if the map contains a key
boolean statKey = map.containsKey(40);
System.out.println(statKey);
// get value using his key
String val = map.get(100);
System.out.println(val);
// check if the map is empty
boolean isEmpty = map.isEmpty();
System.out.println(isEmpty);
// insert tvalues from hashmap to arraylist
ArrayList<String> values = new ArrayList<String>(map.values());
System.out.println("Values: "+values);
// create another hashmap
HashMap<Integer, String> map2 = new HashMap<Integer, String>();
map2.put(100,"MAP2-1");
map2.put(200,"MAP2-2");
map2.put(300,"MAP2-3");
// insert a map in another map
map.putAll(map2);
printData(map);
// clone the hashmap
Object obj = map.clone();
System.out.println(obj);
/*
If the specified key is not associated with a value
or the value is null associates it with the given value
in this example the key is 111 and value is null
*/
map.put(111, null);
map.putIfAbsent(111, "NewValue");
System.out.println(map);
// get the HashMap Size
int mapSize = map.size();
System.out.println(mapSize);
// clear all elements from the hashmap
map.clear();
mapSize = map.size();
System.out.println(mapSize);
System.out.println("The HashMap Is Clear: "+map);
System.out.println("The HashMap is Empty Now = "+map.isEmpty());
}
// cretate a function to print data from the hashmap
public static void printData(HashMap<Integer,String> list){
for(Integer i : list.keySet()){
System.out.println("Key: "+i+" Value: "+list.get(i));
}
System.out.println("-----------------------");
}
}
//OUTPUT:
Key: 50 Value: E
Key: 20 Value: B
Key: 40 Value: D
Key: 10 Value: A
Key: 60 Value: F
Key: 30 Value: C
-----------------------
Key: 50 Value: E
Key: 20 Value: B
Key: 40 Value: D
Key: 10 Value: A
Key: 30 Value: C
-----------------------
{50=E, 20=B, 40=D, 10=A, 30=C}
{50=E, 20=B, 40=D, 10=Z, 30=C}
true
true
null
false
Values: [E, B, D, Z, C]
Key: 50 Value: E
Key: 20 Value: B
Key: 100 Value: MAP2-1
Key: 40 Value: D
Key: 200 Value: MAP2-2
Key: 10 Value: Z
Key: 300 Value: MAP2-3
Key: 30 Value: C
-----------------------
{50=E, 20=B, 100=MAP2-1, 40=D, 200=MAP2-2, 10=Z, 300=MAP2-3, 30=C}
{50=E, 20=B, 100=MAP2-1, 40=D, 200=MAP2-2, 10=Z, 300=MAP2-3, 30=C, 111=NewValue}
9
0
The HashMap Is Clear: {}
The HashMap is Empty Now = true
See Also : HashMap In Java With Iterator