Remove duplicates from ArrayLists

后端 未结 14 2176
孤街浪徒
孤街浪徒 2020-11-27 04:12

I have an ArrayList of custom objects. I want to remove duplicate entries.

The objects have three fields: title, subtitle, and id. If a su

相关标签:
14条回答
  • 2020-11-27 04:47

    You can put the content of the ArrayList in a TreeSet using a custom Comparator which should return 0 if the two subtitles are the same. After that you can convert the Set in a List and have the List without "duplicates". Here is an example for Object, of course you should use the correct class and logic.

    public void removeDuplicates(List<Object> l) {
        // ... the list is already populated
        Set<Object> s = new TreeSet<Object>(new Comparator<Object>() {
    
            @Override
            public int compare(Object o1, Object o2) {
                // ... compare the two object according to your requirements
                return 0;
            }
        });
                s.addAll(l);
        List<Object> res = Arrays.asList(s.toArray());
    }
    
    0 讨论(0)
  • 2020-11-27 04:48
    List<Item> result = new ArrayList<Item>();
    Set<String> titles = new HashSet<String>();
    
    for(Item item : originalList) {
        if(titles.add(item.getTitle()) {
            result.add(item);
        }
    }
    

    add() of the Set returns false if the element already exists.

    0 讨论(0)
  • 2020-11-27 04:50
    List<YourObject> all = ******** // this is the object that you have already  and filled it.
    List<YourObject> noRepeat= new ArrayList<YourObject>();
    
    for (YourObject al: all) {
        boolean isPresent = false;
        // check if the current objects subtitle already exists in noRepeat
        for (YourObject nr : noRepeat) {
            if (nr.getName().equals(al.getName()) {
                isFound = true;//yes we have already
                break;
            }
        }
    
        if (!isPresent)
            noRepeat.add(al); // we are adding if we don't have already
    }
    

    take one new ArrayList Object of same type
    one by one add all the old arraylists elements into this new arraylist object but before adding every object check in the new arraylist that if there is any object with the same subtitle.if new arraylist contains such subtitle don't add it. otherwise add it

    0 讨论(0)
  • 2020-11-27 04:51

    I would suggest using a Set

    http://download.oracle.com/javase/6/docs/api/java/util/Set.html

    Which by its nature cannot contain duplicate items. You can create a new set from your original ArrayList using

    Set myset = new HashSet(myArrayList);
    

    Alternatively, just use a Set from the start, and don't use an ArrayList as it is not performing the function that you require.

    0 讨论(0)
  • 2020-11-27 04:53

    You can use an O(n^2) solution: Use list.iterator() to iterate the list once, and on each iteration, iterate it again to check if there are duplicates. If there are - call iterator.remove(). A variation of this is to use guava's Iterables.filter(list, predicate) where your filtering logic is in the predicate.

    Another way (perhaps better) would be to define the equals(..) and hashCode(..) methods to handle your custom equality logic, and then simply construct a new HashSet(list). This will clear duplicates.

    0 讨论(0)
  • 2020-11-27 04:58

    Removes any duplicates in a collection, while preserving the order if it is an ordered collection. Efficient enough for most cases.

    public static <I, T extends Collection<I>> T removeDuplicates(T collection)
    {
        Set<I> setItems = new LinkedHashSet<I>(collection);
        collection.clear();
        collection.addAll(setItems);
    
        return collection;
    }
    
    0 讨论(0)
提交回复
热议问题