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:

Let us now see an example and create a HashMap in Java with string elements:

Output

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:

Output

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:

Output

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:

Output

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():

Output


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


Java HashSet
Java Inheritance
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading