Số sánh các phần tử trong ArrayList

Chuyển đổi danh sách thành Collection và sử dụng removeAll

Collection listOne = new ArrayList(Arrays.asList("a","b", "c", "d", "e", "f", "g")); Collection listTwo = new ArrayList(Arrays.asList("a","b", "d", "e", "f", "gg", "h")); List sourceList = new ArrayList(listOne); List destinationList = new ArrayList(listTwo); sourceList.removeAll( listTwo ); destinationList.removeAll( listOne ); System.out.println( sourceList ); System.out.println( destinationList );

Đầu ra:

[c, g] [gg, h]

[EDIT]

cách khác (rõ ràng hơn)

Collection list = new ArrayList(Arrays.asList("a","b", "c", "d", "e", "f", "g")); List sourceList = new ArrayList(list); List destinationList = new ArrayList(list); list.add("boo"); list.remove("b"); sourceList.removeAll( list ); list.removeAll( destinationList ); System.out.println( sourceList ); System.out.println( list );

Đầu ra:

[b] [boo]