Vavr库最新版本
<dependency><groupId>io.vavr</groupId><artifactId>vavr</artifactId><version>1.0.0-alpha-3</version></dependency>
Tuple
Tuple(元组)数据类型,可以存储多个不可变不同类型的变量,比如如果有个方法想要返回多个值的时候,我们一般要么就是将这多个值封装成一个新的类型,要么放到map或者list里面,使用起来比较麻烦,可以使用Tuple简化:
@Testpublic void testTuple() {Tuple3<Integer, String, String> person = Tuple.of(20, "张三", "Male");System.out.println(person._1());System.out.println(person._2());System.out.println(person._3());}
Tuple最多可以存8个不同参数,使用的时候就是Tuple1~8 8种类型
Try
包装一段异常语句不显示使用Exception,可以根据异常情况返回不同处理结果,比如异常返回默认值等。
@Testpublic void testTry() {Try<Integer> result = Try.of(() -> 1 / 0);System.out.println(result.isFailure());System.out.println(result.getOrElse(-1));}
Lazy
延迟处理类型
@Testpublic void testLazy() {Lazy<Double> lazy = Lazy.of(Math::random);System.out.println(lazy.isEvaluated());System.out.println(lazy.get());System.out.println(lazy.isEvaluated());}
其他特性请查看以下两篇文章
相关链接:
https://blog.csdn.net/Revivedsun/article/details/80088080
https://www.baeldung.com/vavr
