Java 使用stream()两个集合根据元素中的某个字段取交集


list1<Bean1>和list2<Bean2> 根据元素的playerId属性 取交集

Bean1和Bean2 都含有playerId属性

List<Bean1> intersectList = list1.stream()
                        .filter(pe -> find1(pe.getPlayerId(), list2) > -1).collect(Collectors.toList());

辅助方法find1

public int find1(long playerId,List<PlayerExtra> list) {
        int res = -1;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getPlayerId() == playerId) {
                res = i;
                break;
            }
        }
        return res;
    }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM