How To Decease Primal From Value Inwards Hashtable, Hashmap Or Map Inwards Java

It's non slow to acquire key from value inwards Hashtable or HashMap, every bit compared to getting value from key because HashMap or Hashtable doesn't enforce i to i mapping betwixt key as well as value within Map inwards Java. inwards fact, Map allows same value to endure mapped to multiple keys inside HashMap, Hashtable or any other Map implementation. What y'all accept inwards your kitty is Hashtable.containsValue(String value) or Hashtable.containsKey(String key) to banking concern check whether key or value exists inwards Hashtable or not, but sometimes nosotros desire to remember a value from Map corresponding to whatsoever key as well as at that topographic point is no API method to produce inwards Map. We tin all the same produce this, but it highly depends information inwards your Map because Hashtable as well as HashMap both allows duplicate values mapped to the dissimilar key. In this Java tutorial, nosotros volition come across an event of how to acquire key from value inwards Hashtable inwards Java.

Java - Key from Value inwards Map

s non slow to acquire key from value inwards Hashtable or HashMap How to acquire Key From Value inwards Hashtable, HashMap or Map inwards JavaThere are essentially ii ways to detect a key from values inwards Map, i is without using whatsoever tertiary political party library, as well as other is using tertiary political party libraries similar Google collection or common collections which render bi-directional Maps

Though close of the fourth dimension projects already role  these utility libraries, as well as thus it's meliorate to role them if y'all already using it; but but because y'all ask to detect key  from value inwards Hashtable, adding novel dependency doesn't brand sense, peculiarly if y'all tin all the same produce this yesteryear writing a business office as well as iterating over Map inwards Java. 


In adjacent section, nosotros volition code event of retrieving the value from the key inwards a hashtable likewise every bit inwards hashmap.

Key from Value Hashtable HashMap Example

import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Map;
import java.util.Set;

/**
 * Java programme to acquire key from value inwards Java HashMap, Hashtable as well as Map.
 *
 * @author java67
 */
public cast KeyFromValueExample {

public static void main(String args[]) {
       
        //how to acquire key from value inwards hashtable inwards Java
        Hashtable tabular array = new Hashtable();
        table.put("Sony", "Bravia");
        table.put("Samsung", "Galaxy");
        table.put("Nokia", "Lumia");
      
        System.out.println("does hash tabular array has Lumia every bit value : " + table.containsValue("Lumia"));
        System.out.println("does hash tabular array Lumia every bit key : " + table.containsKey("Lumia"));
      
        //finding key corresponding to value inwards hashtable - i to i 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 i to i map
            }
        }
        System.out.println("got key from value inwards hashtable key:  "+ key +" value: " + value);
      
        //finding key corresponding to value inwards hashtable - i 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 inwards hash table:  "+ value);
      
      
        //how to acquire from value inwards HashMap event - similar to Hashtable example
        HashMap map = new HashMap();
        map.put("one", 1);
        map.put("two", 2);
      
        //find key from value inwards HashMap Java - i to i mapping
        Integer intValue=2;
        String strKey = null;
        for(Map.Entry entry: map.entrySet()){
            if(intValue.equals(entry.getValue())){
                strKey = entry.getKey();
                break; //breaking because its i to i map
            }
        }
      
        System.out.println("key from value inwards hash tabular array key:  "+ strKey +" value: " + intValue);
    }
}

Output
does hash tabular array has Lumia every bit value : true
does hash tabular array has Lumia every bit key : false
got the key from value inwards hashtable key:  Nokia value: Lumia
keys : [Nokia, HTC] corresponding to value inwards hash table:  Lumia
key from value inwards hash tabular array key:  two value: 2


If y'all are using bi-directional Map e.g. BiMap (http://google-collections.googlecode.com/svn/trunk/javadoc/index.html?com/google/common/collect/BiMap.html ) from google collections y'all tin produce it inwards but i trouble :

bidirMap.inverse().get(value)

That's all on how to acquire key from value inwards Hashtable as well as HashMap inwards Java. Though using google collection for this chore is much clear as well as concise, it’s non ever the best option. Using JDK to detect the key from value inwards HashMap is better, if y'all know your map is i to i or i to many. Let me know, if y'all come upward across whatsoever other agency of finding the key from value inwards Hashtable or HashMap inwards Java.

Further Learning
Java In-Depth: Become a Complete Java Engineer
Java Fundamentals: Collections
Data Structures as well as Algorithms: Deep Dive Using Java
Algorithms as well as Data Structures - Part 1 as well as 2
Data Structures inwards Java nine yesteryear Heinz Kabutz

0 Response to "How To Decease Primal From Value Inwards Hashtable, Hashmap Or Map Inwards Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel