java8对一些集合的优化操作

1.集合实体提取里面提起一个字段生成新的集合

 List shops = shopService.findList(searchParams);

 List ids = shops.stream().map(Shop::getShopId).collect(Collectors.toList());

2.给里面里面每个实体设置值

List list2 = list.getList();

list2.forEach(t -> t.setBannerId(1L));

3.集合的过滤生成新数组

List< Integer > transactionsIds = transactions.parallelStream().
  filter(t -> t.getType() == Transaction.GROCERY).
  sorted(comparing(Transaction::getValue).reversed()).
  map(Transaction::getId).
  collect(toList());
4.集合的排序
 List datas = daydatas.stream().sorted(Comparator.comparing(RiderForm::getRiderName))
                .collect(Collectors.toList());
 
public class RiderForm implements Serializable, Comparable {
   @Override
    public int compareTo(
            RiderForm o) {
        return riderName.compareTo(o.getRiderName());

    }  
   @Override
    public boolean equals(
            final Object obj) {
        if (obj == null) {
            return false;
        }
        final RiderForm rf = (RiderForm) obj;
        if (this == rf) {
            return true;
        } else {
            return (this.riderName.equals(rf.riderName) && (this.orderType == rf.orderType));
        }
    }
 @Override
    public int hashCode() {
        int hashno = 7;
        hashno = 13 * hashno + (riderName == null ? 0 : riderName.hashCode());
        return hashno;
    }
 
}
5.遍历map
map.entrySet().forEach(entry -> System.out.println("key:value = " + entry.getKey() + ":" + entry.getValue()));
 

转载于:https://www.cnblogs.com/loserchange/p/8317661.html

你可能感兴趣的:(java8对一些集合的优化操作)