0

If I have a Hashmap as follows:

Hashmap<String, Node> nodeMap = new HashMap<String, Node>();

and Node stores multiple values consists of:

String name,
int year,
double weight

How can I print out one of the multiple values stored in this hashmap? I actually have no idea to just print one of the value (which is what I need the most) But as a start, I tried to print all the values first by using following query

Set<String> keySet= nodeMap.keySet();
    for(String x:keySet){
        System.out.println(nodeMap.get(x));
    }

However, I got an output for example like Node@73a28541, Node@6f75e721, Node@69222c14.

I am trying to get the real value of like what is the name, what year, and what is the weight of each key in the Hashmap but it is still not working yet.

And I actually need to know how to print just one of the value..

Any help would be really appreciated. Thank you

EDIT: This is how I stored the Hashmap and node value:

Node n = new Node(resultSet.getString(1), resultSet.getInt(2),weight);
               nodeMap.put(resultSet.getString(1),n);

My expected output is that if I have a certain key for example 123, I want to get the year value of the 123 key.

8

4 Answers 4

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

public class TestMap {
    static class Node {
        public String name;
        public int year;
        public double weight;

        public Node(String name, int year, double weight) {
            this.name = name;
            this.year = year;
            this.weight = weight;
        }

        @Override
        public String toString() {
            // here you can create your own representation of the object
            String repr = "Name:" + name + ",year:" + year + ",weight:" + weight;
            return repr;
            }
        }

    public static void main(String args[]) {
        Map<String, Node> map = new HashMap<String, Node>();
        Node node1 = new Node("A",1987,70.2);
        Node node2 = new Node("B", 2014, 66.4);
        String key1 = "123";
        String key2 = "345";
        map.put(key1,node1);
        map.put(key2,node2);

        Set<String> keySet= map.keySet();
        for(String x:keySet){
            System.out.println(map.get(x));
        }

       System.out.println(map.get(key1).name); 
    }
}

The code above should explain it all.

2
  • Thanks a lot this seems really helpful but when I tried I got the error on the last line, especially the .name part. I still do not know why it seems weird that I have an error there. I will try to use this as my solution though. Thanks
    – fuschia
    Apr 2, 2015 at 20:11
  • Your welcome @fuschia. My code doesn't seem to throw any error when I run it. If you could tell me what the exact error is or even better show me your code, I could help further. Apr 3, 2015 at 4:30
2

In the class Node, override the function toString which will be called when printing a node, you can choose how the printing will appear.

1
for(String key : keySet){
   Node n = map.get(key);
   System.out.println(n.getYear());
}
1
  • I have a problem here that the map.get(key) can't be assigned to Node n. Thanks though I will try to see if there is something wrong with my program
    – fuschia
    Apr 2, 2015 at 19:53
1

Since the nodeMap.get method is called, the entrySet method should be use instead of keySet.

Here is a little comparison of the use of the two methods:

// Create Map instance and populate it
Map<String, Node> nodeMap = new HashMap<String, Node>();
for (int i = 0; i < 100; i++) {
    String tmp = Integer.toString(i);
    nodeMap.put(tmp, new Node(tmp, 2015, 3.0));
}

// Test 1: keySet + get
long t1 = System.nanoTime();
for (String x : nodeMap.keySet()) {
    nodeMap.get(x);
}
System.out.format("keySet + get: %d ns\n" , System.nanoTime() - t1);

// Test 2: entrySet + getValue
t1 = System.nanoTime();
for (Map.Entry<String, Node> e : nodeMap.entrySet()) {
    e.getValue();
}
System.out.format("entrySet + getValue: %d ns\n" , System.nanoTime() - t1);

Output

keySet + get: 384464 ns
entrySet + getValue: 118813 ns

I ran repeatedly this test. On average, entrySet + getValue is twice faster than keySet + get.

1
  • Thanks so much this is really helpful since I am gonna process a very big dataset using it.
    – fuschia
    Apr 3, 2015 at 8:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.