java 集合处理:

// 1 数组转map
public static void main(String[] args) {
    String backendIdStr="[\"backend-mvj05upv7yc\",\"backend-mvj055qvric\",\"backend-mvj04hlutx4\"]";
    String[] backendIdList = JsonUtil.asObject(backendIdStr, String[].class);
    Stream stream = Arrays.stream(backendIdList);
    Map map = stream.collect(Collectors.toMap(key -> key, value -> value));
    System.out.println(11111);
}

//  2 List求差集

public static void main(String[] args) {
        List list1 = new ArrayList();
        list1.add("A");
        list1.add("B");
        List list2 = new ArrayList();
        list2.add("B");
        list2.add("C");
        list1.removeAll(list2);
        System.out.println("交集:" + list1);//差集:[A]

}

// 3 List求交集

public static void main(String[] args) {
        List list1 = new ArrayList();
        list1.add("A");
        list1.add("B");
        List list2 = new ArrayList();
        list2.add("B");
        list2.add("C");
        list1.retainAll(list2);
        System.out.println("交集:" + list1);//交集:[B]
    
}

// java 8 处理 :

public void test1(){

List list1=new ArrayList<>();

list1.add(1);

list1.add(2);

list1.add(3);

List list2=new ArrayList<>();

list2.add(3);

list2.add(4);

list2.add(5);

System.out.println("====求交集===");

List list=list1.stream().filter(t->list2.contains(t)).collect(Collectors.toList());

list.stream().forEach(System.out::println);

System.out.println("====求差集===");

list=list1.stream().filter(t-> !list2.contains(t)).collect(Collectors.toList());

list.stream().forEach(System.out::println);

System.out.println("====求并集===");

list.addAll(list1);

list.addAll(list2);

list=list.stream().distinct().collect(Collectors.toList());

list.stream().forEach(System.out::println);

}

你可能感兴趣的:(java,开发语言)