Get Hashset Key

12,062

A HashSet is backed by a HashMap. I want to mention this first, because what manouti said isn't exactly true. A HashSet does have a key; you just doesn't explicitly know about the key from outside the HashSet (or rather you don't call it the key, you call it the value outside of the HashSet).

In fact, the key in the internal HashMap is the value you use in HashSet#add(E). The code for HashSet#add(E):

public boolean add(E e) {
    return map.put(e, PRESENT)==null;
}

where PRESENT is just a dummy object for the value:

private static final Object PRESENT = new Object();

What you want to do, is call the iterator for HashSet to iterate over all the keys. Per the java.util.HashSet#iterator documentation:

Returns an iterator over the elements in this set. The elements are returned in no particular order.

So this is the equivalent of getting the internal HashMap, getting the HashMap#keySet, and then getting an iterator over that. Not that it matters, but that is exactly how the internal code of HashSet actually does it:

public Iterator<E> iterator() {
    return map.keySet().iterator();
}

So that might be a little more explanation than you were looking for, but to your issue: There is no HashSet#get function, there is no HashSet#KeySet function, there is no HashMap#contains function so I recommend you read through the HashSet documentation at http://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html and HashMap documentation http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html before going any further. When in doubt, read the documentation. In Java you have the unique benefit of dealing with an API that is very, very well documented. If you choose not to use it, then its wasted.

In order to "get" anything out of a HashSet you have to have the object that results in the same hashCode anyway...so I'm not sure I quite understand the logic for what you are doing. In other words, if you already have the object, you don't need to get it from the HashSet.

Anyway, the last 6 lines of your code can be changed to this:

    for(String s: dictionary.iterator()){
        if (Combine.containsKey(s)){
            System.out.println("Dicovered " + s + " ( count " + Combine.get(s) + " )");
        }
    }
Share:
12,062
missyj
Author by

missyj

Updated on June 04, 2022

Comments

  • missyj
    missyj almost 2 years
    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.Map;
    import java.util.Set;
    
    public static void printNumWordsDiscovered( HashMap<String,Integer> vocab, HashSet<String> dictionary ) {
            HashMap <String,Integer> Combine = new HashMap <String,Integer> ();
            Iterator iterVoc = vocab.entrySet().iterator();
            List<String> Dic = new ArrayList<String>();
            int i = 0;
            double actual =  0.0;
            double token = 0.0;
            while(iterVoc.hasNext()){
                Map.Entry iterVocE = (Map.Entry)iterVoc.next();
                if (dictionary.contains(iterVocE.getKey().toString())){
    
                    int Value =  (int) iterVocE.getValue();
                    actual += 1;
                    token  += Value;
                    Combine.put(iterVocE.getKey().toString(), Value);
    
                }
            }
            for(String s: dictionary.KeySet()){
                if (Combine.contains(dictionary.get(s).toString())){
                    System.out.println("Dicovered " + dictionary.get(s) + " ( count " + Combine.get(dictionary.get(s)) + " )");
                }
            }
    }
    

    I am trying to iterate through a HashSet and I get errors concerning my .get() method. How do you get a key in a HashSet?

  • user2584621
    user2584621 over 3 years
    Because I want to be able to get by key my Node class instances that have more data than the key I can't use HashSet - I think that makes no sense.
  • searchengine27
    searchengine27 over 3 years
    You just described the usecase for a Map. Not a Set.
  • user2584621
    user2584621 over 3 years
    did go to a map, my smelly put is nodes.put(newNode, newNode);"