【发布时间】:2015-10-21 05:58:01
【问题描述】:
我有一个包含以下数据的 excel 文件(虚拟)
a b c
d b c
e b c
f b c
g b c
e b c
d b c
d b c
d b c
我正在读取此文件并将结果存储在一个 Set 中,以便可以删除重复项并且我只获得唯一列表。 以下是我尝试过的
FileInputStream file = new FileInputStream(new File("C:\\Users\\harshita.sethi\\Desktop\\ALLOT010T_Input_Keywords.xls"));
HSSFWorkbook w = new HSSFWorkbook(file);
HSSFSheet sheet = w.getSheetAt(0);
int totalrows = sheet.getLastRowNum();
System.out.println(sheet.getRow(0).getPhysicalNumberOfCells());
String[][] data = new String[totalrows+1][sheet.getRow(0).getPhysicalNumberOfCells()];
Set<String[]> keySet = new HashSet<>();
for (int i = 0; i <= totalrows; i++) {
for (int j = 0; j < sheet.getRow(0).getPhysicalNumberOfCells(); j++) {
HSSFCell cell = sheet.getRow(i).getCell(j);
// writing keywords from excel into a hashmap
data[i][j]=cell.getRichStringCellValue().getString();
}
keySet.add(data[i]);
}
Iterator<String[]> iterator = keySet.iterator();
System.out.println("Output Set is as below");
while(iterator.hasNext()){
String[] next = iterator.next();
System.out.println(next[0] + "\t"+ next[1] +"\t "+next[2]);
}
这段代码的输出如下所示
Output Set is as below
d b c
e b c
a b c
d b c
d b c
g b c
e b c
f b c
d b c
该集合没有删除重复项。我可以使用什么其他方法来消除这些重复项。 任何列都可以具有不同或相同的值。所以我不能删除基于特定列的重复项。
我希望整行都是唯一的。
PS:这个数据只是假的。在实际场景中,我有更多列,并且任何列值都可以不同,这将使行独一无二。
【问题讨论】:
-
在您的情况下,您希望删除
d b c和e b c重复项? -
我想说
d b c和e b c是唯一的行,因为它们的值不同。但是d b c和d b c是一样的,因为整行都是一样的。 -
为什么不能使用创建一个三列的对象并为对象使用比较器而不是数组?
标签: java duplicates