2012-06-15 15 views
17

Desidero memorizzare i valori e recuperarli da una Java HashMap.Archiviazione e recupero valore chiave Java HashMap

Questo è quello che ho finora:

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

voglio recuperare tutte le chiavi e valori dal HashMap come una raccolta o un set di utility Java (per esempio LinkedList).

so di poter ottenere il valore se conosco la chiave, in questo modo:

hm.get(1); 

C'è un modo per recuperare i valori chiave come una lista?

+1

Avete considerato consultazione del Javadoc? – EJP

risposta

13

Java hashmap valore chiave esempio:

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() sarebbe darà tutte le chiavi

+1

grazie per la tua risposta bro ..... quindi è possibile per me digitare il valore restituito in una LinkedList ........ ?? Il valore restituito –

+0

è uno dei tipi di 'Set', quindi non direttamente ma è possibile ricavarne il succinto –

1

È possibile utilizzare keySet() per recuperare le chiavi. Si dovrebbe anche considerare l'aggiunta di battitura nella mappa, per esempio:

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

io uso questi tre modi per iterare una mappa. Tutti i metodi (keySet, values, entrySet) restituiscono una raccolta.

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

Dal Java 8 Io uso spesso Streams con lambda 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

Succinto. Grazie molto! –

+0

Esempi molto chiari. – 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

Un esempio completo sulle operazioni di base di 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

segui questo [URL] (http://stackoverflow.com/help) sarà utile per migliorare la qualità dei tuoi contenuti –

Problemi correlati