Java 如何从Android中的arraylist中删除重复值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1879700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:29:07  来源:igfitidea点击:

How to remove Duplicate value from arraylist in Android

javaandroidarraylist

提问by Kumar

ArrayList<String> values=new ArrayList<String>();
values.add("s");
values.add("n");
values.add("a");
values.add("s");

In this Array, I want to remove repeated values.

在这个数组中,我想删除重复的值。

回答by dxh

I think a real neat solution for enforcing unique array lists is this one, if it's not too much code for what you're trying to achieve.

我认为强制执行唯一数组列表的一个真正巧妙的解决方案是这个,如果它不是你想要实现的太多代码。

public class UniqueOverridingList extends ArrayList {

    public enum LAST_RESULT {
        ADD, OVERRIDE, NOTHING;
    }

    private LAST_RESULT lastResult;

    public boolean add(T obj) {
        for (int i = 0; i < size(); i++) {
            if (obj.equals(get(i))) {
                set(i, obj);
                lastResult = LAST_RESULT.OVERRIDE;
                return true;
            }
        }
        boolean b = super.add(obj);
        if (b) {
            lastResult = LAST_RESULT.ADD;
        } else {
            lastResult = LAST_RESULT.NOTHING;
        }
        return b;
    }

    public boolean addAll(Collection c) {
        boolean result = true;
        for (T t : c) {
            if (!add(t)) {
                result = false;
            }
        }
        return result;
    }

    public LAST_RESULT getLastResult() {
        return lastResult;
    }

}

回答by Scharrels

The class David Hedlund suggested can be made a lot shorter:

David Hedlund 建议的类可以缩短很多:

public class UniqueArrayList extends ArrayList {
    /**
     * Only add the object if there is not
     * another copy of it in the list
     */
    public boolean add(T obj) {
        if(this.contains(obj))
           return false;
        return super.add(obj);
    }

    public boolean addAll(Collection c) {
        boolean result = false;
        for (T t : c) {
            if (add(t)) {
                result = true;
            }
        }
        return result;
    }
}

The addAlloperation is modified too. The documentationstates:

addAll操作也被改变。该文件指出:

Returns: true if this list changed as a result of the call.

返回: 如果此列表因调用而更改,则返回 true。

I modified the method to reflect this behaviour. There's still one problem. The documentation of the addAll() method also states:

我修改了方法以反映这种行为。还有一个问题。addAll() 方法的文档还指出:

Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator.

按照指定集合的​​迭代器返回的顺序,将指定集合中的所有元素追加到此列表的末尾。

The order might be broken by using this method. A possible workaround for this problem might be not supporting the addAllmethod.

使用此方法可能会破坏订单。此问题的可能解决方法可能不支持该addAll方法。

回答by Freyr

Try below code,

试试下面的代码,

ArrayList<String> values=new ArrayList<String>();
String newValue;

// repeated additions:
if (!values.contains(newValue)) {values.add(newValue);}

回答by Vaishali Sutariya

HashSet hs = new HashSet();

hs.addAll(demoArrayList); // demoArrayList= name of arrayList from which u want to remove duplicates 

demoArrayList.clear();
demoArrayList.addAll(hs);

回答by Silambarasan Poonguti

Try this...

尝试这个...

    ArrayList<String> values=new ArrayList<String>();
    HashSet<String> hashSet = new HashSet<String>();
    hashSet.addAll(values);
    values.clear();
    values.addAll(hashSet);

Happy coding...

快乐编码...