Java 使用另一个hashmap中的值加载hashmap

Java 使用另一个hashmap中的值加载hashmap,java,hashmap,Java,Hashmap,我有一个hashmap1 我想从原始hashmap1创建一个hashmap2,其中O3是O2中的一个字段 我知道我可以使用代码行 hashmap1.values().O2 检索要加载的O2集合,但我在拼接它时遇到问题 我真的需要在这个过程中使用for循环吗 注意。您可以使用条目集对值进行迭代: for(Entry<O1,O2> e : hashmap1.entrySet()){ hashmap2.put(e.getKey(), e.getValue().getO2); }

我有一个hashmap1

我想从原始hashmap1创建一个hashmap2,其中O3是O2中的一个字段

我知道我可以使用代码行

hashmap1.values().O2
检索要加载的O2集合,但我在拼接它时遇到问题

我真的需要在这个过程中使用for循环吗


注意。

您可以使用条目集对值进行迭代:

for(Entry<O1,O2> e : hashmap1.entrySet()){
   hashmap2.put(e.getKey(), e.getValue().getO2);
}
for(条目e:hashmap1.entrySet()){
hashmap2.put(e.getKey(),e.getValue().getO2);
}

您可以使用条目集对值进行迭代:

for(Entry<O1,O2> e : hashmap1.entrySet()){
   hashmap2.put(e.getKey(), e.getValue().getO2);
}
for(条目e:hashmap1.entrySet()){
hashmap2.put(e.getKey(),e.getValue().getO2);
}
试试:

Map newMap=newhashmap(originalMap.size());
对于(条目originalEntry:originalMap.entrySet()){
newMap.put(originalEntry.getKey(),originalEntry.getValue().getO3());
}
试试:

Map newMap=newhashmap(originalMap.size());
对于(条目originalEntry:originalMap.entrySet()){
newMap.put(originalEntry.getKey(),originalEntry.getValue().getO3());
}

对我来说似乎是一个糟糕的封装。对我来说似乎是一个糟糕的封装。