2012-06-15 9 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"); 
} 

私は(たとえばLinkedList用)は、JavaコレクションまたはユーティリティのセットとしてのHashMapからすべてのキーと値を取得します。

は、私はこのように、キーを知っていれば、私は値を取得することができます知っている:

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()は、あなたがキーを取得するためにkeySet()を使用することができ、すべてのキー

+1

返事をくれてありがとうございました.....私はLinkedListに戻り値を型キャストできますか? –

+0

返される値は 'Set'の型の1つで、直接ではありませんが、それで構築することができます –

1

を与えるだろう。

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

を私はマップを反復処理するために、これらの三つの方法を使用します。 また例えば、あなたの地図でタイピングを追加することを検討すべきです。すべてのメソッド(keySetvaluesentrySet)はコレクションを返します。

// 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ので、私はよくlambda expressionsStreamsを使用しています。

// 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)に従ってください。コンテンツの品質を上げることは便利です –