How do I remove repeated elements from ArrayList?

后端 未结 30 1710
难免孤独
难免孤独 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<String> duplicatList = new ArrayList<String>();
    duplicatList = Arrays.asList("AA","BB","CC","DD","DD","EE","AA","FF");
    //above AA and DD are duplicate
    Set<String> uniqueList = new HashSet<String>(duplicatList);
    duplicatList = new ArrayList<String>(uniqueList); //let GC will doing free memory
    System.out.println("Removed Duplicate : "+duplicatList);
    

    Note: Definitely, there will be memory overhead.

    0 讨论(0)
  • 2020-11-21 06:38

    If you are using model type List< T>/ArrayList< T> . Hope,it's help you.

    Here is my code without using any other data structure like set or hashmap

    for (int i = 0; i < Models.size(); i++){
    for (int j = i + 1; j < Models.size(); j++) {       
     if (Models.get(i).getName().equals(Models.get(j).getName())) {    
     Models.remove(j);
       j--;
      }
     }
    }
    
    0 讨论(0)
  • 2020-11-21 06:39

    Here's a way that doesn't affect your list ordering:

    ArrayList l1 = new ArrayList();
    ArrayList l2 = new ArrayList();
    
    Iterator iterator = l1.iterator();
    
    while (iterator.hasNext()) {
        YourClass o = (YourClass) iterator.next();
        if(!l2.contains(o)) l2.add(o);
    }
    

    l1 is the original list, and l2 is the list without repeated items (Make sure YourClass has the equals method according to what you want to stand for equality)

    0 讨论(0)
  • 2020-11-21 06:40

    If you don't want duplicates in a Collection, you should consider why you're using a Collection that allows duplicates. The easiest way to remove repeated elements is to add the contents to a Set (which will not allow duplicates) and then add the Set back to the ArrayList:

    Set<String> set = new HashSet<>(yourList);
    yourList.clear();
    yourList.addAll(set);
    

    Of course, this destroys the ordering of the elements in the ArrayList.

    0 讨论(0)
  • 2020-11-21 06:40

    Java 8 streams provide a very simple way to remove duplicate elements from a list. Using the distinct method. If we have a list of cities and we want to remove duplicates from that list it can be done in a single line -

     List<String> cityList = new ArrayList<>();
     cityList.add("Delhi");
     cityList.add("Mumbai");
     cityList.add("Bangalore");
     cityList.add("Chennai");
     cityList.add("Kolkata");
     cityList.add("Mumbai");
    
     cityList = cityList.stream().distinct().collect(Collectors.toList());
    

    How to remove duplicate elements from an arraylist

    0 讨论(0)
  • 2020-11-21 06:41
            List<String> result = new ArrayList<String>();
            Set<String> set = new LinkedHashSet<String>();
            String s = "ravi is a good!boy. But ravi is very nasty fellow.";
            StringTokenizer st = new StringTokenizer(s, " ,. ,!");
            while (st.hasMoreTokens()) {
                result.add(st.nextToken());
            }
             System.out.println(result);
             set.addAll(result);
            result.clear();
            result.addAll(set);
            System.out.println(result);
    
    output:
    [ravi, is, a, good, boy, But, ravi, is, very, nasty, fellow]
    [ravi, is, a, good, boy, But, very, nasty, fellow]
    
    0 讨论(0)
提交回复
热议问题