package com.srs.common.utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author zhy
 * @date 2023-08-18 9:55
 * @description 列表方法
 */
public class listFunc {

    /**
     * 两个列表的差集
     * @author zhy
     * @date 2023-08-18 09:56:19
     * @param list1
     * @param list2
     * @return
     **/
    public static List<String> subList1(List<String> list1, List<String> list2) {
        Map<String, String> tempMap = list2.parallelStream().collect(Collectors.toMap(Function.identity(), Function.identity(), (oldData, newData) -> newData));
        return list1.parallelStream().filter(str->{
            return !tempMap.containsKey(str);
        }).collect(Collectors.toList());
    }

    public static List<String> subList(List<String> actualList, List<String> expectList){
        List<String> list1 = actualList.stream().map(String::new).collect(Collectors.toList());
        List<String> list2 = expectList.stream().map(String::new).collect(Collectors.toList());

        if(list2.size() == 0){
            return list1;
        }
        if(list1.containsAll(list2) && list2.containsAll(list1)){
            return new ArrayList<>();
        }  else {
            List<String> temp = list1.stream()
                    .filter(x->list2.contains(x)).collect(Collectors.toList());
            list1.removeAll(temp);
            return list1;
        }
    }


    /**
     * 两个列表的交集
     * @author zhy
     * @date 2023-08-18 09:59:34
     * @param list1
     * @param list2
     * @return
     **/
    public static List<String> intersectList(List<String> list1, List<String> list2){
        Map<String, String> tempMap = list2.parallelStream().collect(Collectors.toMap(Function.identity(), Function.identity(), (oldData, newData) -> newData));
        return list1.parallelStream().filter(str->{
            return tempMap.containsKey(str);
        }).collect(Collectors.toList());
    }


}
