lambda表达式

Stream 流式处理4
Supplier 无参 有返回值
Function 有参 有返回值
Consumer 有参 无返回值
Predict 有参 有返回值(boolean)

Optional 用于判断对象是否为空,并做其他操作

Stream:懒加载 先校验 再执行,如果校验发现没有终结函数则不执行(若返回值为Stream则说明为非终结函数)。

======================================================

List<Person> personList = xxx;

集合过滤

  personList.stream.filter(x->x.getAge()>20).collect(Collectors.toList());

对象取字段

  List<String> names = personList.stream.map(Person::getName()).collect(Collectors.toList()); 

分组

  Map<String, List<ProdSku>> prodSkuMap = prodSkus.stream().collect(Collectors.groupingBy(ProdSku::getSpuCode));

List转Map

  Map<String,Person> personMap = personList.stream.collect(Collectors.toMap(Person::getId,Function.identity()));

 判断是否匹配:

  Arrays.asList(whiteListStrs).stream().anyMatch(requestURI::endsWith)

 

循环降低维度:

  1、List转Map

    Map<String,SkuInfoOfCombine> tempSkuMap = tempSkuList.stream().collect(Collectors.toMap(x->x.getGoodsCode(),x->x));

  2、for循环体内比较---》

  for (CombineSkuResponse subSku : subSkuList) {
   SkuInfoOfCombine skuInfoOfCombine = tempSkuMap.get(subSku.getGoodsCode()); //核心
  if(!ObjectUtils.isEmpty(skuInfoOfCombine)){
   。。。
        。。。
  }
  }

 动态代理

public class TestLambdaProxy {

static String calc(User user, Function<User,String> func){
//apply入参类型需与Function代理类的第一个参数一致
//apply出参类型需与Function代理类的第一个参数一致
return func.apply(user);
}

static String getName(User u){
return calc(u,(e)->{return u.getName();});
}

public static void main(String[] args) {
User user = new User();
user.setName("zhangsan");
System.out.println(getName(user));
}
}

==========================================================================================

Function<T, R> 

T:入参类型,R:出参类型

调用方法:R apply(T t); 

定义函数示例:Function<Integer, Integer> func = p -> p * 10;    // 输出入参的10倍

调用函数示例:func.apply(10);    // 结果100

Consumer<T>

T:入参类型;没有出参

 

调用方法:void accept(T t);

定义函数示例:Consumer<String> consumer= p -> System.out.println(p);    // 因为没有出参,常用于打印、发送短信等消费动作

调用函数示例:consumer.accept("18800008888");

Supplier<T>

T:出参类型;没有入参

 

调用方法:T get();

定义函数示例:Supplier<Integer> supplier= () -> 100;    // 常用于业务“有条件运行”时,符合条件再调用获取结果的应用场景;运行结果须提前定义,但不运行。

调用函数示例:supplier.get();

Predicate<T>

T:入参类型;出参类型是Boolean

 

调用方法:boolean test(T t);

定义函数示例:Predicate<Integer> predicate = p -> p % 2 == 0;    // 判断是否、是不是偶数

调用函数示例:predicate.test(100);    // 运行结果true

=======================================================================================================

举例:
声明:private Predicate<Integer> gt5 = i -> i > 5;
引用 if(gt5.test(1)){.....}

=======================================================================================================

public static void main(String[] args) {
List<String> users = Lists.newArrayList();
users.add("1");
collectionEmptyCondition(users, Collection::isEmpty);
}

private static <T> void collectionEmptyCondition(Collection<T> collection, Predicate<Collection<T>> predicate) {
if (predicate.test(collection)) {
System.out.println("empty");
}else{
System.out.println("not empty");
}
}

 









  

posted @ 2020-01-02 22:45  骑驴晒太阳  阅读(218)  评论(0)    收藏  举报