![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 9326 Articles for Object Oriented Programming
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
2K+ Views
To check whether two HashMap are equal or not, use the equals() method.Let us first create the first HashMap −// Create hash map 1 HashMap hm1 = new HashMap(); hm1.put("Shirts", new Integer(700)); hm1.put("Trousers", new Integer(600)); hm1.put("Jeans", new Integer(1200)); hm1.put("Android TV", new Integer(450)); hm1.put("Air Purifiers", new Integer(300)); hm1.put("Food Processors", new Integer(950));Let us now create the second HashMap −HashMap hm2 = new HashMap(); hm2.put("Shirts", new Integer(700)); hm2.put("Trousers", new Integer(600)); hm2.put("Jeans", new Integer(1200)); hm2.put("Android TV", new Integer(450)); hm2.put("Air Purifiers", new Integer(300)); hm2.put("Food Processors", new Integer(950));To check whether both the HashMap are equal or not, use the equals() method like this −hm1.equals(hm2)The following is ... Read More
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
147 Views
Use the clone() method to clone HashMap.The following is our HashMap with elements −HashMap hm1 = new HashMap(); hm1.put("Shirts", new Integer(700)); hm1.put("Trousers", new Integer(600)); hm1.put("Jeans", new Integer(1200)); hm1.put("Android TV", new Integer(450)); hm1.put("Air Purifiers", new Integer(300)); hm1.put("Food Processors", new Integer(950));Create another HashMap and clone the first HashMap into it −HashMap hm2 = (HashMap)hm1.clone();The following is an example to clone HashMap in Java −Example Live Demoimport java.util.*; public class Demo { public static void main(String args[]) { // Create hash map HashMap hm1 = new HashMap(); ... Read More
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
251 Views
Create a HashMap and add elements to it −HashMap hm = new HashMap(); hm.put("Wallet", new Integer(700)); hm.put("Belt", new Integer(600)); hm.put("Backpack", new Integer(1200));The following is the code snippet to retrieve a set of Map.Entry elements −Set s = hm.entrySet(); Iterator iter = s.iterator(); System.out.println("Key\tValue"); while (iter.hasNext()) { Map.Entry e = (Map.Entry) iter.next(); System.out.println(e.getKey() + " " + e.getValue()); }The following is an example to retrieve a set of Map.Entry elements from a HashMap −Example Live Demoimport java.util.*; public class Demo { public static void main(String args[]) { ... Read More
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
198 Views
First, create a HashMap and add elements −HashMap hm = new HashMap(); hm.put("Wallet", new Integer(700)); hm.put("Belt", new Integer(600)); hm.put("Backpack", new Integer(1200));Now, retrieve all the values −Collection getValues = hm.values(); System.out.println("Values..."); Iterator i = getValues.iterator(); while (i.hasNext()) { System.out.println(i.next()); }The following is an example to get the set of all the values in the HashMap −Example Live Demoimport java.util.*; public class Demo { public static void main(String args[]) { // Create hash map HashMap hm = new HashMap(); ... Read More
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
437 Views
To copy, use the putAll() method.Let us first create two Map −First Map −HashMap hm = new HashMap(); hm.put("Wallet", new Integer(700)); hm.put("Belt", new Integer(600));Second Map −HashMap hm2 = new HashMap(); hm.put("Bag", new Integer(1100)); hm.put("Sunglasses", new Integer(2000)); hm.put("Frames", new Integer(800));Now, copy the key-value pairs from one Map to another −hm.putAll(hm2);The following is an example to copy all the key-value pairs from one Map into another −Example Live Demoimport java.util.*; public class Demo { public static void main(String args[]) { // Create hash map 1 HashMap hm = ... Read More
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
361 Views
Use Iterator to iterate through the values of HashMap −HashMap hm = new HashMap(); // Put elements to the map hm.put("Bag", new Integer(1100)); hm.put("Sunglasses", new Integer(2000)); hm.put("Frames", new Integer(800)); hm.put("Wallet", new Integer(700)); hm.put("Belt", new Integer(600));Now, use Iterator to display each and every value and key −// Get an iterator Iterator i = set.iterator(); // Display elements while(i.hasNext()) { Map.Entry me = (Map.Entry)i.next(); System.out.print(me.getKey() + ": "); System.out.println(me.getValue()); }The following is an example to iterate through the values of HashMap −Example Live Demoimport java.util.*; public class Demo { public static ... Read More
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
88 Views
Use the remove() method to remove a key only if it is associated with a given value.Let’s say the following is our HashMap −// Create a hash map HashMap hm = new HashMap(); // Put elements to the map hm.put("Bag", new Integer(1100)); hm.put("Sunglasses", new Integer(2000)); hm.put("Frames", new Integer(800)); hm.put("Wallet", new Integer(700)); hm.put("Belt", new Integer(600));Here, we are removing a key “Belt” only if it is associated with the given value 600 −hm.remove("Belt", 600);The following is an example to remove a key only if it is associated with a given value −Example Live Demoimport java.util.*; public class Demo { public static void ... Read More
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
494 Views
To remove a key from HashMap, use the remove() method.First, create a HashMap and add elements −// Create a hash map HashMap hm = new HashMap(); // Put elements to the map hm.put("Bag", new Integer(1100)); hm.put("Sunglasses", new Integer(2000)); hm.put("Frames", new Integer(800)); hm.put("Wallet", new Integer(700)); hm.put("Belt", new Integer(600));Let us now remove a key −hm.remove("Wallet");The following is an example to remove a key from a HashMap −Example Live Demoimport java.util.*; public class Demo { public static void main(String args[]) { // Create a hash map HashMap hm = new HashMap(); // Put elements ... Read More
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
236 Views
First, create a HashMap and add elements to it −// Create a hash map HashMap hm = new HashMap(); // Put elements to the map hm.put("Bag", new Integer(1100)); hm.put("Sunglasses", new Integer(2000)); hm.put("Frames", new Integer(800)); hm.put("Wallet", new Integer(700)); hm.put("Belt", new Integer(600));Now, to modify the value associated with a given key, use the put() method. Here, we are modifying the value for the key “Frames” −hm.put("Frames", "900");The following is an example to modify the value associated with a given key −Example Live Demoimport java.util.*; public class Demo { public static void main(String args[]) { ... Read More
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
1K+ Views
Use the get() method to get the value associated with a given key.Here is our HashMap and its elements −// Create a hash map HashMap hm = new HashMap(); // Put elements to the map hm.put("Bag", new Integer(1100)); hm.put("Sunglasses", new Integer(2000)); hm.put("Frames", new Integer(800)); hm.put("Wallet", new Integer(700)); hm.put("Belt", new Integer(600));To get the associated value. the key is set as a parameter like this −hm.get("Frames")Above, the key is Frames and we are fetching the value associated with it.The following is an example to get the value associated with a given key in Java HashMap −Example Live Demoimport java.util.*; public class Demo { ... Read More