java8总结
JDK API
英文版:
Oracle 官网在线 JDK1.9 文档:
https://docs.oracle.com/javase/9/docs/api/
Oracle 官网在线 JDK1.8 文档:
https://docs.oracle.com/javase/8/docs/api/
中文版:
云盘下载:https://tooyi.lanzouo.com/ikZ28xbcjfa 密码:26wm
Java8 特性
函数
- 匿名:它不像普通的方法那样有一个明确的名称:写得少而想得多!
- 函数:Lambda函数不像方法那样属于某个特定的类。但和方法一样,Lambda有参数列表、函数主体、返回类型,还可能有可以抛出的异常列表。
- 传递:Lambda表达式可以作为参数传递给方法或存储在变量中。
- 简洁:无需像匿名类那样写很多模板代码。
函数式接口 | 函数描述符 | 原始类型特化 |
---|---|---|
Predicate | T->boolean | IntPredicate,LongPredicate, DoublePredicate |
Function<T,R> | T->R | IntFunction,IntToDoubleFunction,IntToLongFunction, LongFunction,LongToDoubleFunction,LongToIntFunction, DoubleFunction, ToIntFunction,ToDoubleFunction, ToLongFunction |
Supplier | ()->T | BooleanSupplier,IntSupplier, LongSupplier, DoubleSupplier |
Consumer | T->void | IntConsumer,LongConsumer, DoubleConsumer |
BiPredicate<L,R> | (L,R)->boolean | IntBinaryOperator, LongBinaryOperator, DoubleBinaryOperator |
BiFunction<T,U,R> | (T,U)->R | ToIntBiFunction<T,U>, ToLongBiFunction<T,U>, ToDoubleBiFunction<T,U> |
BiConsumer<T,U> | (T,U)->void | ObjIntConsumer, ObjLongConsumer, ObjDoubleConsumer |
UnaryOperator | T->T | IntUnaryOperator, LongUnaryOperator, DoubleUnaryOperator |
BinaryOperator | (T,T)->T | IntBinaryOperator, LongBinaryOperator, DoubleBinaryOperator |
Lambdas及函数式接口的例子
使用案例 | Lambda 的例子 | 对应的函数式接口 |
---|---|---|
布尔表达式 | (List list) -> list.isEmpty() | Predicate<List> |
创建对象 | () -> new Project() | Supplier |
消费一个 对象 |
(Project p) -> System.out.println(p.getStars()) | Consumer |
从一个对象 中选择/提取 |
(int a, int b) -> a * b | IntBinaryOperator |
比较两个 对象 |
(Project p1, Project p2) -> p1.getStars().compareTo(p2.getStars()) |
Comparator 或 BiFunction<Project,Project, Integer> 或 ToIntBiFunction<Project, Project> |
四大函数
Predicate< T > :断言
接收一个输入参数,返回一个布尔值结果。 java8 之前的写法,等价于 if 判断。
@Test
public void testPredicate(){
Predicate<String> predicate = new Predicate<String>() {
@Override
public boolean test(String s) {
return s.endsWith(".com");
}
};
Assert.assertTrue(predicate.test("https://www.bilibili.com"));
System.out.println(predicate.test("https://www.bilibili.com"));
}
java8 的写法,Predicate< T > :T 可以是任意对象,代表入参,Predicate 只有一个 test 方法。
@Test
public void testPredicate01(){
Predicate<String> predicate = s -> s.endsWith(".com");
System.out.println(predicate.test("https://www.sina.com.cn")); //输出false
IntPredicate intPredicate = (int i) -> i % 2 == 0;
intPredicate.test(200); //输出true
}
传入一个实体举例
@Test
public void testPredicate01(){
Predicate<Channel> predicate1 = channel -> checkFieldRequire(channel);
Channel channel = new Channel().setSourceId(1)
.setSourceName("BEI JIN")
.setChannelCode("A")
.setChannelName("京");
System.out.println(predicate1.test(channel)); //输出 true
}
/**
* 校验方法
*/
public static Boolean checkFieldRequire(Channel channel){
if (ObjectUtil.isEmpty(channel.getSourceId())) {
//ObjectUtil 是hutool的方法
return Boolean.FALSE;
}
if (StrUtil.isEmpty(channel.getSourceName())) {
return Boolean.FALSE;
}
if (StrUtil.isEmpty(channel.getChannelCode())) {
return Boolean.FALSE;
}
if (StrUtil.isEmpty(channel.getChannelName())) {
return Boolean.FALSE;
}
return Boolean.TRUE;
}
实体对象,建议搭建一个简单的 springBoot 工程,方便引用工具包。
@Data
@Accessors(chain = true)
@ApiModel(value = "Channel")
public static class Channel {
@TableId(value = "id", type = IdType.AUTO)
@ApiModelProperty("主键")
private Long id;
@TableField("source_id")
@ApiModelProperty("来源")
private Integer sourceId;
@TableField("source_name")
@ApiModelProperty("来源")
private String sourceName;
@ApiModelProperty("编码")
private String channelCode;
@ApiModelProperty("名称")
private String channelName;
@ApiModelProperty("创建人")
private String createUser;
@ApiModelProperty("创建时间")
@TableField(fill = FieldFill.INSERT)
private Date createTime;
@ApiModelProperty("修改人")
private String updateUser;
@ApiModelProperty("修改时间")
@TableField(fill = FieldFill.UPDATE)
private Date updateTime;
@ApiModelProperty("是否刪除(Y 是,N 否)")
private String isDelete;
}
BiPredicate<T, U>
BiPredicate 是 Predicate 的变种函数,代表了接收两个参数的boolean值方法。
@Test
public void testBiPredicate(){
BiPredicate<List<String>, String> biPredicate = (list, item) -> list.contains(item);
List<String> strList = Stream.of("A", "B", "F", "K", "W", "Y").collect(Collectors.toList());
System.out.println(biPredicate.test(strList, "H"));
}
Function<T, R> :转换
接受一个输入参数,返回一个结果。
/**
* 转换
*/
public static void function(){
Function<String, String> toUpperCase = String::toUpperCase