比较excel中的两个列表,并使用java删除不重复的列表
我的目的是比较excel中的两个列表,并删除那些不重复的列表。在我的代码中,我首先将第二个列表添加到第一个列表中。然后我想循环第一个元素来标识每个元素,在同一个循环中,我想选择使用我标识的第一个元素来再次循环整个列表,看看它是否可以找到它的任何重复项,如果它找到了,则将其添加到一个新的列表中。最后,我想删除这两个列表并保存新的列表,这样我就有了一个包含所有重复编号的新列表比较excel中的两个列表,并使用java删除不重复的列表,java,excel,duplicates,Java,Excel,Duplicates,我的目的是比较excel中的两个列表,并删除那些不重复的列表。在我的代码中,我首先将第二个列表添加到第一个列表中。然后我想循环第一个元素来标识每个元素,在同一个循环中,我想选择使用我标识的第一个元素来再次循环整个列表,看看它是否可以找到它的任何重复项,如果它找到了,则将其添加到一个新的列表中。最后,我想删除这两个列表并保存新的列表,这样我就有了一个包含所有重复编号的新列表 public void compareLists(){ getOutCells1().addAll(getOut
public void compareLists(){
getOutCells1().addAll(getOutCells2());
for(int i =0;i<getOutCells1().size();i++){
getOutCells1().get(i);
for( int e=0;e<getOutCells1().size();e++){
getOutCells1().get(e);
if(getOutCells1().get(e)==getOutCells1().get(i)){
System.out.print(getOutCells1().get(e)+" ");
}
}
}
}
我不确定我是否理解你的问题,但我会尽力回答。 首先,我假设在连接它们之前,您不介意重复项是在第一个列表中还是在第二个列表中-您只想在连接的列表中找到重复项。 如果是这种情况,您可能希望查看此处,因为您的实现有许多错误: 如果您的目标是查找第一个列表和第二个列表中的元素,最简单的方法是:
List<Integer> list = new ArrayList<>();
list = getOutCells1().stream()
.distinct()
.collect(Collectors.toList());
List List=new ArrayList();
list=getOutCells1().stream()
.distinct()
.collect(Collectors.toList());
我做了以下操作,效果很好
public void compareLists(){
getOutCells1().addAll(getOutCells2());
for(int i=0;i<getOutCells1().size();i++){
String a=getOutCells1().get(i).toString();
doubleCellList.add(Double.parseDouble(a));
}
findDuplicates(doubleCellList);
}
public Set<Double> findDuplicates(List<Double> doubleCellList)
{
final Set<Double> setToReturn = new HashSet();
final Set<Double> set1 = new HashSet();
for (Double yourInt : doubleCellList)
{
if (!set1.add(yourInt))
{
setToReturn.add(yourInt);
}
}
newDoubleCellList.addAll(setToReturn);
return setToReturn;
}
public void compareLists(){
getOutCells1().addAll(getOutCells2());
对于(int i=0;i您应该只在集合中使用retainal()
Set<String> intersection = new HashSet<>(getOutCells1());
intersection.retainAll(getOutCells2())
return union;
Set intersection=newhashset(getOutCells1());
intersection.retainal(getOutCells2())
回归联盟;
如果getOutCells1()是对象列表,您应该用Hashcode()和Equal()覆盖它。您使用的是哪个库?一个问题似乎是我无法将整数与这个excel库组合:不兼容的类型。这不是并集,而是交集。
Set<String> intersection = new HashSet<>(getOutCells1());
intersection.retainAll(getOutCells2())
return union;