删除java中对象数组列表中的重复项

时间:2016-06-20 19:12:51

标签: java collections

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

上面代码的结果是,

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

这里我已经生成了一个示例数据,并且我在项目中遇到了类似的情况。

我希望删除重复的记录,这是有效的方法。

结果必须是:

values:   hello       world      GoodEvening
values:   good       morning      helloo

3 个答案:

答案 0 :(得分:2)

尝试:

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

答案 1 :(得分:2)

另一种方式,仍然使用TreeSet,但使用更清晰的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));
}

<强>输出:

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

答案 2 :(得分:0)

一种方法是使用HashSet(https://docs.oracle.com/javase/8/docs/api/java/util/HashSet.html)。如果列表中已存在相同的元素,它将拒绝将项添加到列表中。但是,不保留返回元素的顺序。您可能必须为对象添加自己的equals方法。