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.

fuschia
  • 283
  • 2
  • 6
  • 23
  • 1
    You need to override the [`Object#toString()`](http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#toString()) method. – M A Apr 02 '15 at 19:36
  • but string is in uppercase, you have an error in the Node – xsami Apr 02 '15 at 19:39
  • @xsami sorry thats a typo here. I used String with uppercase in my program for sure. – fuschia Apr 02 '15 at 19:40
  • possible duplicate of [How do I print my Java object without getting "SomeType@2f92e0f4"?](http://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4) – Radiodef Apr 02 '15 at 19:40
  • @radiodef sorry but the point of my post, i actually tried to ask how to print one value out of multiple values in Hashmap. But thanks I'll try to read that as well – fuschia Apr 02 '15 at 19:42
  • 2
    If you are trying to ask how to access your Node's fields then you should be reviewing the tutorials. http://docs.oracle.com/javase/tutorial/java/javaOO/usingobject.html – Radiodef Apr 02 '15 at 19:44
  • Are you storing the Node instances with the key as the Node value fields? ie: `map.put("nodeName",node); map.put("year",node)`, etc?? Can you be more specific about what you expect as the output? – MadConan Apr 02 '15 at 19:45
  • Based on your last edit, why aren't you just printing the key value? – MadConan Apr 02 '15 at 19:48

4 Answers4

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.

Rahul Parida
  • 175
  • 1
  • 7
  • 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 02 '15 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. – Rahul Parida Apr 03 '15 at 04: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.

Eyal
  • 1,748
  • 2
  • 17
  • 31
1
for(String key : keySet){
   Node n = map.get(key);
   System.out.println(n.getYear());
}
MadConan
  • 3,749
  • 1
  • 16
  • 27
  • 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 02 '15 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.

Stephan
  • 41,764
  • 65
  • 238
  • 329