Java 8:在addall之后删除重复两次,可能的并发问题?

时间:2017-05-13 16:20:46

标签: java concurrency

我知道这是一个古老的话题,但以下奇怪的测试用例看起来很新,至少对我自己而言,提前感谢!

问题: 我希望删除重复的项目,但结果不稳定,即如果运行多次,则结果不一样。这些代码背后可能存在并发问题。

如果一个接一个地添加重复项,那么代码就可以了。 但是,如果使用addall两次(仅用于测试目的),那么期望的结果是不稳定的。

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.stream.Collectors;

public class TestJava {

    public static void main(String[] args) {
        List<TestObj> result = new ArrayList<>();

        List<TestObj> tempList = new ArrayList<>();
        List<TestObj> tempList2 = new ArrayList<>();
        List<TestObj> tempResult = new ArrayList<>();

        int size = 3;

        for (int i = 0; i < size; i++) {
            TestObj testObj = new TestObj();
            testObj.setField1("555");
            testObj.setField2("" + i);
            tempList.add(testObj);

            TestObj testObj2 = new TestObj();
            testObj2.setField1("555");
            testObj2.setField2("" + i);
            tempList2.add(testObj2);

            // tempResult.add(testObj); // <-- if use this, then no concurrency issue
            // tempResult.add(testObj2);
        }

        tempResult.addAll(tempList);
        tempResult.addAll(tempList2);

        System.out.println("expecting 6, actual = " + tempResult.size());

        FilterComparator comp = new FilterComparator();

        result = tempResult.stream() //
                .filter(new ConcurrentSkipListSet<>(comp)::add)//
                .collect(Collectors.toList());
        System.out.println("expecting 3, actual = " + result.size()); // <-- Here, looks like there is a concurrency issue! 
    }

    public static class TestObj {
        private String field1;
        private String field2;
        private String field3;

        public String getField1() {
            return field1;
        }

        public void setField1(String field1) {
            this.field1 = field1;
        }

        public String getField2() {
            return field2;
        }

        public void setField2(String field2) {
            this.field2 = field2;
        }

        public String getField3() {
            return field3;
        }

        public void setField3(String field3) {
            this.field3 = field3;
        }

    }

    public static class FilterComparator implements Comparator<TestObj> {
        public int compare(TestObj o1, TestObj o2) {
            if (compareIfSameString(o1.getField1(), o2.getField1()) //
                    && compareIfSameString(o1.getField2(), o2.getField2()) //
            ) {
                return 0;
            }

            return 1;
        }
    }

    public static boolean compareIfSameString(String oldValue, String newValue) {
        if (isReallyEmptyOrNull(oldValue) && isReallyEmptyOrNull(newValue)) {
            return true;
        }

        return Objects.equals(oldValue, newValue);
    }

    public static boolean isReallyEmptyOrNull(String value) {
        return value == null || "".equals(value.trim());
    }

}

更新:供以后参考 感谢JB Nizet的评论。我修改了比较器如下,一切都很好!

public static class FilterComparator implements Comparator<TestObj> {
    public int compare(TestObj o1, TestObj o2) {
        if (o1.getField1() == null) {
            if (o2.getField1() != null) {
                return o2.getField1().compareTo(o1.getField1());
            }
        } else if (o1.getField1().compareTo(o2.getField1()) != 0) {
            return o1.getField1().compareTo(o2.getField1());
        }

        if (o1.getField2() == null) {
            if (o2.getField2() != null) {
                return o2.getField2().compareTo(o1.getField2());
            }
        } else if (o1.getField2().compareTo(o2.getField2()) != 0) {
            return o1.getField2().compareTo(o2.getField2());
        }

        if (o1.getField3() == null) {
            if (o2.getField3() != null) {
                return o2.getField3().compareTo(o1.getField3());
            }
        } else if (o1.getField3().compareTo(o2.getField3()) != 0) {
            return o1.getField3().compareTo(o2.getField3());
        }
        return 0;

    }
}

1 个答案:

答案 0 :(得分:1)

你的比较器被错误地实现了,检查这些值的反转,你不仅每次都得到相同的结果,而且发现预期的第二个值是2而不是3.; - )

 public static class FilterComparator implements Comparator<TestObj> {

        public  int compare(TestObj o1, TestObj o2) {
            if (compareIfSameString(o1.getField1(), o2.getField1()) //
                    && compareIfSameString(o1.getField2(), o2.getField2()) //
            ) {
                return 1;// here
            }

            return 0; //and here
        }
    }