java 两个列表的元素是否相等且各自元素数量相等

示例如下:

import java.util.*;

public class Main {
    public static void main(String[] args) {
        List<String> list1 = Arrays.asList("深圳攀峰运","深圳攀峰运","广州博纳德","广州博纳德","广州博纳德","广州博纳德");
        List<String> list2 = Arrays.asList("广州博纳德","广州博纳德","深圳攀峰运","深圳攀峰运","深圳攀峰运","深圳攀峰运");
        
        // 方法一:使用 Collections 类的 frequency 方法
        boolean isEqual = true;
        for (String element : new HashSet<>(list1)) {
            if (Collections.frequency(list1, element) != Collections.frequency(list2, element)) {
                isEqual = false;
                break;
            }
        }

        System.out.println("两个列表的元素是否相等且各自元素数量相等: " + isEqual);
    }
}
posted @ 2024-05-07 17:56  吴川华仔  阅读(38)  评论(0编辑  收藏  举报