Simple way to compare 2 ArrayLists

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

提交回复
热议问题