比较2个ArrayLists的简单方法

我有两个字符串对象的arraylists。

List sourceList = new ArrayList(); List destinationList = new ArrayList(); 

我有一些逻辑,我需要处理源列表,并最终得到目标列表。 目标列表将添加一些其他元素添加到源列表或从源列表中删除。

我的预期输出是2个字符串ArrayList,其中第一个列表应该从源中删除所有字符串,第二个列表应该具有新添加到源的所有字符串。

任何更简单的方法来实现这一目标?

将列表转换为Collection并使用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 ); 

输出:

 [c, g] [gg, h] 

[编辑]

其他方式(更清楚)

  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 ); 

输出:

 [b] [boo] 

这应检查两个列表是否相等,它首先进行一些基本检查(即空值和长度),然后排序并使用collections.equals方法检查它们是否相等。

 public boolean equalLists(List a, List b){ // Check for sizes and nulls if (a == null && b == null) return true; if ((a == null && b!= null) || (a != null && b== null) || (a.size() != b.size())) { return false; } // Sort and compare the two lists Collections.sort(a); Collections.sort(b); return a.equals(b); } 

List转换为String并检查字符串是否相同

 import java.util.ArrayList; import java.util.List; /** * @author Rakesh KR * */ public class ListCompare { public static boolean compareList(List ls1,List ls2){ return ls1.toString().contentEquals(ls2.toString())?true:false; } public static void main(String[] args) { ArrayList one = new ArrayList(); ArrayList two = new ArrayList(); one.add("one"); one.add("two"); one.add("six"); two.add("one"); two.add("two"); two.add("six"); System.out.println("Output1 :: "+compareList(one,two)); two.add("ten"); System.out.println("Output2 :: "+compareList(one,two)); } } 

答案在@ dku-rajkumarpost中给出。

ArrayList commonList = CollectionUtils.retainAll(list1,list2);

最简单的方法是逐个遍历源列表和目标列表,如下所示:

 List newAddedElementsList = new ArrayList(); List removedElementsList = new ArrayList(); for(String ele : sourceList){ if(destinationList.contains(ele)){ continue; }else{ removedElementsList.add(ele); } } for(String ele : destinationList){ if(sourceList.contains(ele)){ continue; }else{ newAddedElementsList.add(ele); } } 

虽然如果你的源列表和目标列表有很多元素可能效率不高,但肯定会更简单。

如果您的要求是维护插入顺序并检查两个arraylist的内容,那么您应该执行以下操作:

 List listOne = new ArrayList(); List listTwo = new ArrayList(); listOne.add("stack"); listOne.add("overflow"); listTwo.add("stack"); listTwo.add("overflow"); boolean result = Arrays.equals(listOne.toArray(),listTwo.toArray()); 

这将返回true。

但是,如果您更改顺序,例如:

 listOne.add("stack"); listOne.add("overflow"); listTwo.add("overflow"); listTwo.add("stack"); boolean result = Arrays.equals(listOne.toArray(),listTwo.toArray()); 

因为排序不同,将返回false。

据我所知,我认为最简单的方法是使用4个列表: – 你的sourceList – 你的destinationList – 一个removedItemsList – 一个recentAddedItemsList

 private int compareLists(List list1, List list2){ Collections.sort(list1); Collections.sort(list2); int maxIteration = 0; if(list1.size() == list2.size() || list1.size() < list2.size()){ maxIteration = list1.size(); } else { maxIteration = list2.size(); } for (int index = 0; index < maxIteration; index++) { int result = list1.get(index).compareTo(list2.get(index)); if (result == 0) { continue; } else { return result; } } return list1.size() - list2.size(); } 
 boolean isEquals(List firstList, List secondList){ ArrayList commons = new ArrayList<>(); for (String s2 : secondList) { for (String s1 : firstList) { if(s2.contains(s1)){ commons.add(s2); } } } firstList.removeAll(commons); secondList.removeAll(commons); return !(firstList.size() > 0 || secondList.size() > 0) ; }