gpt4 book ai didi

Java 8 : removing duplicate after addall twice, 可能存在并发问题?

转载 作者:行者123 更新时间:2023-11-30 06:44:03 24 4
gpt4 key购买 nike

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

问题:我期望重复的项目将被删除,但结果不稳定,即,如果运行多次,则结果不相同。此代码背后可能存在并发问题。

如果一个接一个地添加重复项,那么代码就可以了。但是,如果使用 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;

}
}

最佳答案

你的比较器错误地实现了,检查这些值的反转,你不仅每次都会得到相同的结果,而且会发现第二个预期值是 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
}
}

关于Java 8 : removing duplicate after addall twice, 可能存在并发问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43955363/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com