0

How I can remove duplicates of ArrayList?

If collection type is Car,

public class Car{

String Name;
String Color;
String Door;
...
}

ArrayList carList = new ArrayList;

Remove duplicate of carList, but it removes if which elements same Name, Color, Door.

I want remove duplicate of arraylist by specific component. like Name.

If you know about this, Please help me.

Polaris Nation
  • 1,085
  • 2
  • 18
  • 49
  • Actually, this link may be even more useful: https://stackoverflow.com/questions/2849450/how-to-remove-duplicates-from-a-list – Tim Biegeleisen Aug 23 '17 at 06:40
  • @TimBiegelseisen not really duplcate because he want to remove "duplicate" which are not complete duplicates, bu only duplicate of name – azro Aug 23 '17 at 06:40
  • How have you tried to compare the elements? car.equals(anotherCar) won´t work here as it would have to be a copy from the object – Alexander Heim Aug 23 '17 at 06:40
  • @azro Duplicate of the second link, sorry. – Tim Biegeleisen Aug 23 '17 at 06:40
  • @AlexanderHeim Yes I think of it. compare each other until arraylist's length but it is not good. If arraylist comes big, it spend a lot of time to duplicate arraylist – Polaris Nation Aug 23 '17 at 06:48
  • What I mean is you should set up a method that does the comparison between two car objects for you, where you do something like that if(car.name.equals(othercar.name) && car.color.equals(othercar.color) ... – Alexander Heim Aug 23 '17 at 07:16

0 Answers0