21 Jul Java HashMap
The HashMap is a Collections class in Java. Creating a HashMap requires key/value pairs. It is a part of the java.util package i.e., you need to import the java.util.HashMap package.
Let us see some LinkedList operation/ methods with examples:
- Create a HashMap
- Size of a HashMap
- Display Values from a HashMap
- Display Keys from a HashMap
- Remove an item from a HashSMap
Let us first see how to create a HashMap in Java.
Create a HashMap
To create a HashMap with string key and value pair, use <String, String>. This creates String keys and String values. Set the type in angular braces i.e., use the wrapper class:
1 2 3 |
HashMap<String, String> myMap = new HashSet <String, String>(); |
Let us now see an example and create a HashMap in Java with string elements:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.HashMap; class Studyopedia { public static void main(String[] args) { // HashMap with key value pairs HashMap<String, String> player = new HashMap<String, String>(); player.put("Virat", "India"); player.put("Steve", "Australia"); player.put("Joe", "England"); player.put("Kane", "New Zealand"); player.put("Karunaratne", "Sri Lanka"); player.put("Litton", "Bangladesh"); System.out.println(player); } } |
Output
1 2 3 |
{Joe=England, Steve=Australia, Kane=New Zealand, Virat=India, Karunaratne=Sri Lanka} |
Size of a HashMap
To get the size of HashMap i.e., how many elements does it have, use the size() method. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.HashMap; class Studyopedia { public static void main(String[] args) { // HashMap with key value pairs HashMap<String, String> player = new HashMap<String, String>(); player.put("Virat", "India"); player.put("Steve", "Australia"); player.put("Joe", "England"); player.put("Kane", "New Zealand"); player.put("Karunaratne", "Sri Lanka"); System.out.println(player); // Size of the HashMap System.out.println("\nHashMap Size = "+player.size()); } } |
Output
1 2 3 4 5 |
{Joe=England, Steve=Australia, Kane=New Zealand, Virat=India, Karunaratne=Sri Lanka} HashMap Size = 5 |
Display Values from a HashMap
The values() method is used to display only values from the key/value pair of a HashMap in Java. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import java.util.HashMap; class Studyopedia { public static void main(String[] args) { // HashMap with key value pairs HashMap<String, String> player = new HashMap<String, String>(); player.put("Virat", "India"); player.put("Steve", "Australia"); player.put("Joe", "England"); player.put("Kane", "New Zealand"); player.put("Karunaratne", "Sri Lanka"); System.out.println(player); // Display values System.out.println("\nValues...(Country Name)"); for (String s : player.values()) { System.out.println(s); } } } |
Output
1 2 3 4 5 6 7 8 9 10 |
{Joe=England, Steve=Australia, Kane=New Zealand, Virat=India, Karunaratne=Sri Lanka} Values...(Country Name) England Australia New Zealand India Sri Lanka |
Display Keys from a HashMap
The keySet() method is used to display only keys from the key/value pair of a HashMap in Java. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import java.util.HashMap; class Studyopedia { public static void main(String[] args) { // HashMap with key value pairs HashMap<String, String> player = new HashMap<String, String>(); player.put("Virat", "India"); player.put("Steve", "Australia"); player.put("Joe", "England"); player.put("Kane", "New Zealand"); player.put("Karunaratne", "Sri Lanka"); System.out.println(player); // Display values System.out.println("\nKeys...(Player Name)"); for (String s : player.keySet()) { System.out.println(s); } } } |
Output
1 2 3 4 5 6 7 8 9 10 |
{Joe=England, Steve=Australia, Kane=New Zealand, Virat=India, Karunaratne=Sri Lanka} Keys...(Player Name) Joe Steve Kane Virat Karunaratne |
Remove an item from a HashMap
Use the remove() method to remove an item from a HashMap. For the key/value pair, you want to remove, set only the key as a parameter of the remove():
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import java.util.HashMap; class Studyopedia { public static void main(String[] args) { // HashMap with key value pairs HashMap<String, String> player = new HashMap<String, String>(); player.put("Virat", "India"); player.put("Steve", "Australia"); player.put("Joe", "England"); player.put("Kane", "New Zealand"); player.put("Karunaratne", "Sri Lanka"); // Display the HashMap System.out.println(player); // Remove an item player.remove("Kane"); // Updated HashMap System.out.println("\nUpdated HashMap after removing an item =\n"+player); } } |
Output
1 2 3 4 5 6 |
{Joe=England, Steve=Australia, Kane=New Zealand, Virat=India, Karunaratne=Sri Lanka} Updated HashMap after removing an item = {Joe=England, Steve=Australia, Virat=India, Karunaratne=Sri Lanka} |
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.
For Videos, Join Our YouTube Channel: Join Now
No Comments