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