java8实现两个list取交集

package test;

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

public class ListAndList {
	/**
	 * java8实现两个list取交集
	 * @param args
	 */

	public static void main(String[] args) {
		 List<Integer> accountIdListOne = new ArrayList<>();
	        accountIdListOne.add(1);
	        accountIdListOne.add(2);
	        accountIdListOne.add(3);
	        accountIdListOne.add(4);
	 
	        List<Integer> accountIdListTwo = new ArrayList<>();
	        accountIdListTwo.add(3);
	        accountIdListTwo.add(4);
	        accountIdListTwo.add(5);
	        accountIdListTwo.add(6);
	        
	        
	        List<Integer> accountIdList = accountIdListOne.stream().filter(accountIdListTwo::contains).collect(Collectors.toList());
	        
	        System.out.println(accountIdList.toString());

	}

}

  

posted @ 2022-03-02 14:32  红尘沙漏  阅读(3229)  评论(1编辑  收藏  举报