小编典典
Collection firstList = new ArrayList() {{
add("str1");
add("str2");
}};
Collection secondList = new ArrayList() {{
add("str1");
add("str3");
add("str4");
}};
System.out.println("First List: " + firstList);
System.out.println("Second List: " + secondList);
// Here is main part
secondList.removeAll(firstList);
System.out.println("Result: " + secondList);
更新: 更复杂的代码版本
Collection firstList = new ArrayList();
firstList.add("str1");
firstList.add("str2");
Collection secondList = new ArrayList();
secondList.add("str1");
secondList.add("str2");
secondList.add("str3");
System.out.println("First List: " + firstList);
System.out.println("Second List: " + secondList);
// Here is main part
secondList.removeAll(firstList);
更新:
要获得两个字符串列表之间的实际差异,请执行此操作。
Set setOne = new HashSet();
Set setTwo = new HashSet();
setOne.add("1");
setOne.add("2");
setOne.add("5");
setTwo.add("1");
setTwo.add("3");
setTwo.add("4");
Set setTwoDummy = new HashSet(setTwo);
setTwo.retainAll(setOne);
setTwoDummy.addAll(setOne);
setTwoDummy.removeAll(setTwo);
System.out.println(""+setTwoDummy);
2020-11-01
这篇博客探讨了Java中ArrayList的使用,展示如何初始化列表并进行元素操作。通过示例展示了如何使用`removeAll()`方法从一个列表中移除另一个列表的所有元素,实现两个列表的差异比较。更新部分提供了更复杂的代码,使用HashSet进行列表差异计算,并给出了具体步骤。

被折叠的 条评论
为什么被折叠?



