gpt4 book ai didi

java - map 比较不会产生正确的结果

转载 作者:行者123 更新时间:2023-11-30 03:34:27 25 4
gpt4 key购买 nike

我有两个 HashMap FinalOldCsv 和 FinalNewCsv。这些映射存储从旧 csv 和新 csv 读取的值。下面是我的代码,用于找出常见行、仅来自旧 csv 的行和仅来自新 csv 的行。对于包含以下内容的小 csv数千行,我的代码工作正常。但是当我尝试对一百万行的 csv 执行相同的操作时。它会产生错误的结果。代码-

 private static void findDiff(LinkedHashMap<String, Integer> finalOldCsv,
LinkedHashMap<String, Integer> finalNewCsv) {
for(String test:finalOldCsv.keySet())
{
System.out.println("first row from old="+finalOldCsv.get(test));
}
for(String test1:finalNewCsv.keySet())
{
System.out.println("first row from new="+finalNewCsv.get(test1));

}
ArrayList<String>temp=new ArrayList<String>();
for(String oldMatch : finalNewCsv.keySet())
{
if(oldMatch.contains(column[0]))
continue;
else
{
if (finalNewCsv.containsKey(oldMatch)&& finalOldCsv.containsKey(oldMatch))
{
System.out.println("Match Found");
writeCsv(writer,"Result/"+prefix+"_", oldMatch,"Common Rows");
temp.add(oldMatch);
}
}
}
System.out.println("before old csv size="+finalOldCsv.size());
for(String t:temp)
{
finalNewCsv.remove(t);
finalOldCsv.remove(t);
}
System.out.println("after old csv size="+finalOldCsv.size());
temp.clear();
for(String newMatch : finalNewCsv.keySet())
{
if(newMatch.contains(column[0]))
continue;
else
{
if (!finalOldCsv.containsKey(newMatch)&& finalNewCsv.containsKey(newMatch))
{
writeCsv(writer,"Result/"+prefix+"_", newMatch,"New Rows in New Table");
temp.add(newMatch);

}
}

}
for(String t:temp)
{
finalNewCsv.remove(t);
}
temp.clear();
System.out.println("finalOldCsv.keySet().size()"+finalOldCsv.keySet().size());
for(String restFromOldTable:finalOldCsv.keySet())
{
if(restFromOldTable.contains(column[0]))
continue;
else
// if()
writeCsv(writer,"Result/"+prefix+"_", restFromOldTable,"Rows from Old Table");
}

}

最佳答案

我认为你让事情变得更加复杂了。例如,当您在 if 语句中迭代 finalNewCsv 时,您会得到这个 finalNewCsv.containsKey(oldMatch),这是不必要的,因为它始终为 true

整个方法可以简化为:

Iterator<Map.Entry<String, Integer>> it = oldMan.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, Integer> entry = it.next();
if (newMap.containsKey(entry.getKey())) {
it.remove();
commonEntries.put(entry.getKey(), entry.getValue());
newMap.remove(entry.getKey());
}
}

其作用是将 oldMapnewMap 中所有相似的键添加到 commonEntries 映射中。我不完全确定这是否是 findDiff() 应该做的事情(该方法的名称具有误导性)。

关于java - map 比较不会产生正确的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28319900/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com