轉換詞典來Collection
並使用removeAll
Collection<String> listOne = new ArrayList(Arrays.asList("a","b", "c", "d", "e", "f", "g"));
Collection<String> listTwo = new ArrayList(Arrays.asList("a","b", "d", "e", "f", "gg", "h"));
List<String> sourceList = new ArrayList<String>(listOne);
List<String> destinationList = new ArrayList<String>(listTwo);
sourceList.removeAll(listTwo);
destinationList.removeAll(listOne);
System.out.println(sourceList);
System.out.println(destinationList);
輸出:
[c, g]
[gg, h]
[編輯]
其他方式(更清晰)
Collection<String> list = new ArrayList(Arrays.asList("a","b", "c", "d", "e", "f", "g"));
List<String> sourceList = new ArrayList<String>(list);
List<String> destinationList = new ArrayList<String>(list);
list.add("boo");
list.remove("b");
sourceList.removeAll(list);
list.removeAll(destinationList);
System.out.println(sourceList);
System.out.println(list);
輸出:
[b]
[boo]
你到目前爲止嘗試了什麼? –
對不起,老兄讀了3遍。沒有得到你所問的。順便說一句,你的代碼在哪裏? –
用於循環來實現它。尋找一些第三方apis .. – prabu