HashMap in Java
HashMap is a class
which implements Map (an interface).
Hashmap has set of Key and Value.
Each key - value set must have unique keys but values can be
duplicate.
Hashmap accepts only one null Key but there can be multiple
null values.
There is no specific order of the elements in the hashmap.
Syntax:
HashMap
To enter a key - value set we use "put" and to
retrieve the VALUE using the KEY , we
use "get".
Example:
//To
use HashMap
import java.util.HashMap; //You need to import this class
public class HashMapTest {
public static void main(String[] args) {
HashMap mapObject = new HashMap<>();
//Entering
values(Key-Value set) in HashMap
mapObject.put("Name", "Smith"); // This is a key value pair being
stored in Hashmap
mapObject.put("Salary","2000");
mapObject.put("Department","Technical");
//Now to
retrieve the value using respective KEY.
String name = mapObject.get("Name");
System.out.println("Name is : "+name);
String salary = mapObject.get("Salary");
System.out.println("Salary is : "+salary);
String dept = mapObject.get("Department");
System.out.println("Department is : "+dept);
}
}
Output:
Name is
: Smith
Salary
is : 2000
Department is : Technical
You can put different data types for Key-Value:
Example 1:
import java.util.HashMap; //You need to import this class
public class HashMapTest {
public static void main(String[] args) {
HashMapInteger
>
mapObject = new HashMap<>();
//Entering values(Key-Value
set) in HashMap
mapObject.put("SNO", 10); // This is a key value pair being
stored in Hashmap
mapObject.put("Salary",2000);
mapObject.put("Phone", 1234567890);
//Now to
retrieve the value using respective KEY.
int sno = mapObject.get("SNO");
System.out.println("SNO is : "+sno);
int salary = mapObject.get("Salary");
System.out.println("Salary is : "+salary);
int ph = mapObject.get("Phone");
System.out.println("Phone is : "+ph);
}
}
OutPut:
SNO is :
10
Salary
is : 2000
Phone is
: 1234567890
Example 2:
//To
use HashMap
import java.util.HashMap; //You need to import this class
public class HashMapTest {
public static void main(String[] args) {
HashMap mapObject = new HashMap<>();
//Entering
values(Key-Value set) in HashMap
mapObject.put(1, 10.11f); // This is a key value
pair being stored in Hashmap
mapObject.put(2,20.25f);
//Now to
retrieve the value using respective KEY.
float value1 = mapObject.get(1);
System.out.println("Value1 is : "+value1);
float value2 = mapObject.get(2);
System.out.println("Value2 is : "+value2);
}
}
OutPut:
Value1
is : 10.11
Value2 is : 20.25
Remove the key-value and other features in HashMap:
From above example, remove the key-value set with Key=2:
mapObject.remove(2);
After running above code, the key-value set 2-20.25 is
removed from HashMap.
mapObject.size(); --- this will show the number of key value pairs
in the HashMap, size of the hashmap.
mapObject.clear();
--- this will removes all the Key-Value sets from Hashmap. After using this ,
you run mapObject.size(); , this will return zero. And
if you try to "get" the value , you will get "null"
mapObject.isEmpty(); --- To check if the hashmap is empty.
Examples:
import java.util.HashMap; //You need to import this class
public class HashMapTest {
public static void main(String[] args) {
HashMap mapObject = new HashMap<>();
//Entering
values(Key-Value set) in HashMap
mapObject.put("Name", "Smith"); // This is a key value pair being
stored in Hashmap
mapObject.put("Salary","2000");
mapObject.put("Department","Technical");
//Now to
retrieve the value using respective KEY.
String name = mapObject.get("Name");
System.out.println("Name is : "+name);
String salary = mapObject.get("Salary");
System.out.println("Salary is : "+salary);
String dept = mapObject.get("Department");
System.out.println("Department is : "+dept);
int size = mapObject.size();
System.out.println("HashMap size is : "+size);
mapObject.clear();
int size1 = mapObject.size();
System.out.println("HashMap size is : "+size1);
String name1 = mapObject.get("Name");
System.out.println("Name is : "+name1);
String salary1 = mapObject.get("Salary");
System.out.println("Salary is : "+salary1);
String dept1 = mapObject.get("Department");
System.out.println("Department is : "+dept1);
Boolean Empty = mapObject.isEmpty() ;
System.out.println("is the HashMap Empty ? "+ Empty);
}
}
OutPut:
Name is
: Smith
Salary
is : 2000
Department
is : Technical
HashMap
size is : 3
HashMap
size is : 0
Name is
: null
Salary
is : null
Department is : null
is the HashMap Empty ?
false
To know all key-value pairs in HashMap:
import java.util.HashMap;
public class HelloWorld {
public static void main(String[] args) {
HashMap mapObject = new HashMap<>();
//Entering
values(Key-Value set) in HashMap
mapObject.put("Name", "Smith");
mapObject.put("Salary","2000");
mapObject.put("Department","Technical");
System.out.println(mapObject);
}
OutPut:
{Salary=2000,
Department=Technical, Name=Smith}
To know/print all Keys and/or their value present in the
HashMap(To iterate through HashMap):
import java.util.HashMap;
import java.util.Set; // you need to import this additional class
public class HelloWorld {
public static void main(String[] args) {
HashMap mapObject = new HashMap<>();
//Entering
values(Key-Value set) in HashMap
mapObject.put("Name", "Smith");
mapObject.put("Salary","2000");
mapObject.put("Department","Technical");
System.out.println(mapObject);
//To know all
the keys
Set allKeys = mapObject.keySet();
for(String eachKEY: allKeys){
System.out.println("Key = "+eachKEY);
//To know
value of each key
System.out.println("Value for "+eachKEY+" =
" + mapObject.get(eachKEY));
}
}
}
OutPut:
{Salary=2000,
Department=Technical, Name=Smith}
Key =
Salary
Value
for Salary = 2000
Key =
Department
Value
for Department = Technical
Key =
Name
Value for Name = Smith
To know/print if any Key and/or Value is Present in the
HashMap:
import java.util.HashMap;
import java.util.Set; // you need to import this additional class
public class HelloWorld {
public static void main(String[] args) {
HashMap mapObject = new HashMap<>();
//Entering
values(Key-Value set) in HashMap
mapObject.put("Name", "Smith");
mapObject.put("Salary","2000");
mapObject.put("Department","Technical");
System.out.println(mapObject);
//Check if
the key is present
if(mapObject.containsKey("Department")) {
System.out.println("The key is present.");
} else {
System.out.println("The key is not present.");
}
//Check if
the value is present
if(mapObject.containsValue("Smith")) {
System.out.println("The value is present.");
} else {
System.out.println("The value is not present.");
}
}
}
OutPut:
{Salary=2000,
Department=Technical, Name=Smith}
The key
is present.
The value is present.
Copy one hashmap to another hashmap:
import java.util.HashMap;
public class HelloWorld {
public static void main(String[] args) {
//First hashmap
HashMap map1 = new HashMap<>();
map1.put("Name", "Smith");
map1.put("Salary","2000");
map1.put("Department","Technical");
System.out.println("map1 before copy " +map1);
//Second hashmap
HashMap map2 = new HashMap<>();
map2.put("Education", "Engineering");
System.out.println("map2 before copy "+map2);
//Copy map1 to map2
map2.putAll(map1);
System.out.println("map2 after copy "+map2);
System.out.println("map1 after copy "+map1);
}
}
OutPut:
map1
before copy {Salary=2000, Department=Technical, Name=Smith}
map2
before copy {Education=Engineering}
map2
after copy {Salary=2000, Department=Technical, Education=Engineering,
Name=Smith}
map1 after copy
{Salary=2000, Department=Technical, Name=Smith}
Additional Information
package test;
import java.util.HashMap;
import java.util.Map;
public class HashmapTest {
public static void main(String[] args) {
//using map
interface
Map<String, String> map = new HashMap<String, String>();
OR Map map = new HashMap<String, String>();
OR Map map = new HashMap<>(); //for
Map<Integer, Integer> map1 = new HashMap<Integer, Integer>();
Map<String, Integer> map2 = new HashMap<String, Integer>();
map.put("Name", "Ram");
map1.put(2001, 100);
map2.put("No", 15);
System.out.println(map.get("Name"));
System.out.println(map1.get(2001));
System.out.println(map2.get("No"));
//directly
using hashmap class
HashMap<String, String> hmap = new HashMap<String, String>();
OR HashMap hmap = new HashMap<String, String>();
OR HashMap hmap = new HashMap<>();
HashMap<Integer, Integer> hmap1 = new HashMap<Integer, Integer>();
HashMap<String, Integer> hmap2 = new HashMap<String, Integer>();
hmap.put("State", "TX");
hmap1.put(200, 100);
hmap2.put("EmpNo", 105);
System.out.println(hmap.get("State"));
System.out.println(hmap1.get(200));
System.out.println(hmap2.get("EmpNo"));
//no
parameters on left side
HashMap hmap3 = new HashMap<String, Integer>();
hmap3.put("EmpNo", 1000);
System.out.println(hmap3.get("EmpNo"));
Map map3 = new HashMap<String, Integer>();
map3.put("EmpNo", 1001);
System.out.println(map3.get("EmpNo"));
//hashmap
with float/decimal value, NOT using "f", Integer is working for float
HashMap hmap4 = new HashMap<String, Integer>();
hmap4.put("Pay", 123.45);
System.out.println(hmap4.get("Pay"));
//map with
float/decimal value, NOT using "f", Integer is working for float
Map map4 = new HashMap<String, Integer>();
map4.put("Pay", 1001.123456789);
System.out.println(map4.get("Pay"));
//hashmap
WITH OUT parameters
HashMap hmap5 = new HashMap<>();
hmap5.put("Pay", 100.0001);
System.out.println(hmap5.get("Pay"));
//map WITH
OUT parameters
//HashMap<String,
float> hmap5 = new HashMap<String, float>(); Error
//Map map5 =
new HashMap<String, long>(); Error
Map map5 = new HashMap<>();
map5.put("float value", 1001.123456789f);//float, but no need to use the char f
System.out.println(map5.get("float
value"));
Map map6 = new HashMap<>();
//map6.put("long
value", 10.1234789l); Error
map6.put("double value", 10.1234789d);//double, but no need to use the char d
System.out.println(map6.get("double
value"));
}
}
Output:
Ram
100
15
TX
100
105
1000
1001
123.45
1001.123456789
100.0001
1001.1235
10.1234789
No comments:
Post a Comment