简体   繁体   中英

remove duplicates in list of object array in java

import java.util.ArrayList;
import java.util.List;
public class mainPage {
    public static void main(String[] args) {
        testMethod();
    }
    public static void testMethod() {
        List<Object[]> obj = new ArrayList<Object[]>();

        /*START: Populating obj with test data */
        for (int i = 0; i <= 10; i++) {
            if (i == 5 || i == 6) {
                Object[] value = new Object[] { "hello", "world", "GoodEvening" };
                obj.add(value);
            } else {
                Object[] value = new Object[] { "good", "morning", "helloo" };
                obj.add(value);
            }
        }
        /*END : Populating obj with test data */

        for (Object[] jj : obj) {
            System.out.println("values:   " + jj[0] + "       " + jj[1] + "      " + jj[2]);
        }

        // need a List<Object[]> obj with out duplicates
    }
}

the result of above code is,

values:   good       morning      helloo
values:   good       morning      helloo
values:   good       morning      helloo
values:   good       morning      helloo
values:   good       morning      helloo
values:   hello       world      GoodEvening
values:   hello       world      GoodEvening
values:   good       morning      helloo
values:   good       morning      helloo
values:   good       morning      helloo
values:   good       morning      helloo

Here i have generated a sample data and i have a similar scenario that am facing in the project.

i want the duplicate records to be removed, what is the efficient way to do it.

The result must be:

values:   hello       world      GoodEvening
values:   good       morning      helloo

Try:

List<Object[]> obj = new ArrayList<Object[]>();

// rest of your code goes here

Comparator<Object[]> comp = new Comparator<Object[]>(){
    @Override
    public int compare(Object[] o1, Object[] o2) {
        if( o1.length != o2.length){
            return o1.length - o2.length;
        }
        for( int i = 0; i < o1.length; i++){
            int val = o1[i].toString().compareTo(o2[i].toString());
            if( val != 0 ){
                return val;
            }
        }
        return 0;
    }
};

Set<Object[]> mySet = new TreeSet<>( comp );
mySet.addAll(obj);

for (Object[] jj : mySet) {
    System.out.println("values:   " + jj[0] + "       " + jj[1] + "      " + jj[2]);
}

Another way, still with a TreeSet but with a cleaner Comparator :

for (int i = 0; i <= 10; i++) {
    if (i == 5 || i == 6) {
        obj.add(new Object[] { "hello", "world", "GoodEvening" });
    } else {
        obj.add(new Object[] { "good", "morning", "helloo" });
    }
}

Set<Object[]> result = new TreeSet<>(new Comparator<Object[]>() {
    @Override
    public int compare(final Object[] o1, final Object[] o2) {
        if (Arrays.equals(o1, o2)) {
            return 0;
        }
        return Arrays.hashCode(o1) - Arrays.hashCode(o2);
    }
});
result.addAll(obj);
for (Object[] array : result) {
    System.out.println(Arrays.toString(array));
}

Output:

[hello, world, GoodEvening]
[good, morning, helloo]

One way to do it is to use HashSet ( https://docs.oracle.com/javase/8/docs/api/java/util/HashSet.html ). It will refuse to add items to the list if there is already an identical element in the list. However, the order in which elements will be returned is not preserved. You may have to add your own equals method for the object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM