1 2 Previous Next 16 Replies Latest reply on Jun 20, 2008 4:52 PM by DrClap

    remove duplicates from ArrayList


      I am sorry for asking a simple question.

      I have an ArrayList which contains Location objects.
      Location is a bean class with LocationId, Name, Address etc..

      Now I want to remove duplicates from this ArrayList.
      We know whether duplicates exist or not by checking LocationId of Location object. LocationId is int.

      How can I remove duplicates from this ArrayList?
      I tried using contains method of ArrayList.
      I even tried to copy all the values to HashSet
      (HashSet h = new HashSet();
      arrayList.addAll(h); )

      but as ArrayList contains Location objects, it can not detect duplicate elements.

      Thanks in advance
        1 2 Previous Next