Java的差异列表 (Java de chāyì lièbiǎo)
以下是我用于取得两个ArrayList、Set等之间差异的备忘录。
(ArrayList,Set)a1,a2;
a1.removeAll(a2)
a2.removeAll(a1)
可以通过a1-a2,a2-a1的形式获取差异列表。a1,a2可以是Collection类型,不需要类型匹配。
以下是实验代码和结果。
以下为实验代码和结果。
我只需要一种选项,将以下日语句子归纳为中文:
代码:
ArrayList<String> arr1 = new ArrayList<String>() {
private static final long serialVersionUID = 1L;
{
add("a");
add("b");
add("c");
add("d");
add("e");
}
};
ArrayList<String> arr2 = new ArrayList<String>(arr1);
arr2.remove(0);
arr2.remove(0);
arr2.add("f");
System.out.println("arr1");
for (String s : arr1) {
System.out.println(s);
}
System.out.println("--------");
System.out.println("arr2");
for (String s : arr2) {
System.out.println(s);
}
System.out.println("--------");
System.out.println();
ArrayList<String> subArr1 = new ArrayList<String>(arr1);
subArr1.removeAll(arr2);
System.out.println("subArr1");
for (String s : subArr1) {
System.out.println(s);
}
System.out.println("-------");
ArrayList<String> subArr2 = new ArrayList<String>(arr2);
subArr2.removeAll(arr1);
System.out.println("subArr2");
for (String s: subArr2) {
System.out.println(s);
}
System.out.println("-------");
System.out.println();
Set<String> set1 = new HashSet<String>();
set1.add("a");
set1.add("b");
set1.add("c");
set1.add("d");
set1.add("e");
Set<String> set2 = new HashSet<String>(set1);
set2.remove("a");
set2.remove("b");
set2.add("f");
System.out.println("set1");
for (String s : set1) {
System.out.println(s);
}
System.out.println("--------");
System.out.println("set2");
for (String s : set2) {
System.out.println(s);
}
System.out.println("-------");
System.out.println();
Set<String> subSet1 = new HashSet<String>(set1);
subSet1.removeAll(set2);
System.out.println("subSet1");
for (String s : subSet1) {
System.out.println(s);
}
System.out.println("-------");
Set<String> subSet2 = new HashSet<String>(set2);
System.out.println("subSet2");
subSet2.removeAll(set1);
for (String s: subSet2) {
System.out.println(s);
}
System.out.println("-------");
System.out.println();
ArrayList<String> subArrWithSet = new ArrayList<String>(set1);
subArrWithSet.removeAll(arr2);
System.out.println("subArrWithSet");
for (String s : subArrWithSet) {
System.out.println(s);
}
System.out.println("-------");
结果:
arr1
a
b
c
d
e
--------
arr2
c
d
e
f
--------
subArr1
a
b
-------
subArr2
f
-------
set1
a
b
c
d
e
--------
set2
c
d
e
f
-------
subSet1
a
b
-------
subSet2
f
-------
subArrWithSet
a
b
-------