Reputation: 283
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.
Upvotes: 0
Views: 3081
Reputation: 43013
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);
keySet + get: 384464 ns
entrySet + getValue: 118813 ns
I ran repeatedly this test. On average, entrySet + getValue
is twice faster than keySet + get
.
Upvotes: 1
Reputation: 175
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.
Upvotes: 3
Reputation: 3767
for(String key : keySet){
Node n = map.get(key);
System.out.println(n.getYear());
}
Upvotes: 1
Reputation: 1768
In the class Node, override the function toString which will be called when printing a node, you can choose how the printing will appear.
Upvotes: 2