Remove duplicated elements in array by using Set i

2019-06-03 18:47发布

问题:

I have a Array of object, since I am using a 3rd party library, the array is got by calling one method from the library, I am not able to access the MyObject class.

//I have no access to MyObject class, I am sure the objects contain duplicated elements.
MyObject[] objects = SOME_LIB_CLASS.getObjects(); 
System.out.println("length is "+ objects.length); //length is 6

I try to remove duplicated elements in objects, I use Set:

Set<MyObject>  objectSet = new HashSet<MyObject>(Arrays.asList(objects));
System.out.println("length is "+ objectSet.size()); //length is 6 still

But the objectSet still contains duplicated elements, why & how to solve my problem without iterate through the array?

回答1:

If the set still contains "duplicate" elements than the equals method of your objects does not what you expect it to do.

Duplicates in a HashSet are determined by the equals implementation.

If you can not change the implementation of MyObject.equals()( because you don't have the source code - it is a library class), I recommend to use a TreeSet and provide a special comparator.

For example

public class Main {

    public static class MyObject {

        public int value;

        @Override
        public String toString() {
            return "MyObject [value=" + value + "]";
        }

    }

    public static void main(String str[]) throws IOException {
        Set<MyObject> myObjects = new TreeSet<MyObject>(
                new Comparator<MyObject>() {

                    public int compare(MyObject object1, MyObject object2) {
                        return object1.value - object2.value;
                    }
                });

        addMyObjects(myObjects);
        addMyObjects(myObjects); // try to add the duplicates

        System.out.println(myObjects);
    }

    private static void addMyObjects(Set<MyObject> set){
        for (int i = 0; i < 5; i++) {
            MyObject myObject = new MyObject();
            myObject.value = i;
            set.add(myObject);
        }
    }
}


回答2:

It contains duplicated entries, because MyObject doesn't overwrite equals and hashcode. If you can't access the class, then you have to iterate over the array, and check for differences manually.



回答3:

Can you try to print hashcode of the objects, I think those objects are not same but have same values for there fields. and if MyObject is your class, override equals and hascode method to make set working.



回答4:

Hope below code help you

            ArrayList al = new ArrayList();
            al.add("hello");
            al.add("hi");
            al.add("hello");
            al.add("dadsa");
            al.add("hello");

            // add elements to al, including duplicates
            HashSet hs = new HashSet();
            hs.addAll(al);
            al.clear();
            al.addAll(hs);

            for(int i=0; i<al.size(); i++)
            {
                Log.i("element ", al.get(i).toString());

            }

It will remove all duplicate values from arraylist