Simple way to compare 2 ArrayLists

后端 未结 10 1829
没有蜡笔的小新
没有蜡笔的小新 2020-11-30 00:50

I have 2 arraylists of string object.

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

        
相关标签:
10条回答
  • 2020-11-30 01:04

    If your requirement is to maintain the insertion order plus check the contents of the two arraylist then you should do following:

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

    This will return true.

    However, if you change the ordering for example:

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

    will return false as ordering is different.

    0 讨论(0)
  • 2020-11-30 01:10

    Convert Lists to Collection and use 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 );
    

    Output:

    [c, g]
    [gg, h]
    

    [EDIT]

    other way (more clear)

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

    Output:

    [b]
    [boo]
    
    0 讨论(0)
  • 2020-11-30 01:12
    List<String> oldList = Arrays.asList("a", "b", "c", "d", "e", "f");
    List<String> modifiedList = Arrays.asList("a", "b", "c", "d", "e", "g");
    
    List<String> added = new HashSet<>(modifiedList);
    List<String> removed = new HashSet<>(oldList);
    
    modifiedList.stream().filter(removed::remove).forEach(added::remove);
    
    // added items
    System.out.println(added);
    // removed items
    System.out.println(removed);
    
    0 讨论(0)
  • 2020-11-30 01:16

    Convert the List in to String and check whether the Strings are same or not

    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<String> one  = new ArrayList<String>();
            ArrayList<String> two  = new ArrayList<String>();
    
            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));
        }
    }
    
    0 讨论(0)
  • 2020-11-30 01:20

    This should check if two lists are equal, it does some basic checks first (i.e. nulls and lengths), then sorts and uses the collections.equals method to check if they are equal.

    public  boolean equalLists(List<String> a, List<String> 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);
    }
    
    0 讨论(0)
  • 2020-11-30 01:20
    boolean isEquals(List<String> firstList, List<String> secondList){
        ArrayList<String> 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) ;
    }
    
    0 讨论(0)
提交回复
热议问题