Simple way to compare 2 ArrayLists

后端 未结 10 1845
没有蜡笔的小新
没有蜡笔的小新 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: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 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);
    }
    

提交回复
热议问题