2012-06-15 118 views
17

我想存儲值並從Java HashMap中檢索它們。Java HashMap鍵值存儲和檢索

這是我到目前爲止有:

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"); 
} 

我想要檢索所有鍵和值從HashMap中的Java集合或工具集(例如LinkedList)。

我知道我能得到的值,如果我知道的關鍵,就像這樣:

hm.get(1); 

有沒有一種方法來檢索鍵值作爲一個列表?

+1

你有沒有考慮諮詢的Javadoc? – EJP

回答

13

的Java的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

感謝您的回覆兄弟.....所以我可能會將返回值轉換爲LinkedList ........ ?? ?? –

+0

返回值是Set的類型之一,所以不能直接使用,但可以從中構造 –

1

您可以使用keySet()檢索鍵。 你也應該考慮在你的地圖增加打字,e.g:

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,valuesentrySet)都會返回一個集合。

// 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()); 

由於Java 8我經常使用Streamslambda expressions

// 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

有關HashMap – 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

的基本操作的完整示例,請按照[URL](http://stackoverflow.com/help)提供您的內容質量 –