集合拆分和取交集

1.集合拆分Lists.partition的使用

需要的包

import com.google.common.collect.Lists;

引入maven依赖

<dependency>
	<groupId>com.google.guava</groupId>
	<artifactId>guava</artifactId>
	<version>21.0</version>
</dependency>

使用场景:list集合中数据量过大,可根据需要进行拆分,进而通过循环或者多线程来处理数据

Lists.partition方法,根据传入的size,对list进行拆分

package test;
 
import com.google.common.collect.Lists;
import org.junit.Test;
 
import java.util.List;
 
/**
 * Created by Administrator on 2018/10/28/028.
 */
public class testList {
 
    @Test
    public void  test(){
        List<Integer> numList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8);
 
        List<List<Integer>> lists=Lists.partition(numList,3);
        System.out.println(lists);//[[1, 2, 3], [4, 5, 6], [7, 8]]
 
    }
 
}

2. 两个集合取交集

        List<Integer> accountIdListOne = new ArrayList<>();
        accountIdListOne.add(1);
        accountIdListOne.add(2);
        accountIdListOne.add(3);
 
        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());
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值