Java 从ArrayList中删除重复项

Java 从ArrayList中删除重复项,java,Java,我有一个自定义对象的数组列表。我想删除重复的条目 对象有三个字段:title、subtitle和id。如果一个字幕出现多次,我只需要第一个带有该字幕的项目(忽略带有该字幕的剩余对象) 您可以使用O(n^2)解决方案:使用list.iterator()迭代列表一次,每次迭代时,再次迭代以检查是否存在重复项。如果存在-calliterator.remove()。一种变体是使用guava的Iterables.filter(list,predicate),其中过滤逻辑位于谓词中 另一种方法(也许更好)是

我有一个自定义对象的数组列表。我想删除重复的条目

对象有三个字段:
title、subtitle
id
。如果一个字幕出现多次,我只需要第一个带有该字幕的项目(忽略带有该字幕的剩余对象)

您可以使用O(n^2)解决方案:使用
list.iterator()
迭代列表一次,每次迭代时,再次迭代以检查是否存在重复项。如果存在-call
iterator.remove()
。一种变体是使用guava的
Iterables.filter(list,predicate)
,其中过滤逻辑位于谓词中


另一种方法(也许更好)是定义
equals(..)
hashCode(..)
方法来处理自定义的相等逻辑,然后简单地构造一个
新的HashSet(list)
。这将清除重复项。

我建议使用一套

就其性质而言,不能包含重复项。可以使用从原始ArrayList创建新集合

Set myset = new HashSet(myArrayList);

或者,从一开始就使用一个集合,不要使用ArrayList,因为它没有执行您需要的功能。

您可以使用自定义比较器将ArrayList的内容放入树集中,如果两个字幕相同,则该比较器应返回0。 之后,您可以转换列表中的集合,并使列表中没有“重复项”。 这是一个对象的示例,当然您应该使用正确的类和逻辑

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());
}
public void removedeplicates(列表l){
//…列表已填充
设置s=新树集(新比较器(){
@凌驾
公共整数比较(对象o1、对象o2){
//…根据您的要求比较这两个对象
返回0;
}
});
s、 阿道尔(l);
List res=Arrays.asList(s.toArray());
}

您可能需要重写“equals()”,这样,如果两个元素具有相同的子标题(或者可能是tite和subtitle?),那么它们就被视为相等。

如果我理解正确,您有一个
ArrayList
,让我们称之为
列表。您的
Custom
类有一个subtitle字段,比如使用返回
String
getSubtitle()
方法。您只想保留第一个唯一的字幕,并删除所有剩余的重复字幕。以下是如何做到这一点:

Set<String> subtitles = new HashSet<String>();
for (Iterator<Custom> it = list.iterator(); it.hasNext(); ) {
    if (!subtitles.add(it.next().getSubtitle())) {
        it.remove();
    }
}
Set subtitles=newhashset();
for(Iterator it=list.Iterator();it.hasNext();){
if(!subtitles.add(it.next().getSubtitle())){
it.remove();
}
}
列表结果=新建ArrayList();
Set titles=new HashSet();
用于(项目:原始列表){
if(titles.add(item.getTitle()){
结果.添加(项目);
}
}
如果元素已经存在,
集合的
add()
返回
false

私有静态列表移除副本(列表列表){
private static List<Integer> removeDuplicates(List<Integer> list) {
    ArrayList<Integer> uniqueList = new ArrayList<Integer>();
    for (Integer i : list) {
        if (!inArray(i, uniqueList)) {
            uniqueList.add(i);
        }
    }

    return uniqueList;
}

private static boolean inArray(Integer i, List<Integer> list) {
    for (Integer integer : list) {
        if (integer == i) {
            return true;
        }
    }

    return false;
}
ArrayList uniqueList=新的ArrayList(); for(整数i:列表){ 如果(!inArray(i,唯一列表)){ 唯一列表。添加(i); } } 返回唯一列表; } 私有静态布尔数组(整数i,列表){ 用于(整数:列表){ 如果(整数==i){ 返回true; } } 返回false; }
删除集合中的任何重复项,同时保留顺序(如果它是有序集合)。对于大多数情况,效率足够高

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;
}
公共静态T移除副本(T集合)
{
Set setItems=新的LinkedHashSet(集合);
collection.clear();
collection.addAll(集合项);
回收;
}
使用Collections.sort()进行排序,并使用简单的for循环捕捉双精度,例如:

Collections.sort(myList);
A previous = null;
for (A elem: myList) {
    if (elem.compareTo(previous) == 0) continue;
    previous = elem;

    [... process unique element ...]
}
这假定您将在类型A中实现Comparable。

List all=**********//这是您已经填充的对象。
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
}
List noRepeat=new ArrayList(); 为了(你的目标:全部){ 布尔值isPresent=false; //检查noRepeat中是否已存在当前对象字幕 用于(您的对象编号:noRepeat){ 如果(nr.getName().equals(al.getName()){ isFound=true;//是的,我们已经找到了 打破 } } 如果(!isPresent) noRepeat.add(al);//如果我们还没有添加 }
获取一个相同类型的新ArrayList对象
逐个将所有旧的arraylist元素添加到此新的arraylist对象中 但在添加每个对象之前,请在新arraylist中检查是否有具有相同字幕的对象。如果新arraylist包含此类字幕,请不要添加它。否则,请添加它


解决办法取决于具体情况

如果您没有太多数据,则使用Set
Set unique=new HashSet(yourList);
(如果您关心顺序,请使用LinkedHashSet。它会创建一个新集合,但通常不是问题

当您想要修改现有列表并且不想/无法创建新集合时,可以删除重复项,如下所示:

List<Integer> numbers =
    new ArrayList<>(asList(1, 1, 2, 1, 2, 3, 5));

System.out.println("Numbers: " + numbers);
ListIterator<Integer> it = numbers.listIterator();
while (it.hasNext()) {
    int i = it.nextIndex();
    Integer current = it.next();
    for (int j = 0; j < i; ++j) {
        if (current.equals(numbers.get(j))) {
            it.remove();
            break;
        }
    }
}
System.out.println("Unique: " + numbers);
列表编号=
新的ArrayList(asList(1,1,2,1,2,3,5));
System.out.println(“数字:”+数字);
ListIterator it=numbers.ListIterator();
while(it.hasNext()){
int i=it.nextIndex();
当前整数=it.next();
对于(int j=0;j

它在O(n^2)中工作,但它工作。类似的实现,但更简单,是当列表被排序时——在O(n)时间内工作。这两种实现都在Farenda:.

Java8更新:

使用Java8流,您还可以做一些非常琐碎的事情

ArrayList<String> deduped;
deduped = yourArrayList.stream()
             .distinct()
             .collect(Collectors.toCollection(ArrayList::new));
ArrayList重复数据消除;
dedupled=yourArrayList.stream()
.distinct()
.collect(收集器.toCollection(ArrayList::new));
这也比使用ArrayList有优势→ 设置→ 维护列表
List<Integer> numbers =
    new ArrayList<>(asList(1, 1, 2, 1, 2, 3, 5));

System.out.println("Numbers: " + numbers);
ListIterator<Integer> it = numbers.listIterator();
while (it.hasNext()) {
    int i = it.nextIndex();
    Integer current = it.next();
    for (int j = 0; j < i; ++j) {
        if (current.equals(numbers.get(j))) {
            it.remove();
            break;
        }
    }
}
System.out.println("Unique: " + numbers);
ArrayList<String> deduped;
deduped = yourArrayList.stream()
             .distinct()
             .collect(Collectors.toCollection(ArrayList::new));
List<Customer> CustomerLists;
List<Customer> unique = CustomerLists.stream().collect(collectingAndThen(
        toCollection(() -> new TreeSet<>(comparingLong(Customer::getId))),
        ArrayList::new));
yourList.stream().collect(
     Collectors.toMap(
         obj -> obj.getSubtitle(),
         Function.identity(), 
         (o1,o2) -> o1))
    .values();
new ArrayList(resultCollection);