Java8 new features must be installed in the stream (continuous update)

Stop talking nonsense and just install it! ! !

Stream's distinct() method (requires equals() and hashCode())

 List<PenBean> newPenBeanList = penBeanList.stream().distinct().collect(Collectors.toList());

De-duplication according to an attribute of Object in List

//根据user的account属性去重
List<User> list =users.stream().collect( Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getAccount()))),ArrayList::new));

## 获取集合第一个对象 没有就返回为null;

```bash
list.stream().findFirst().orElse(null);

Get all the values ​​of the attributes of the objects in the list

//获取PmsProductCategory集合里面PmsProductCategory的id
List<Long> cid = categories.stream().map(PmsProductCategory::getId).collect(Collectors.toList());

list is grouped according to a certain attribute of the object

//按照pmsProduct的productCategoryId进行分组
Map<Long, List<PmsProduct>> productMap = pmsProducts.stream().collect(Collectors.groupingBy(PmsProduct::getProductCategoryId));
/**
 * String字符串转成List<Long>数据格式
 * String str = "1,2,3,4,5,6" -> List<Long> listLong [1,2,3,4,5,6];
 *
 * @param strArr
 * @return
 */
private List<Long> stringToLongList(String strArr) {
    
    
    return Arrays.stream(strArr.split(","))
                    .map(s -> Long.parseLong(s.trim()))
                    .collect(Collectors.toList());
}

Convert Long type collection to String type collection

Set<String> skuStr= skuIds.stream().map(String::valueOf).collect(Collectors.toSet());

Sort according to a certain attribute of the object

list.stream().sorted(Comparator.comparing(Product::getId).reversed()).collect(Collectors.toList());

Get a collection of an object property

List<Long> orderId = omsOrderPage.getRecords().stream().map(OmsOrder::getId).collect(Collectors.toList());

Guess you like

Origin blog.csdn.net/weixin_45528650/article/details/109153245
Recommended