Print Hashmap In Java

In this tutorial we will print hashmap in java using various methods . Let’s see different methods for printing hashmap in java steps by steps .

Print HashMap Elements in Java

We see how to print hashmap in java using System.out.println() . We create map using constructor new Hashmap() , then use map put method to add element into java hashmap . After adding element in java hashmap we give reference of hashmap to println() method . It println() method print a map in java as key-value pairs inside curly braces as below .


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

public class PrintHashMap {
	
	public static void main(String[] args) {
				Map<Integer, String> map = new HashMap<>();
				map.put(10, "php");
				map.put(20, "java");
				map.put(30, "mysql");
				System.out.println(map);
		
	}

}

Output:

{20=java, 10=php, 30=mysql}

Print HashMap using for method with keyset()

Now we are printing a map java using keySet() .In this example we see , how to get key of hashmap java also. KeySet() returns a Set view of the keys contained in this map , we iterate this set using for loop .  map.get(key) method return the value to which the specified key is mapped in this map .

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

public class PrintHashMapUsingKeySet {
	public static void main(String[] args) {
		Map<Integer, String> map = new HashMap<>();
		map.put(10, "php");
		map.put(20, "java");
		map.put(30, "mysql");
		for (Integer key: map.keySet()){  
			System.out.println(key+ " = " + map.get(key));
		}
   }
}

Output :

20 = java
10 = php
30 = mysql

 

Print Hashmap in Java Using forEach()

From java8 , we can use forEach to printing hashmap in java . getKey() method return key from hashmap entryset and getValue() method return values mapped with key in hashmap entrySet .

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

public class PrintHashMapUsingforEach {
	
	public static void main(String[] args) {
		Map<Integer, String> map = new HashMap<>();
		map.put(10, "php");
		map.put(20, "java");
		map.put(30, "mysql");
		
		map.entrySet().forEach(entry->{
			System.out.println(entry.getKey() + " = " + entry.getValue());  
		}); 
       		
   }

}

Output :

20 = java
10 = php
30 = mysql

Printing Hashmap In Java Using Arrays.asList()

We can print java hashmap element using Arrays.asList() method . asList(map) method retrun list representation of hashmap in java.

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

public class PrintHashMapUsingArrays {

	public static void main(String[] args) {
		Map<Integer, String> map = new HashMap<>();
		map.put(10, "php");
		map.put(20, "java");
		map.put(30, "mysql");
		System.out.println(Arrays.asList(map));
   }
	
}

Output :

[{20=java, 10=php, 30=mysql}]

Print HashMap In Java  Using Collections Class

To print out the elements of a HashMap we use static method singletonList() of collections class .  Here singletonList() method Returns an immutable list containing hashmap .

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

public class PrintHashMapUsingCollections {

	public static void main(String[] args) {
		Map<Integer, String> map = new HashMap<>();
		map.put(10, "php");
		map.put(20, "java");
		map.put(30, "mysql");
		System.out.println(Collections.singletonList(map));
   }
	
}

Output:

[{20=java, 10=php, 30=mysql}]

Printing HashMap In Java Using the entrySet()

Java map entryset return entries from map , there entry can iterate using for loop . getKey() method return key from entry and getValue() method return value mapped to key in this entry .

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

public class PrintHashMapUsingentrySet {

	public static void main(String[] args) {
		Map<Integer, String> map = new HashMap<>();
		map.put(10, "php");
		map.put(20, "java");
		map.put(30, "mysql");
		for (Map.Entry<Integer, String> entry : map.entrySet()) {
		    System.out.println(entry.getKey()+" = "+entry.getValue());
		}
   }
	
}

Output:

20 = java
10 = php
30 = mysql

Print HashMap Elements Using values() and keySet() Method in Java

Printing a map java we can use values() and keySet() method . java map values() method return list of values in this map  Java map keyset() method return list of  key in map .

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

public class PrintHashMapUsingValues {

	public static void main(String[] args) {
		Map<Integer, String> map = new HashMap<>();
		map.put(10, "php");
		map.put(20, "java");
		map.put(30, "mysql");
		System.out.println(map.values());
		System.out.println(map.keySet());
   }
	
}

Output:

[java, php, mysql]
[20, 10, 30]

Print HashMap In Java Using Biconsumer

Printing hashmap elements we can use the Biconsumer. Biconsumer is interface in Java that can be used to print HashMap in java using the lambda expression. See the example below.

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

public class PrintHashMapUsingBiconsumer {

	public static void main(String[] args) {
		Map<Integer, String> map = new HashMap<>();
		map.put(10, "php");
		map.put(20, "java");
		map.put(30, "mysql");   
		BiConsumer<Integer, String> biconsumer = (key, val) -> 
		System.out.println(key + " = " + val);
		map.forEach(biconsumer);
	
   }
	
}

Output:

20 = java
10 = php
30 = mysql

We learned in this tutorials various method to printing hashmap in java . You can see more java hashmap example with easy explanation .

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

6 + 3 =