Java HashMap键值存储和检索
我想存储值并从Java HashMap中检索它们 这就是我到目前为止所做的:Java HashMap键值存储和检索,java,hashmap,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"); } 我想从Has
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);
有没有办法以列表形式检索键值?将为您提供所有键您可以使用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();
Map hm=newhashmap();
hm.put(1,“哥德里克·格兰芬多”);
hm.put(2,“黑尔加·赫夫勒普夫”);
hm.put(3,“罗韦娜·拉文克劳”);
hm.put(4,“萨拉扎尔·斯莱特林”);
设置键=hm.keySet();
公共静态void main(字符串[]args){
HashMap HashMap=新的HashMap();
hashmap.put(“一”、“1”);
hashmap.put(“二”,“二”);
hashmap.put(“三”,“三”);
hashmap.put(“四”,“四”);
hashmap.put(“五”、“五”);
hashmap.put(“六”、“六”);
迭代器keyIterator=hashmap.keySet().Iterator();
迭代器valueIterator=hashmap.values().Iterator();
while(keyIterator.hasNext()){
System.out.println(“key:+keyinterator.next());
}
while(valueIterator.hasNext()){
System.out.println(“value:+valueIterator.next());
}
}
我使用这三种方法来迭代贴图。所有方法(,)都返回一个集合
// 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 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.
}
//导入语句
导入java.util.HashMap;
导入java.util.Iterator;
导入java.util.TreeMap;
//hashmap测试类
公共类HashMapTest{
公共静态void main(字符串参数[]){
HashMap HashMap=新的HashMap();
hashMap.put(91,“印度”);
hashMap.put(34,“西班牙”);
hashMap.put(63,“菲律宾”);
hashMap.put(41,“瑞士”);
//排序元素
System.out.println(“未排序的HashMap:+HashMap”);
TreeMap sortedHashMap=新的TreeMap(hashMap);
System.out.println(“排序HashMap:+sortedHashMap”);
//哈希映射空检查
布尔值isHashMapEmpty=hashMap.isEmpty();
System.out.println(“HashMap Empty:+isHashMapEmpty”);
//哈希映射大小
System.out.println(“HashMap Size:+HashMap.Size());
//hashmap迭代和打印
迭代器keyIterator=hashMap.keySet().Iterator();
while(keyIterator.hasNext()){
Integer key=keyIterator.next();
System.out.println(“Code=“+key+”Country=“+hashMap.get(key));
}
//按键和值搜索元素
System.out.println(“HashMap是否包含91作为键:“+HashMap.containsKey(91));
System.out.println(“HashMap是否包含印度作为值:”+HashMap.containsValue(“印度”));
//按键删除元素
整数键=91;
对象值=hashMap.remove(键);
System.out.println(“以下项从HashMap中删除:“+value”);
}
}
void hashMapExample(){
HashMap hMap=新的HashMap();
hMap.put(“键1”、“值1”);
hMap.put(“键2”、“值2”);
hMap.put(“键3”、“值3”);
hMap.put(“键4”、“值4”);
hMap.put(“键5”、“值5”);
if(hMap!=null&&!hMap.isEmpty()){
for(字符串键:hMap.keySet()){
System.out.println(key+”:“+hMap.get(key));
}
}
}
感谢您的回复兄弟…..那么我是否可以将返回值键入LinkedList…..?返回值是集合的类型之一
,所以不是直接的,但是你可以从itA构建一个关于HashMap基本操作的完整示例。你考虑过咨询Javadoc吗?请遵循这一点,这将有助于提高你的内容质量
// 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()));
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.
}
//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);
}
}
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));
}
}
}