Results 1 to 13 of 13
Thread: Getting value for key in HashSet
- 02-02-2011, 03:27 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 24
- Rep Power
- 0
-
- 02-02-2011, 04:12 AM #3
HashMaps have key/value pairs.
HashSets do not.
- 02-02-2011, 04:57 AM #4
Member
- Join Date
- Jan 2011
- Posts
- 24
- Rep Power
- 0
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.
VennyLast edited by Venny; 02-02-2011 at 05:20 AM.
- 02-02-2011, 05:07 AM #5
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.
- 02-02-2011, 05:22 AM #6
Member
- Join Date
- Jan 2011
- Posts
- 24
- Rep Power
- 0
yeah thats exactly I am asking. How do i implement iterator() method to get the value.
Sorry for not being clear.
Veny
- 02-02-2011, 05:25 AM #7
HashSet has an iterator method.
- 02-02-2011, 05:30 AM #8
Member
- Join Date
- Jan 2011
- Posts
- 24
- Rep Power
- 0
yeah..but how to get the value ? its not clear to me.
nodes.iterator ();
What next ?
- 02-02-2011, 05:35 AM #9
You would use methods of the Iterator class to do whatever you want.
- 02-02-2011, 05:40 AM #10
Member
- Join Date
- Jan 2011
- Posts
- 24
- Rep Power
- 0
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 :(
- 02-02-2011, 05:43 AM #11
- 02-02-2011, 05:51 AM #12
Member
- Join Date
- Jan 2011
- Posts
- 24
- Rep Power
- 0
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 ?
- 02-02-2011, 08:42 AM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
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.
Similar Threads
-
Adding to a HashSet
By Neivaed in forum New To JavaReplies: 6Last Post: 12-05-2010, 06:16 PM -
:( anyone here plz help on HashSet
By waklo99 in forum New To JavaReplies: 8Last Post: 09-20-2010, 03:02 AM -
HashSet Contains problem
By guywalder in forum Advanced JavaReplies: 11Last Post: 09-01-2009, 02:48 PM -
HashSet anomaly
By jon80 in forum New To JavaReplies: 1Last Post: 06-21-2009, 08:22 PM -
Iterating through a HashSet
By Java Tip in forum Java TipReplies: 0Last Post: 01-21-2008, 04:34 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks