1

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.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
ihoaxed
  • 127
  • 2
  • 16
  • 1
    Why don't you iterate through the Map and print it whatever way you want? – Atri Jan 10 '16 at 05:29
  • 1
    Possible duplicate of [Iterate through a HashMap](http://stackoverflow.com/questions/1066589/iterate-through-a-hashmap) – Atri Jan 10 '16 at 05:30

3 Answers3

1
for (String key : dataMap.keySet()) {
    System.out.println(key + " = " + dataMap.get(key));
}
Rahin
  • 943
  • 6
  • 21
  • Probably better to iterate using `entrySet()`, to prevent the overhead of all those `get()` calls. – Andreas Jan 10 '16 at 05:32
  • Wow, Rahin, your code works flawlessly. But @Andreas, can you go into more depth why I should use entrySet() instead? What potential problems can I run into if I use Rahin's code? – ihoaxed Jan 10 '16 at 05:39
  • You won't run into problems in terms of correctness (i.e. you'll always have the right output). However, get() calls have the potential to be expensive. Usually, a get takes O(1) time, but can take up to O(n) in the absolute worst case. By using a solution that uses entrySet(), you're no longer calling .get(key) on the map, and potentially avoiding the O(n) worst case for a particular get operation. For best practices, I agree, iterate over entrySet(), but you probably won't notice anything in terms of performance – Rahin Jan 10 '16 at 05:47
  • @ihoaxed No problems, but using `entrySet()`, like in answer by Dinil, is retrieving the value without doing a map lookup. Although map lookups are very fast, not doing them is faster. You probably won't notice a different, so it's a minor point. – Andreas Jan 10 '16 at 05:47
  • I see. Thanks for the great response guys. This really helps. Also, @Rahin, what do you mean by "O(1)" and "O(n)"? I'm self teaching myself Java and am pretty new to all this! – ihoaxed Jan 10 '16 at 05:51
  • @ihoaxed O(some math function) is Big-O notation. It's not Java specific, but a mathematical concept applied to Computer Science to analyze the runtime of a snippet of code. I would recommend reading the wikipedia page on it to grasp an understanding of it mathematically, and https://www.interviewcake.com/article/java/big-o-notation-time-and-space-complexity to see how it's used to analyze code – Rahin Jan 10 '16 at 06:01
0
    Iterator it = dataMap.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry pair = (Map.Entry) it.next();
        System.out.println(pair.getKey() + " = " + pair.getValue());
    }
R.Costa
  • 1,395
  • 9
  • 16
0
    for (Entry<String, Integer> entry : dataMap.entrySet()) {
        System.out.println(entry.getKey() + " = " + entry.getValue());
    } 

If you want the output in the order of insertion, use LinkedHashMap.

Updated for TreeMap sample:

    Map<String, Integer> dataMap = new TreeMap<String, Integer>();
    dataMap.put("D", 4);
    dataMap.put("B", 2);
    dataMap.put("F", 6);
    dataMap.put("E", 5);
    dataMap.put("C", 3);
    dataMap.put("G", 7);
    dataMap.put("A", 1);


    for (Entry<String, Integer> entry : dataMap.entrySet()) {
        System.out.println(entry.getKey() + " = " +        entry.getValue());
    } 
Dinil
  • 66
  • 8