Search code examples
javahashmappretty-print

Vertically print a hashmap


I'm pretty new at Java and feel pretty blanked out right now.

I'm making a program that uses Apachi POI to get data from an excel document, and prints the results.

I've created a hashmap that uses a 'for' loop that iterates through an excel sheet, and stores the values in it.

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

When the loop is done running, I run the following code to print the results:

System.out.println(dataMap);

The results print out in this format:

{A=1, Q=15, R=35, D=98, X=60, Y=21, N=57}

Is there a way I can print my hashmap out so it looks more neatly organized, like a list? Kind of like this:

A = 1
Q = 15
R = 35
D = 98
X = 60
Y = 21
N = 57

I couldn't find a solution to this problem. If someone can please show me step by step on how to print this code out the way I want it to, that would be awesome.


Solution

  • for (String key : dataMap.keySet()) {
        System.out.println(key + " = " + dataMap.get(key));
    }