java数组或集合转换等操作

字符串数组转int数组

String[] ids= {“1”, “2”, “3”};
int[] array = Arrays.asList(ids).stream().mapToInt(Integer::parseInt).toArray();
int[] array = Arrays.stream(ids).mapToInt(Integer::parseInt).toArray();

List转String数组

List list = new ArrayList();
list.add(“a1”);
list.add(“a2”);
String[] arr = list.toArray(new String[list.size()]);

对象获取ids集合

Set clazzId = list.stream().map(p -> p.getClazz().getId()).collect(Collectors.toSet());

String数组转List

String[] ids= {“1”, “2”, “3”};
List list = Arrays.asList(ids);

获取Set集合单一元素

set.stream().findFirst().get()

移除元素

list.removeIf(e -> materialIds.contains(e.getId()));

stream分组

Map<String, List> jshStockDetailMap = jshStockDetailList.stream().collect(Collectors.groupingBy(JshStockDetail::getId));

stream升序

ps:需要重新赋值否则不起作用
List materialList = materialList.stream().sorted(Comparator.comparing(MaterialTreeVO::getCreateTime)).collect(Collectors.toList());

stream 降序

ps:需要重新赋值否则不起作用
List materialList = materialList.stream().sorted(Comparator.comparing(MaterialTreeVO::getCreateTime).reversed()).collect(Collectors.toList());

stream 逗号分割成字符串

List list = new ArrayList<>();
list.add(“b”);
list.add(“c”);
list.stream().collect(Collectors.joining(“,”)).concat(“,”);

list转map (单字段)

Map<String, Users> collect = list.stream().collect(Collectors.toMap(Users::getUserId, Function.identity()));

list转map (多个字段)

Map<String, BigDecimal> collect = yarnMaterialInfos.stream().collect(Collectors.toMap(p -> p.getInfoGauzeBatch() + “-” + p.getInfoMaterialId(), ProduceYarnMaterialInfo::getMaterialPercent));

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值