Java 两个HashMap迭代
我有两个hashmap,我可以用下面的代码迭代这两个hashmapJava 两个HashMap迭代,java,data-structures,Java,Data Structures,我有两个hashmap,我可以用下面的代码迭代这两个hashmap Iterator it = mp.entrySet().iterator(); while (it.hasNext()) { Map.Entry pairs = (Map.Entry)it.next(); String firstVal = pairs.getValue(); } Iterator it2 = mp2.entrySet().iterator(); while (it2.hasNext()) {
Iterator it = mp.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry)it.next();
String firstVal = pairs.getValue();
}
Iterator it2 = mp2.entrySet().iterator();
while (it2.hasNext()) {
Map.Entry pairs2 = (Map.Entry)it.next();
String SecondVal = pairs2.getValue();
}
myFunction(firstVal, SecondVal)
是否可以在不使用两个循环的情况下同时迭代两个hashmap
目前,我有一个接受两个参数的方法,每个参数值存储在第一个和第二个hashmap中。我必须先迭代散列,然后再迭代第二个以获得值。我想一定有个好办法,但我不知道:(
注:上面的代码可能有一些错误,因为这只是一个解释我问题的示例。每个迭代器都是原始程序中的一个方法,并且接受一个参数。我无法复制过去的实时函数,因为它们太大了!您的代码看起来不错。只需使用while循环作为内部和外部循环在两个哈希映射上进行迭代。在第二个while中循环,您可以调用您的函数来执行任何您想要的操作
Map.Entry pairs;
String firstValue = null;
String secondValue = null;
while(it.hasNext() || it2.hasNext()){
if (it.hasNext()){
pairs = (Map.Entry)it.next();
firstValue = pairs.getValue();
}
if (it2.hasNext(){
pairs = (Map.Entry)it2.next();
secondValue = pairs.getValue();
}
if (firstValue != null && secondValue != null){
yourMethodHere();
firstValue = null;
secondValue = null;
}
}
while loop (iterate first hashmap)
second while loop(iterate second hashmap)
call your function here and pass values
将两个贴图的值放入列表,然后循环列表:
//Merge 2 values of maps to a list
List<String> mergedList = new ArrayList<String>();
mergedList.addAll(map1.values());
mergedList.addAll(map2.values());
int size = map1.size() < map2.size() ? map1.size() : map2.size();
for(int i=0; i < size; i++){
myFunction(mergedList.get(i), mergedList.get(map1.size() + i));
}
//将两个贴图值合并到一个列表中
List mergedList=new ArrayList();
mergedList.addAll(map1.values());
mergedList.addAll(map2.values());
int size=map1.size()
我想你要做的是:
if (mp.size() != mp2.size()) {
throw SomeException("mismatched parameters");
}
Iterator it = mp.entrySet().iterator();
Iterator it2 = mp2.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry)it.next();
String firstVal = pairs.getValue();
Map.Entry pairs2 = (Map.Entry)it.next();
String secondVal = pairs2.getValue();
myFunction(firstVal, secondVal);
}
请注意,对一对HashMaps中的条目进行并行迭代是不可靠的。HashMaps条目将按键“对齐”的唯一情况是,如果两个HashMap具有相同的键和相同的HashCode,并且从新分配的HashMaps开始以相同的顺序填充它们
因此,我认为你真的需要这样做
if (mp.size() != mp2.size()) {
throw SomeException("mismatched parameters");
}
Iterator it = mp.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry)it.next();
String firstVal = pairs.getValue();
String SecondVal = mp2.get(pairs.getKey());
myFunction(firstVal, SecondVal);
}
你不能在一个循环中迭代两个映射,除非它们具有相同的键集。我不知道你如何找到
firstVal
和SecondVal
,这似乎有点含糊不清。也许你可以通过Map.get()获得这两个值
?如果映射对象本身是并行的,那么更好的解决方案可能是创建一个自定义类,而不是使用映射。可以保证映射中的迭代顺序,正如某些util对象中已经提到的那样(另一个是LinkedHashMap)。但这并不允许开发人员将对象当作数组或列表来使用。谢谢,但我希望一次传递一个值,如果可能,在循环中传递。第一个值和第二个值是否具有相同的键?没有两个映射具有不同的值:(它们是如何关联的?是的,那么它们是如何关联的?映射没有像列表一样保留插入顺序,因此最好发布整个代码或在较高级别指定要求,以便我们提供更好的帮助。好的,每个hashmap包含文档列表,我正在执行一些计算。我从一个hashmap中获取值,然后乘以我从第二个hashmap中获取了所有值。我希望它能有所帮助。