java比较两个list是否相等

判断两个list是否相等:
先判断list的个数是否相等,然后判断是否相互包含。
public boolean equalList(List list1, List list2) {return (list1.size() == list2.size()) && list1.containsAll(list2);}

    public static void main(String[] args) {
        createList01();
        createList02();
    }

    static void createList01(){
        List oneList = new ArrayList<>();
        oneList.add("令狐冲");
        List secondList = new ArrayList<>();
        secondList.add("岳不群");
        System.out.println("比较两个List的结果为:"+equalList(oneList,secondList));
    }

    static void createList02(){
        List oneList = new ArrayList<>();
        oneList.add(User.builder().name("令狐冲").build());
        List secondList = new ArrayList<>();
        secondList.add(User.builder().name("令狐冲").build());
        System.out.println("比较两个List的结果为:"+equalList(oneList,secondList));
    }

    public static boolean equalList(List list1, List list2) {
        return (list1.size() == list2.size()) && list1.containsAll(list2) && list2.containsAll(list1);
    }
image.png

你可能感兴趣的:(java比较两个list是否相等)