jdk1.8流和多线程使用代码示范

6 篇文章 0 订阅
3 篇文章 0 订阅

/**
	 * 新建线程池
	 *
	 * @param threadNamePrefix 线程名称前缀 batch_payment-%d
	 * */
	public static ExecutorService getExecutorService(int corePoolSize, int maxPoolSize, int queueSize, String threadNamePrefix){
		ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat(threadNamePrefix + "-%d").build();

		ExecutorService execService = new ThreadPoolExecutor(corePoolSize,maxPoolSize,60, TimeUnit.SECONDS,
				new LinkedBlockingDeque<>(queueSize),threadFactory,new ThreadPoolExecutor.AbortPolicy());

		return execService;
	}

   final ExecutorService executorService = BaseUtil.getExecutorService(4,Integer.MAX_VALUE,15,"thread-scanCodeFor-runner");

	if(!executorService.isShutdown()){
                    List<CompletableFuture<PayRhRequestInfoDTO>> priceFutures = payRHRequestInfoEntities.stream()
                            .map(payRhRequestInfoDTO -> CompletableFuture.supplyAsync(
                                    () -> conversionPayRHTransationInfoEntity(payRhRequestInfoDTO),executorService
                            ).exceptionally(e -> {
                                System.out.println(e);
                                return payRhRequestInfoDTO;
                            })).collect(toList());

                    //得到对应的结果 这一步对性能的消耗很大
                    payRHRequestInfoEntities = priceFutures.stream().map(CompletableFuture::join).collect(toList());

                    executorService.shutdown();
                }

根据用户的id分组

Map<String, List<User>> vatInvIcsDataMap = userList.stream()
                .collect(
                        Collectors.groupingBy(
                                User::getId
                        )
                );

通过stream对list去重
摘自https://blog.csdn.net/ianly123/article/details/82658622
另一个方法https://blog.csdn.net/haiyoung/article/details/80934467?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.add_param_isCf&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.add_param_isCf

// Person 对象
public class Person {
    private String id;
    
    private String name;
    
    private String sex;

    <!--省略 get set-->
}

// 根据name去重
List<Person> unique = persons.stream().collect(
            Collectors.collectingAndThen(
                    Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Person::getName))), ArrayList::new)
);


// 根据name,sex两个属性去重
List<Person> unique = persons.stream().collect(
           Collectors. collectingAndThen(
                    Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getName() + ";" + o.getSex()))), ArrayList::new)
);

filter()过滤列表

List<Person> filterList = persons.stream().filter(p -> p.getSex().equals(1)).collect(Collectors.toList());


List转Map
从一个Person对象的List集合,取出id和name组成一个map集合

Map<String, String> collect = list.stream().collect(Collectors.toMap(p -> p.getId(), p -> p.getName()));

从 List 中取出某个属性的组成 list 集合

//1.提取出list对象中的一个属性
List<String> stIdList1 = stuList.stream().map(Person::getId).collect(Collectors.toList());

//2.提取出list对象中的一个属性并去重
List<String> stIdList2 = stuList.stream().map(Person::getId).distinct().collect(Collectors.toList());

对两个list进行数据的匹配

    /**
     * 对两个字符串集合进行数据的比对,如果有不一致数据,报错;
     * firstList    要比对的少量数据
     * secondList   要比对的全量数据
     */
    public static void checkLists(List<String> childList,
                                  List<String> parentList,
                                  String message){
        childList = childList.stream().map(
                child -> parentList.stream().filter(
                        parent -> Objects.equals(parent, child)
                ).findAny().orElse(null)
        ).collect(Collectors.toList());

        for (Object o : childList){
            if (!PartnerUtils.isNotEmpty(o)){
                throw new PartnerException(message);
            }
        }
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值