1 2 Previous Next 16 Replies Latest reply: Mar 13, 2008 11:01 PM by 807591 RSS

    remove duplicates from ArrayList

    807591
      Hi,

      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();
      h.addAll(arrayList);
      arrayList.clear();
      arrayList.addAll(h); )

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

      Thanks in advance
      nmadhava
        1 2 Previous Next