Vavr
Vavr
比方说,我们要实现一个比较器来比较两个对象的大小,在
Compartor<Apple> byWeight = new Comparator<Apple>() {
public int compare(Apple a1, Apple a2) {
return a1.getWeight().compareTo(a2.getWeight());
}
}
上面的代码使用
Comparator<Apple> byWeight = (Apple a1, Apple a2) -> a1.getWeight().compareTo(a2.getWeight());
受限于
扩展支持
<dependency>
<groupId>io.vavr</groupId>
<artifactId>vavr-jackson</artifactId>
<version>0.10.3</version>
</dependency>
首先注册
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new VavrModule());
然后就可以进行序列化操作了:
String json = mapper.writeValueAsString(List.of(1));
// = [1]
List<Integer> restored = mapper.readValue(json, new TypeReference<List<Integer>>() {});
// = List(1)