在cells1中,我有值列表,即[4000,4001,4002,4003],在cells2中,有即[1234,4000]。我试图找到重复项和唯一值,但即使它具有重复值,它每次都会转到其他部分。为什么有代码错误。
Iterator itr = cells2.iterator();
while(itr.hasNext()){
HSSFCell s = (HSSFCell) itr.next();
boolean sta = cells1.contains(s.getStringCellValue());
if(sta==true){
System.out.println("Dublicate:>>>>"+s.toString());
}else{
System.out.println("unique value:>>>>:"+s.toString());
}
}
}
输出值为:
唯一值:>>>>:1234
唯一值:>>>>:4000 <- 这是错误的输出
最佳答案
来自您的评论 cells1
是 ArrayList<Cell>
所以你调用 cells1.contains(s.getStringCellValue());
意味着找到String
在ArrayList
的Cells
它不会做任何事情,这就是它进入 else 部分的原因。
最终你试图找到String
在Cells
这是不可能直接实现的。
你必须比较String
String
的每个单元格的值(表示单元格的 ArrayList<Cell>
值)与您的s.getStringCellValue()
你必须覆盖 contains
方法或声明您自己的方法。
public boolean isThereAnyCellWithString(String str){
//Iterate over cells1 and compare
//Use contains here if you want to or equals to find exact match
for(HSSFCell cell : cells1){
if(cell.getStringCellValue().contains(str))
return true;
}
return false;
}
关于java - 查找唯一值和重复值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25093512/