stream 比较两个list对象中的某个值是否相等

一个用户的list列表,一个员工的list列表,现在比较两个列表中的id是否相同。


  
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class TestListComponent {

    public static void main(String[] args) {
        List<User> userList=new ArrayList<>();
        for(int i=1;i<=30;i++){
            User user=new User();
            user.setUserId(i);
            user.setName(i+"name");
            userList.add(user);
        }
        List<Employee> employeeList=new ArrayList<>();
        for(int i=30;i>=1;i--){
            Employee employee=new Employee();
            employee.setEmployeeId(i);

            employeeList.add(employee);
        }

        System.out.println("===="+isALLCheckpointHasTime(userList,employeeList));

    }


    public static boolean isALLCheckpointHasTime(List<User> userList, List<Employee> employeeList){

        //将列表中需要比较的对象拿出来
        List<Integer>  userIntList=userList.stream().map(User::getUserId).collect(Collectors.toList());
        List<Integer>  employeeIntList= employeeList.stream().map(Employee::getEmployeeId).collect(Collectors.toList());
        if(userIntList==employeeIntList){
            return true;
        }
        //遍历一个对象,查看另外一个list中是否存在
        return  !userIntList.retainAll(employeeIntList);
    }

看一下retainAll的源码

    protected transient int modCount = 0;
    transient Object[] elementData; 
    private int size;
    public boolean retainAll(Collection<?> c) {
        Objects.requireNonNull(c);
        //调用自己的私有方法
        return batchRemove(c, true);
    }
    //如果此 collection 由于调用而发生更改,则返回 true
    //集合A比较与集合B的交集
    private boolean batchRemove(Collection<?> c, boolean complement) {
        //获得当前对象的所有元素
        final Object[] elementData = this.elementData;
        //w:标记两个集合公共元素的个数
        int r = 0, w = 0;
        //设置标志位
        boolean modified = false;
        try {
            //遍历集合A
            for (; r < size; r++)
                //判断集合B中是否包含集合A中的当前元素
                if (c.contains(elementData[r]) == complement)
                    //如果包含则直接保存。
                    elementData[w++] = elementData[r];
        } finally {
            // Preserve behavioral compatibility with AbstractCollection,
            // even if c.contains() throws.
            // 如果 c.contains() 抛出异常
            if (r != size) {
                System.arraycopy(elementData, r,
                        elementData, w,
                        size - r);
                //w为当前集合A的length
                w += size - r;
            }
            //如果集合A的大小放生改变
            if (w != size) {
                // clear to let GC do its work
                // 清除工作
                for (int i = w; i < size; i++)
                    elementData[i] = null;
                //记录集合中元素的改变(add/remove)
                modCount += size - w;
                //设置当前数组的大小
                size = w;
                //返回为true
                modified = true;
            }
        }
        return modified;
    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章