JIYIK CN >

Current Location:Home > Learning > PROGRAM > Java >

How to Print a HashMap in Java

Author:JIYIK Last Updated:2025/04/14 Views:

This tutorial explains how to print HashMapan element in Java and also provides some sample code to understand the topic.

HashMapIt is Mapan implementation class of the interface and is used to collect elements into key and value pairs. We can use various methods to print its elements. For example, keySet()the print method, values()the print method, entrySet()the print method, the asList()print method, etc. Let's see some examples.


HashMapPrinting Elements in Java

HashMapThis is the simplest way to print in Java . Just HashMappass the reference of to println()the method and it will print the key-value pairs inside the curly braces. See the following example.

import java.util.HashMap;
import java.util.Map;

public class SimpleTesting {
  public static void main(String[] args) {
    Map<Integer, String> map = new HashMap<>();
    map.put(10, "Ten");
    map.put(100, "Hundred");
    map.put(1000, "Thousand");
    System.out.println(map);
  }
}

Output:

{100=Hundred, 1000=Thousand, 10=Ten}

Printing elements using keySet()method in JavaHashMap

We can use keySet()the method to get a set of keys and then foruse get()the method in a loop to get the values. get()The method returns the value associated with the passed key. See the following example.

import java.util.HashMap;
import java.util.Map;

public class SimpleTesting {
  public static void main(String[] args) {
    Map<Integer, String> map = new HashMap<>();
    map.put(10, "Ten");
    map.put(100, "Hundred");
    map.put(1000, "Thousand");
    for (Integer key : map.keySet()) {
      System.out.println(key + " = " + map.get(key));
    }
  }
}

Output:

100 = Hundred
1000 = Thousand
10 = Ten

Printing elements using forEach()method in JavaHashMap

From Java 8 onwards, we can use forEach()the print method to print the elements of a with the help of getKey()and getValue()print methods HashMap. getKey()The print method entrySetreturns a key from a and getValue()the print method returns the value associated with that key. See the following example.

import java.util.HashMap;
import java.util.Map;

public class SimpleTesting {
  public static void main(String[] args) {
    Map<Integer, String> map = new HashMap<>();
    map.put(10, "Ten");
    map.put(100, "Hundred");
    map.put(1000, "Thousand");
    map.entrySet().forEach(
        entry -> { System.out.println(entry.getKey() + " = " + entry.getValue()); });
  }
}

Using the Arrays.asList()Print HashMapelement in Java

We can use Arrays.asList()the method to print HashMapthe elements of . asList()The method returns HashMapthe list form of . See the following example.

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class SimpleTesting {
  public static void main(String[] args) {
    Map<Integer, String> map = new HashMap<>();
    map.put(10, "Ten");
    map.put(100, "Hundred");
    map.put(1000, "Thousand");
    System.out.println(Arrays.asList(map));
  }
}

Output:

[{100=Hundred, 1000=Thousand, 10=Ten}]

CollectionsPrinting HashMapan element in Java using the class

We can use Collections.singletonList()the static method to print HashMapthe elements of . singletonList()The method returns HashMapthe list representation of . See the following example.

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class SimpleTesting {
  public static void main(String[] args) {
    Map<Integer, String> map = new HashMap<>();
    map.put(10, "Ten");
    map.put(100, "Hundred");
    map.put(1000, "Thousand");
    System.out.println(Collections.singletonList(map));
  }
}

Output:

[{100=Hundred, 1000=Thousand, 10=Ten}]

Printing elements using entrySet()method in JavaHashMap

entrySet()The method returns a set of items that can be used in a for loop to print HashMapelements. See the following example.

import java.util.HashMap;
import java.util.Map;

public class SimpleTesting {
  public static void main(String[] args) {
    Map<Integer, String> map = new HashMap<>();
    map.put(10, "Ten");
    map.put(100, "Hundred");
    map.put(1000, "Thousand");
    for (Map.Entry<Integer, String> entry : map.entrySet()) {
      System.out.println(entry.getKey() + " = " + entry.getValue());
    }
  }
}

Output:

100 = Hundred
1000 = Thousand
10 = Ten

Printing elements using values()and keySet()methods in JavaHashMap

If we want to print the values ​​and keys separately, we can use the values()and keySet()methods. values()The method returns a list of all values, while keySet()the method returns a list of all HashMapkeys. See the following example.

import java.util.HashMap;
import java.util.Map;

public class SimpleTesting {
  public static void main(String[] args) {
    Map<Integer, String> map = new HashMap<>();
    map.put(10, "Ten");
    map.put(100, "Hundred");
    map.put(1000, "Thousand");
    System.out.println(map.values());
    System.out.println(map.keySet());
  }
}

Output:

[Hundred, Thousand, Ten]
[100, 1000, 10]

Using the BiconsumerPrint HashMapelement in Java

BiconsumerIs an interface in Java that can be used to lambdaprint HashMapan element using an expression. See the following example.

import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;

public class SimpleTesting {
  public static void main(String[] args) {
    Map<Integer, String> map = new HashMap<>();
    map.put(10, "Ten");
    map.put(100, "Hundred");
    map.put(1000, "Thousand");
    BiConsumer<Integer, String> biconsumer = (key, val) -> System.out.println(key + " = " + val);
    map.forEach(biconsumer);
  }
}

Output:

100 = Hundred
1000 = Thousand
10 = Ten

Previous:Updating Hashmap Values in Java

Next: None

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Updating Hashmap Values in Java

Publish Date:2025/04/14 Views:88 Category:Java

This article explains how to use the two methods contained in the HashMap class in Java - update put() and replace() update the values ​​in a HashMap. hashmap.put() Update the value in Hashmap in Java using When we want to HashMap inser

Difference between hashmap and map in Java

Publish Date:2025/04/14 Views:193 Category:Java

This tutorial explains the key differences between Map and in Java HashMap . In Java, Map is an interface for storing data in key-value pairs, and HashMap is Map an implementation class of the interface. Java has several classes ( TreeHashM

Get user home directory in Java

Publish Date:2025/04/14 Views:161 Category:Java

This tutorial explains how to get the user's home directory in Java and lists some sample code to guide you through the topic. For a multi-user operating system, each user has a file system directory; this directory is called the user's hom

Difference between size and length in Java

Publish Date:2025/04/14 Views:114 Category:Java

This tutorial explains the difference between size and length in Java. We have also listed some sample codes to help you understand the topic. Java has a size() method and a length property. Beginners may think that they are interchangeable

Mutex Locks in Java

Publish Date:2025/04/14 Views:198 Category:Java

In the field of computer science, mutual exclusion or mutex is known as the property of concurrency control. Every computer uses a minimum sequence of program instructions called a thread. At a time, the computer works on one thread. For be

How to compare characters for equality in Java

Publish Date:2025/04/14 Views:128 Category:Java

This tutorial shows you how to check if two characters are equal in Java. In Java, we can use the equals( == ) operator or the Character compare() equals() method of the Character class to compare two characters. If you are working with pri

Compile multiple Java files with a single command in Java

Publish Date:2025/04/14 Views:192 Category:Java

This tutorial explains how to compile multiple java files using a single command in Java. Compilation is a term used to refer to the process of converting java source code into bytecode using JDK. To execute any Java file, we need to follow

Arrow operator in Java ->

Publish Date:2025/04/14 Views:113 Category:Java

This tutorial explains - the role of the arrow operator ( ) in Java and lists some sample code to understand the topic. In Java 8, a new feature lambda expression was added, and the arrow operator appeared in Java to form lambda expressions

>> operator in Java

Publish Date:2025/04/14 Views:66 Category:Java

This guide will introduce you to the operator in Java . To understand this concept, you need to be familiar with some lower-level computing concepts. For example, bits, bytes, etc. Let's take a deeper look. Operators in Java In Java, the op

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial