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 partsecondList.removeAll(firstList);System.out.println("Result: " + secondList);
更新: 更复杂的代码版本
Collection<String> firstList = new ArrayList<String>();firstList.add("str1");firstList.add("str2");Collection<String> secondList = new ArrayList<String>();secondList.add("str1");secondList.add("str2");secondList.add("str3");System.out.println("First List: " + firstList);System.out.println("Second List: " + secondList);// Here is main partsecondList.removeAll(firstList);
更新:
要获得两个字符串列表之间的实际差异,请执行此 *** 作。
Set<String> setOne = new HashSet<String>(); Set<String> setTwo = new HashSet<String>(); setOne.add("1"); setOne.add("2"); setOne.add("5"); setTwo.add("1"); setTwo.add("3"); setTwo.add("4"); Set<String> setTwoDummy = new HashSet<String>(setTwo); setTwo.retainAll(setOne); setTwoDummy.addAll(setOne); setTwoDummy.removeAll(setTwo); System.out.println(""+setTwoDummy);
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)