How do I remove repeated elements from ArrayList?

后端 未结 30 2262
难免孤独
难免孤独 2020-11-21 06:24

I have an ArrayList, and I want to remove repeated strings from it. How can I do this?

30条回答
  •  梦如初夏
    2020-11-21 06:38

    Code:

    List duplicatList = new ArrayList();
    duplicatList = Arrays.asList("AA","BB","CC","DD","DD","EE","AA","FF");
    //above AA and DD are duplicate
    Set uniqueList = new HashSet(duplicatList);
    duplicatList = new ArrayList(uniqueList); //let GC will doing free memory
    System.out.println("Removed Duplicate : "+duplicatList);
    

    Note: Definitely, there will be memory overhead.

提交回复
热议问题