Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/255.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从哈希映射返回键值列表_Java_Hashmap_Key Value - Fatal编程技术网

Java 如何从哈希映射返回键值列表

Java 如何从哈希映射返回键值列表,java,hashmap,key-value,Java,Hashmap,Key Value,输入是一个散列映射,如 HashMap<String, String> hashmap = new HashMap<String, String>(); for (Map.Entry<String, String> entry : hashmap.entrySet()) { String key = entry.getKey(); Object value = entry.getValue(); } Hash

输入是一个散列映射,如

HashMap<String, String> hashmap = new HashMap<String, String>();

for (Map.Entry<String, String> entry : hashmap.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
}
HashMap HashMap=newhashmap();
for(Map.Entry:hashmap.entrySet()){
String key=entry.getKey();
对象值=entry.getValue();
}
我想编写一个方法,返回类型为Class a的列表,该类具有键、字符串类型的值属性以及hashmap中的键值

如何让它成为现实,提前感谢

List listOfA=new ArrayList();
List<A> listOfA= new ArrayList<>();
for (Map.Entry<String, String> entry : hashmap.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            A aClass = new A(key, value);
            listOfA.add(aClass);
}
return listOfA;
for(Map.Entry:hashmap.entrySet()){ String key=entry.getKey(); 字符串值=entry.getValue(); A aClass=新的A(键、值); 添加(aClass); } 返回列表A;
List listOfA=new ArrayList();
for(Map.Entry:hashmap.entrySet()){
String key=entry.getKey();
字符串值=entry.getValue();
A aClass=新的A(键、值);
添加(aClass);
}
返回列表A;

如果您使用的是Java 8,您可以执行以下操作:

List<Entry<String, String>> list = hashmap
    .entrySet() // Get the set of (key,value)
    .stream()   // Transform to a stream
    .collect(Collectors.toList()); // Convert to a list.
假设在
a
中有一个构造函数,看起来像这样:

A(Map.Entry<String,String> e){
    this.key=e.getKey();
    this.value=e.getValue();
}
A(Map.Entry e){
this.key=e.getKey();
this.value=e.getValue();
}

我希望它能有所帮助。

如果您使用的是Java8,您可以这样做:

List<Entry<String, String>> list = hashmap
    .entrySet() // Get the set of (key,value)
    .stream()   // Transform to a stream
    .collect(Collectors.toList()); // Convert to a list.
假设在
a
中有一个构造函数,看起来像这样:

A(Map.Entry<String,String> e){
    this.key=e.getKey();
    this.value=e.getValue();
}
A(Map.Entry e){
this.key=e.getKey();
this.value=e.getValue();
}

我希望它能有所帮助。

您的意思是您有一个具有两个属性的类a:字符串键;字符串值;在从hashmap获取值时,您必须创建这些对象的列表,对吗?您的意思是您有一个类a,它有两个属性:String key;字符串值;在从hashmap中获取值时,您必须创建这些对象的列表,对吗?当数据已经在
映射中时,为什么要复制到类
a
的实例。Entry
?我不知道,但由于OP希望这样做,所以我提供了一个解决方案,虽然我完全同意你所说的,当数据已经在
映射中时,为什么要复制到class
A
的一个实例。Entry
?我不知道,但由于OP希望这样做,我只提供了一个解决方案,尽管我完全同意你所说的