user2094103
user2094103

Reputation: 735

Retrieving key from value in hashtable in Java

I am a new bie to the world of collections but still, In my below class I have hashtable as I have to choose hashtable and I want to retrieve the key basis on the value , below is my class..

public class KeyFromValueExample
 {
public static void main(String args[]) 
{
Hashtable table = new Hashtable();
        table.put("Sony", "Bravia");
        table.put("Samsung", "Galaxy");
        table.put("Nokia", "Lumia");
System.out.println("does hash table has Lumia as value : " + table.containsValue("Lumia"));
        System.out.println("does hash table Lumia as key : " + table.containsKey("Lumia"));

        //finding key corresponding to value in hashtable - one to one mapping
        String key= null;
        String value="Lumia";
        for(Map.Entry entry: table.entrySet()){
            if(value.equals(entry.getValue())){
                key = entry.getKey();
                break; //breaking because its one to one map
            }
        }
        System.out.println("got key from value in hashtable key:  "+ key +" value: " + value);

//finding key corresponding to value in hashtable - one to many mapping
        table.put("HTC", "Lumia");
        Set keys = new HashSet();

        for(Map.Entry entry: table.entrySet()){
            if(value.equals(entry.getValue())){
                keys.add(entry.getKey()); //no break, looping entire hashtable
            }
        }
        System.out.println("keys : " + keys +" corresponding to value in hash table:  "+ value);

Output :-

does hash table has Lumia as value : true
does hash table has Lumia as key : false
got key from value in hashtable key:  Nokia value: Lumia
keys : [Nokia, HTC] corresponding to value in hash talbe:  Lumia

Now please advis is there any other better way to achieve the same thing, Please advise if other better alternatives are there.

Upvotes: 2

Views: 11557

Answers (4)

elsa
elsa

Reputation: 165

Is there  a way to find the key of a particular value in HashTable without iterator??
like String getvalue(key); ??

for(Map.Entry entry: table.entrySet()){
            if(value.equals(entry.getValue())){
                keys.add(entry.getKey()); //no break, looping entire hashtable
            }
        }

Upvotes: 0

SDJMcHattie
SDJMcHattie

Reputation: 1699

That looks pretty obvious what it's doing to me. I'm not sure there's a more efficient way of doing it, but perhaps if you need to look up values to find the keys quite regularly, then the type of map you are using is wrong. I would suggest you turn the values into keys and make a Map<String, Set<String>> instead

Upvotes: 2

mkirci
mkirci

Reputation: 159

I do not know if it is what you want, but why don't you reverse your key and values? So, you can use

table.put("Lumia", new List<String>("HTC", "Nokia")); 

something like that if above is legal.

Upvotes: 0

Alexander Pogrebnyak
Alexander Pogrebnyak

Reputation: 45576

If your map has both unique keys and values, consider using BiMap from Guava collections -> http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/BiMap.html

With that library, you can just use simple containsKey and containsValue tests.

Upvotes: 1

Related Questions