【问题标题】:How to remove duplicates from a list of object如何从对象列表中删除重复项
【发布时间】: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 ce b c 重复项?
  • 我想说d b ce b c 是唯一的行,因为它们的值不同。但是d b cd b c 是一样的,因为整行都是一样的。
  • 为什么不能使用创建一个三列的对象并为对象使用比较器而不是数组?

标签: java duplicates


【解决方案1】:

Set&lt;String[]&gt; 不能使用HashSet 实现,因为数组不会覆盖Object 类的默认hashCode()equals() 实现。

您的替代方法是使用Set&lt;List&lt;String&gt;&gt;(即将每个String[] 转换为List&lt;String&gt;,这可以通过Arrays.asList() 轻松完成)或带有自定义Comparator&lt;String[]&gt;TreeSet&lt;String[]&gt;

例如:

Set<List<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(Arrays.asList(data[i]));
}
Iterator<List<String>> iterator = keySet.iterator();
System.out.println("Output Set is as below");
while(iterator.hasNext()){
    List<String> next = iterator.next();
    System.out.println(next.get(0) + "\t"+ next.get(1) +"\t "+next.get(2));
}

【讨论】:

    【解决方案2】:

    您可以使用比较器类:

    您可以将 TreeSet 与自定义比较器一起使用,以比较字符串数组是否相等。

    Set<String[]> mySet = new TreeSet<>(new Comparator<String[]>() {
    
      @Override
      public int compare(String[] o1, String[] o2) {
        //logic for comparison.
      }
    
    });
    

    另一种更好的方法是使用集合。使用 List 而不是 String[]:

    例如:

    Set<List<String>> set = //...
    set.add(Arrays.asList("a", "b", "c"));
    set.add(Arrays.asList("a", "b", "c"));
    set.add(Arrays.asList("a", "b", "d"));
    
    System.out.println(set.size()); // 2
    

    【讨论】:

    • 但他想要基于值的比较而不是哈希码
    猜你喜欢
    • 2017-06-23
    • 2021-12-11
    • 1970-01-01
    • 2020-03-30
    • 2022-01-04
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    相关资源
    最近更新 更多