Results 1 to 13 of 13
  1. #1
    Venny is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default Getting value for key in HashSet

    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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Venny View Post
    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??
    You question is not very clear at all. You should start by restating it, giving us enough details so we can help you. Please have a look at the link in my signature links below titled "how to ask smart questions" as it will give you useful suggestions.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    HashMaps have key/value pairs.
    HashSets do not.

  4. #4
    Venny is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    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
    Last edited by Venny; 02-02-2011 at 05:20 AM.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    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.

  6. #6
    Venny is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    yeah thats exactly I am asking. How do i implement iterator() method to get the value.

    Sorry for not being clear.

    Veny

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    HashSet has an iterator method.

  8. #8
    Venny is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    yeah..but how to get the value ? its not clear to me.

    nodes.iterator ();

    What next ?

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    You would use methods of the Iterator class to do whatever you want.

  10. #10
    Venny is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    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 :(

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Quote Originally Posted by Venny View Post
    how do i get value of key n2.
    What does that mean? Since you have not fully explained what you are trying do I doubt I or anyone else can help you.

  12. #12
    Venny is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    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 ?

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default

    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

  1. Adding to a HashSet
    By Neivaed in forum New To Java
    Replies: 6
    Last Post: 12-05-2010, 06:16 PM
  2. :( anyone here plz help on HashSet
    By waklo99 in forum New To Java
    Replies: 8
    Last Post: 09-20-2010, 03:02 AM
  3. HashSet Contains problem
    By guywalder in forum Advanced Java
    Replies: 11
    Last Post: 09-01-2009, 02:48 PM
  4. HashSet anomaly
    By jon80 in forum New To Java
    Replies: 1
    Last Post: 06-21-2009, 08:22 PM
  5. Iterating through a HashSet
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-21-2008, 04:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •