• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

removing duplicates usig set

 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
good day,
i have an ArrayList of String Array. i have duplicates in that ArrayList. how to remove the duplicates using LinkedHashSet.

this is the model of the string array,
String [] clause = {"xyz", "area", "584", "12", "45", "32"};

suppose the arrayList contains three String arrays
1. {"xyz", "area", "584", "12", "45", "32"};
2. {"xyz", "area", "585", "12", "45", "32"};
3. {"xyz", "area", "584", "12", "45", "32"};

i need to remove 3rd array.

it's the clause[2] i need to use to remove duplicates. it's the unique quantity. how to achieve this using LinkedHashset.

Good day
krishna prasad
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't really do this with LinkedHashSet (or any Set) because String[] arrays (and all arrays) do not override equals() and hashCode() in the way that a Set (specifically LinkedHashSet) needs them to be overridden. It would be much better if, instead of an ArrayList of String[] arrays, you had an ArrayList of ArrayLists of Strings. Then you could add each inner ArrayList into a Set (any set, really). Any ArrayList that duplicates a previous ArrayList will simply not be added. But this only works because the Set will check the eauals() method of the items it adds, and ArrayList overrides equals() to be meaningful. That's why ArrayList works better than String[] arrays here.
 
krishna prasad gunasekaran
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thankyou jim,

but how can i tell the Set, upon which criterion the non-duplication has to be carried out.

for example
i have three ArrayLists with 5 elements each

1. "qwe","hai","234","45","23"
2. "qwe","hai","235","45","23"
3. "well","i am duplicate","234","45","23".

1 and 2 are perfectly alright. 3rd is a duplicate. i use the third element each arraylist to carry out non-duplication movement.

my question is how am i to tell the Set that it's the third element of the
arraylist that should be unique, upon which the Set should operate.

good day jim
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can create a wrapper class that has an equals method that only checks on these elements. Don't forget to create a hashCode method as well.
 
krishna prasad gunasekaran
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thankyou, i got the solution, i gave it a try and it worked. what i did is, i created a subclass of ArrayList, overrode the equals() and hashCode() methods to my convienience. it worked.

good day
krishna prasad g
 
reply
    Bookmark Topic Watch Topic
  • New Topic