How to remove Duplicate value from arraylist in Android [duplicate]

雨燕双飞 提交于 2019-12-17 18:25:31

问题


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.


回答1:


Try this...

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

Happy coding...




回答2:


Try below code,

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

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



回答3:


HashSet hs = new HashSet();

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

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



回答4:


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;
    }

}



回答5:


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

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 addAll operation is modified too. The documentation states:

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

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

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 addAll method.



来源:https://stackoverflow.com/questions/1879700/how-to-remove-duplicate-value-from-arraylist-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!