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);
有没有办法将键值检索为列表?
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. }
我用这三种方法迭代一张地图。 所有方法( keySet
, values
, entrySet
)都返回一个集合。
// Given the following map Map 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 entry : myMap.entrySet()) System.out.println(entry.getKey() + " - " + entry.getValue());
由于Java 8我经常使用带有lambda表达式的 Stream
。
// 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()));
map.keySet()
会给你所有的键
//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 hashMap = new HashMap(); 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 sortedHashMap = new TreeMap(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 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); } }
您可以使用keySet()
来检索密钥。 您还应该考虑在地图中添加输入,例如:
Map hm = new HashMap(); hm.put(1,"godric gryfindor"); hm.put(2,"helga hufflepuff"); hm.put(3,"rowena ravenclaw"); hm.put(4,"salazaar slytherin"); Set keys = hm.keySet();
public static void main(String[] args) { HashMap hashmap = new HashMap(); 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 keyIterator = hashmap.keySet().iterator(); Iterator valueIterator = hashmap.values().iterator(); while (keyIterator.hasNext()) { System.out.println("key: " + keyIterator.next()); } while (valueIterator.hasNext()) { System.out.println("value: " + valueIterator.next()); } }
void hashMapExample(){ HashMap hMap = new HashMap(); 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)); } } }