Pages

Saturday, November 20, 2021

Java Print HashMap - Displaying Values

1. Overview

In this article, we'll learn how to print the values of HashMap in different ways in java and jdk 8. 
Java Print HashMap - Displaying Values


2. Java Print Hashmap values using Map reference


This is a very simple option to display the key and values onto the console using System.out.println() statement.

Example 1:

In the below program, print the key and its values of the map.
package com.javaprogramto.collections.hashmap.print;

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

public class HashMapPrintExample1 {

	public static void main(String[] args) {
		
		Map<Integer, String> map = new HashMap<>();
		
		map.put(1, "one");
		map.put(2, "two");
		map.put(3, "three");

		System.out.println("map values : "+map);
	}
}

Output:
map values : {1=one, 2=two, 3=three}

3. Java 8 Print Hashmap values using Iterator


Call the iterator() on the entrySet() method. entrySet() returns Entry<Integer, String> objects as Set.

Next, run forEachRemaining() method of Iterator from java 8.

Example 2:
package com.javaprogramto.collections.hashmap.print;

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

public class HashMapPrintExample2 {

	public static void main(String[] args) {

		Map<Integer, String> map = new HashMap<>();

		map.put(1, "one");
		map.put(2, "two");
		map.put(3, "three");

		Iterator<Map.Entry<Integer, String>> it = map.entrySet().iterator();
		
		it.forEachRemaining( entry -> System.out.println(entry.getKey() + " - "+entry.getValue()));
	}
}

Output:
1 - one
2 - two
3 - three

4. Java Print Hashmap values using entrySet()


Get the Set of Entry object using Map.entrySet() method and then call foreach() method.

Example 3:
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class HashMapPrintExample3 {

	public static void main(String[] args) {

		Map<Integer, String> map = new HashMap<>();

		map.put(1, "one");
		map.put(2, "two");
		map.put(3, "three");

		Set<Map.Entry<Integer, String>> set = map.entrySet();
		
		set.forEach(System.out::println);

	}
}
Output:
1=one
2=two
3=three

5. Java Print Hashmap values using keySet()


Invoke keySet() method and loop it through for loop to print the values.

Example 4:
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class HashMapPrintExample4 {

	public static void main(String[] args) {

		Map<Integer, String> map = new HashMap<>();

		map.put(1, "one");
		map.put(2, "two");
		map.put(3, "three");

		Set<Integer> keys = map.keySet();
		
		for(Integer key : keys) {
			System.out.println(key+" - "+map.get(key));
		}

	}
}
Output:
1 - one
2 - two
3 - three

6. Java 8 Print Hashmap values using BiConsumer


Java 8 is added with the BiConsumer functional interface and pass the biconsumer to the forEach() method.

Example 5:
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;

public class HashMapPrintExample5 {

	public static void main(String[] args) {

		Map<Integer, String> map = new HashMap<>();

		map.put(1, "one");
		map.put(2, "two");
		map.put(3, "three");

		BiConsumer<Integer, String> printBiConsumer = (key, value) -> System.out.println(key+" - "+value);
		
		map.forEach(printBiConsumer);
	}
}
Output is the same as the above section.

7. Conclusion


In this article, we've seen what are the different ways to print the hashmap values in java 8 and older versions.





No comments:

Post a Comment

Please do not add any spam links in the comments section.