/**
* 新建线程池
*
* @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);
}
}
}