Simple way to compare 2 ArrayLists

后端 未结 10 1848
没有蜡笔的小新
没有蜡笔的小新 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:21

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

提交回复
热议问题