• Skip to main content
  • Skip to after header navigation
  • Skip to site footer

Free-Mail

  • Events
  • Images
    • Instagram Goodness
  • Music
  • News
  • Software

Use a hashmap in Java

Use a hashmap in Java: Use a hashmap in Java Seth Kenlon Sun, 11/13/2022 – 03:00

In the Java programming language, a hashmap is a list of associated values. Java uses hashmaps to store data. If you keep a lot of structured data, it’s useful to know the various retrieval methods available.

More on Java
What is enterprise Java programming?
An open source alternative to Oracle JDK
Java cheat sheet
Red Hat build of OpenJDK
Free online course: Developing cloud-native applications with microservices
Fresh Java articles

Create a hashmap

Create a hashmap in Java by importing and using the HashMap class. When you create a hashmap, you must define what constitutes a valid value (such as Integer or String). A hashmap holds pairs of values.

 
package com.opensource.example;

import java.util.HashMap;

public class Mapping {
    public static void main(String[] args) {
        HashMap<String, String> myMap = new HashMap<>();
        myMap.put("foo", "hello");
        myMap.put("bar", "world");

        System.out.println(myMap.get("foo") + " " + myMap.get("bar"));
        System.out.println(myMap.get("hello") + " " + myMap.get("world"));
    }
}

The put method allows you to add data to your hashmap, and the get method retrieves data from the hashmap.

Run the code to see the output. Assuming you’ve saved the code in a file called main.java, you can run it directly with the java command:

 
$ java ./main.java
hello world
null null

Calling the second values in the hashmap returns null, so the first value is essentially a key and the second a value. This is known as a dictionary or associative array in some languages.

You can mix and match the types of data you put into a hashmap as long as you tell Java what to expect when creating it:

 
package com.opensource.example;

import java.util.HashMap;

public class Mapping {
    public static void main(String[] args) {
        HashMap<Integer, String> myMap = new HashMap<>();
        myMap.put(71, "zombie");
        myMap.put(2066, "apocalypse");

        System.out.println(myMap.get(71) + " " + myMap.get(2066));
    }
}

Run the code:

 
$ java ./main.java
zombie apocalypse

Iterate over a hashmap with forEach

There are many ways to retrieve all data pairs in a hashmap, but the most direct method is a forEach loop:

 
package com.opensource.example;

import java.util.HashMap;

public class Mapping {

    public static void main(String[] args) {
        HashMap<String, String> myMap = new HashMap<>();
        myMap.put("foo", "hello");
        myMap.put("bar", "world");

        // retrieval
        myMap.forEach( (key, value) ->
                System.out.println(key + ": " + value));
    }
}

Run the code:

 
$ java ./main.java
bar: world
foo: hello

Structured data

Sometimes it makes sense to use a Java hashmap so you don’t have to keep track of dozens of individual variables. Once you understand how to structure and retrieve data in a language, you’re empowered to generate complex data in an organized and convenient way. A hashmap isn’t the only data structure you’ll ever need in Java, but it’s a great one for related data.

A hashmap is a useful way to store structured data in Java.

Coffee
Image by:

Photo by Nathan Dumlao, Unsplash

Java

What to read next
Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.
Register or Login to post a comment.

: Opensource.com Seth Kenlon

Supporting Open Source.

Have you tried: Farking?

Share this:

  • Share on Facebook (Opens in new window) Facebook
  • Share on X (Opens in new window) X
  • Share on Pinterest (Opens in new window) Pinterest
  • Email a link to a friend (Opens in new window) Email
  • More
  • Share on Tumblr (Opens in new window) Tumblr
  • Print (Opens in new window) Print
  • Share on LinkedIn (Opens in new window) LinkedIn
  • Share on Reddit (Opens in new window) Reddit
Published on 13 November 2022 by Alan Category: SoftwareTag: hashmap, java

SDC19: Samsung Advances Experience Innovation, in Collaboration with Partners and Developers

SDC19: Samsung Advances Experience Innovation, in Collaboration with Partners and Developers: …

Read moreSDC19: Samsung Advances Experience Innovation, in Collaboration with Partners and Developers

Erasmus will ‘beg’ for Lions to play second South Africa A game this weekend

South Africa in the news: Erasmus will ‘beg’ for Lions to play second South Africa A …

Read moreErasmus will ‘beg’ for Lions to play second South Africa A game this weekend

DSCF1805

A nice WordPress image: DSCF1805 Image by Pablo Moratinos Image automatically curated and published …

Read moreDSCF1805

About Alan

Previous Post:Nice Untitled photo
Next Post:Trending Now: PSG vs Auxerre

Reach Trust

Reach Trust is a holding trust for a number of companies within the Stratlec Group.

Copyright © 2026 · Free-Mail · All Rights Reserved · Powered by Straton Electrical

Back to top