How to remove duplicates from a list of object

Harshita Sethi

I have an excel file with following data (dummy)

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

I am reading this file and storing the result in a Set so that duplicates can be removed and I only get unique list. Below is the what I tried

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]);
}

The output of this code is as shown below

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

The set didn't remove the duplicate. What other approach can I used to eliminate these duplicates. Any column can have different or same value. So I cannot remove duplicates based on a particular column.

I want the entire row to be unique.

PS: This data is just dummmy. In real scenario I have more columns and any column value can be different which will make the row unique.

Eran

Set<String[]> can't use a HashSet implementation, since arrays don't override the default hashCode() and equals() implementation of the Object class.

Your alternatives are to use a Set<List<String>> (i.e. convert each String[] to List<String>, which can be done easily with Arrays.asList()) or a TreeSet<String[]> with a custom Comparator<String[]>.

For example :

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));
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to remove duplicates from a list of object

From Dev

How to remove duplicates from a list of custom objects, by a property of the object

From Dev

How to remove duplicates with the same 'title' field from object list in Python?

From Dev

Remove duplicates from a List<Object> using LINQ

From Dev

Remove duplicates from List<Object> based on condition

From Dev

Remove duplicates from a List<Object> using LINQ

From Dev

How to remove all duplicates from a list?

From Dev

How to remove duplicates from a list python

From Dev

How to remove all duplicates from a list

From Dev

How remove duplicates from sql list?

From Dev

How to remove duplicates from circular linked list

From Dev

SetUniqueList, HashSet and Set don't remove duplicates from a List of an object

From Dev

How to remove duplicates from a List<List<T>> for value types T?

From Dev

How to remove duplicates from a List<List<T>> for value types T?

From Dev

Remove duplicates from SimpleXML Object

From Dev

Remove duplicates from an object array

From Dev

Remove 'duplicates' from a list of pairings

From Dev

Remove Duplicates from Sorted List

From Dev

Remove duplicates from a list in SML

From Dev

Elixir Remove Duplicates From List

From Dev

Haskell Remove duplicates from list

From Dev

Remove duplicates from list python

From Dev

Remove duplicates from a list in Scheme

From Dev

Remove duplicates from List(Of T)

From Dev

Remove duplicates from list elements

From Dev

Remove duplicates from subsequences of the list

From Dev

Remove duplicates from List(Of T)

From Dev

Remove duplicates from dropdown list

From Dev

Remove duplicates from a nested list

Related Related

HotTag

Archive