2012-06-15 8 views
17

मैं मानों को स्टोर करना चाहता हूं और उन्हें जावा हैश मैप से पुनर्प्राप्त करना चाहता हूं।जावा हैश मैप कुंजी मूल्य भंडारण और पुनर्प्राप्ति

public void processHashMap() 
{ 
    HashMap hm = new HashMap(); 
    hm.put(1,"godric gryfindor"); 
    hm.put(2,"helga hufflepuff"); 
    hm.put(3,"rowena ravenclaw"); 
    hm.put(4,"salazaar slytherin"); 
} 

मैं के रूप में एक जावा संग्रह या उपयोगिता सेट (उदाहरण के LinkedList के लिए) HashMap से सभी कुंजियां और मान प्राप्त करना चाहते हैं:

यह वही है मैं अब तक है।

मैं जानता हूँ कि अगर मैं कुंजी पता है, इस तरह मैं मूल्य प्राप्त कर सकते हैं:

hm.get(1); 

वहाँ एक सूची कुंजी मान को पुनः प्राप्त करने के लिए एक रास्ता है?

+1

आप जावाडोक परामर्श विचार किया है? – EJP

उत्तर

13

जावा HashMap कुंजी मान उदाहरण:

public void processHashMap() { 
    //add keys->value pairs to a hashmap: 
    HashMap hm = new HashMap(); 
    hm.put(1, "godric gryfindor"); 
    hm.put(2, "helga hufflepuff"); 
    hm.put(3, "rowena ravenclaw"); 
    hm.put(4, "salazaar slytherin"); 

    //Then get data back out of it: 
    LinkedList ll = new LinkedList(); 
    Iterator itr = hm.keySet().iterator(); 
    while(itr.hasNext()) { 
     String key = itr.next(); 
     ll.add(key); 
    } 

    System.out.print(ll); //The key list will be printed. 
} 
10

map.keySet() आप सभी चाबियाँ

+1

आपके उत्तर ब्रो के लिए धन्यवाद ..... तो क्या मेरे लिए एक लिंक्डलिस्ट में वापसी मूल्य टाइप करना संभव है ........?? –

+0

लौटा मूल्य 'सेट' के प्रकार में से एक है, इसलिए सीधे नहीं, लेकिन आप इसे –

1

आप keySet() उपयोग कर सकते हैं कुंजी को पुनः प्राप्त करने देना होगा। आप भी अपने मानचित्र में टाइपिंग जोड़ने, उदा विचार करना चाहिए:

Map<Integer, String> hm = new HashMap<Integer, String>(); 
hm.put(1,"godric gryfindor"); 
hm.put(2,"helga hufflepuff"); 
hm.put(3,"rowena ravenclaw"); 
hm.put(4,"salazaar slytherin"); 

Set<Integer> keys = hm.keySet(); 
1
public static void main(String[] args) { 

    HashMap<String, String> hashmap = new HashMap<String, String>(); 

    hashmap.put("one", "1"); 
    hashmap.put("two", "2"); 
    hashmap.put("three", "3"); 
    hashmap.put("four", "4"); 
    hashmap.put("five", "5"); 
    hashmap.put("six", "6"); 

    Iterator<String> keyIterator = hashmap.keySet().iterator(); 
    Iterator<String> valueIterator = hashmap.values().iterator(); 

    while (keyIterator.hasNext()) { 
     System.out.println("key: " + keyIterator.next()); 
    } 

    while (valueIterator.hasNext()) { 
     System.out.println("value: " + valueIterator.next()); 
    } 
} 
32

मैं इन तीन तरीकों का प्रयोग कर एक नक्शा पुनरावृत्ति करने के लिए। सभी विधियां (keySet, values, entrySet) एक संग्रह वापस करें।

// Given the following map 
Map<KeyClass, ValueClass> myMap; 

// Iterate all keys 
for (KeyClass key : myMap.keySet()) 
    System.out.println(key); 

// Iterate all values 
for (ValueClass value : myMap.values()) 
    System.out.println(value); 

// Iterate all key/value pairs 
for (Entry<KeyClass, ValueClass> entry : myMap.entrySet()) 
    System.out.println(entry.getKey() + " - " + entry.getValue()); 

जावा 8 के बाद से मैं अक्सर lambda expressions साथ Streams का उपयोग करें।

// Iterate all keys 
    myMap.keySet().stream().forEach(key -> System.out.println(key)); 

    // Iterate all values 
    myMap.values().parallelStream().forEach(value -> System.out.println(value)); 

    // Iterate all key/value pairs 
    myMap.entrySet().stream().forEach(entry -> System.out.println(entry.getKey() + " - " + entry.getValue())); 
+0

से बना सकते हैं। बहुत धन्यवाद! –

+0

बहुत स्पष्ट उदाहरण। – luizfelippe

2
//import statements 
import java.util.HashMap; 
import java.util.Iterator; 
import java.util.TreeMap; 

// hashmap test class 
public class HashMapTest { 

    public static void main(String args[]) { 

     HashMap<Integer,String> hashMap = new HashMap<Integer,String>(); 

     hashMap.put(91, "India"); 
     hashMap.put(34, "Spain"); 
     hashMap.put(63, "Philippines"); 
     hashMap.put(41, "Switzerland"); 

     // sorting elements 
     System.out.println("Unsorted HashMap: " + hashMap); 
     TreeMap<Integer,String> sortedHashMap = new TreeMap<Integer,String>(hashMap); 
     System.out.println("Sorted HashMap: " + sortedHashMap); 

     // hashmap empty check 
     boolean isHashMapEmpty = hashMap.isEmpty(); 
     System.out.println("HashMap Empty: " + isHashMapEmpty); 

     // hashmap size 
     System.out.println("HashMap Size: " + hashMap.size()); 

     // hashmap iteration and printing 
     Iterator<Integer> keyIterator = hashMap.keySet().iterator(); 
     while(keyIterator.hasNext()) { 
      Integer key = keyIterator.next(); 
      System.out.println("Code=" + key + " Country=" + hashMap.get(key)); 
     } 

     // searching element by key and value 
     System.out.println("Does HashMap contains 91 as key: " + hashMap.containsKey(91)); 
     System.out.println("Does HashMap contains India as value: " + hashMap.containsValue("India")); 

     // deleting element by key 
     Integer key = 91; 
     Object value = hashMap.remove(key); 
     System.out.println("Following item is removed from HashMap: " + value); 

    } 

} 
+0

हैश मैप – user3339147

0
void hashMapExample(){ 

    HashMap<String, String> hMap = new HashMap<String, String>(); 
    hMap.put("key1", "val1"); 
    hMap.put("key2", "val2"); 
    hMap.put("key3", "val3"); 
    hMap.put("key4", "val4"); 
    hMap.put("key5", "val5"); 

    if(hMap != null && !hMap.isEmpty()){ 
     for(String key : hMap.keySet()){ 
      System.out.println(key+":"+hMap.get(key)); 
     } 
    } 
} 
+0

के मूल संचालन पर एक पूर्ण उदाहरण कृपया इस [यूआरएल] (http://stackoverflow.com/help) का पालन करें, यह आपकी सामग्री की गुणवत्ता को उठाने के लिए उपयोगी होगा –

संबंधित मुद्दे