Hi,
I am trying to fetch value of a key from a hashset. My aim is to fetch this value and add it to another key.
Anyone has any clue how to do so??
venny
Printable View
Hi,
I am trying to fetch value of a key from a hashset. My aim is to fetch this value and add it to another key.
Anyone has any clue how to do so??
venny
HashMaps have key/value pairs.
HashSets do not.
Sorry. Here is my code:
Set<Node> nodes = new HashSet<Node>();
Map<Node, Set<Node>> edges = new HashMap<Node, Set<Node>>();
public void addEdge(int p1, MethodNode m1, ClassNode c1,
int p2, MethodNode m2, ClassNode c2) {
Node n1 = new Node(p1,m1,c1);
Node n2 = new Node(p2,m2,c2);
nodes.add(n1);
nodes.add(n2);
edges.put(n1,nodes);
My aim is to fetch the value of key n2 and add it to n1. but here in the code above edges.put(n1,nodes) (put(Key, Value) Value = nodes but i want it to be specifically equal to value of key n2.
I did some research and found it can be done by using nodes.iterator() method but am not able to implement the logic here.
Venny
Not 100% clear on what you are doing. Get the Set from the Map. Then iterate over the Set to get the value you want.
yeah thats exactly I am asking. How do i implement iterator() method to get the value.
Sorry for not being clear.
Veny
HashSet has an iterator method.
yeah..but how to get the value ? its not clear to me.
nodes.iterator ();
What next ?
You would use methods of the Iterator class to do whatever you want.
Well.. its still not clear to me. how do i get value of key n2. i know its a small logic but its just not hiting me :(
am trying my best. here is the code :
Set<Node> nodes = new HashSet<Node>();
Map<Node, Set<Node>> edges = new HashMap<Node, Set<Node>>();
public void addEdge(int p1, MethodNode m1, ClassNode c1,
int p2, MethodNode m2, ClassNode c2) {
Node n1 = new Node(p1,m1,c1);
Node n2 = new Node(p2,m2,c2);
nodes.add(n1);
nodes.add(n2);
edges.put(n1,nodes);
My aim is to fetch the value of key n2 and add it to n1. i.e to create an edge from n1 to n2. but here in the code above edges.put(n1,nodes) (put(Key, Value) Value = nodes but i want it to be specifically equal to value of key n2.
Does that makes more sense ?
Not at all.
You have a Map which ties a Node to a Set of Nodes.
Are you saying that in the above code you simply want to get the Set represented by node1 and add node2 to that Set?
Then edges.get(n1).add(n2).
But that assumes that n1 already exists in the Map, so you'd have to check for that first.
Is that Set<Node> you're using used by all keys in the Map? If so you have a problem there.