Java8-Guava实战示例
示例一:
跟示例三对比一下,尽量用示例三
List<InvoiceQueryBean> invoiceQueryBeanList = new ArrayList<>();
List<String> invoices = Lists.newArrayList(Iterators.transform(
invoiceQueryBeanList.iterator(), new Function<InvoiceQueryBean, String>() {
@Nullable
@Override
public String apply(@Nullable InvoiceQueryBean input) {
if (StringUtils.isNotBlank(input.getLoanInvoiceId())) {
return input.getLoanInvoiceId();
} else {
return null;
}
}
}));
//去除空的
Iterators.removeIf(invoices.iterator(), StringUtils::isBlank);
示例二:
public static List<PersonLoanInvoiceQueryPojo> getInvoiceQueryPojoList(List<InvoiceQueryBean> invoiceQueryBean) {
return Lists.newArrayList(Iterators.transform(invoiceQueryBean.iterator(),
input -> input == null ? null :
PersonLoanInvoiceQueryPojo.Builder.getInstance()
.addLoanInvoiceId(input.getLoanInvoiceId())
.addUserName(input.getUserName())
.addCertificateKind(input.getCertificateKind())
.addCertificateNo(input.getCertificateNo()).addProductName(input.getProductName())
.addMerchantName(input.getMerchantName())
.addStoreName(input.getStoreName())
.addApplyDate(input.getApplyDate()).addLoanAmount(input.getLoanAmount())
.addLoanPeriod(input.getLoanPeriod()).addLoanPurpose(input.getLoanPurpose())
.addLoanDate(input.getLoanDate()).addRate(input.getRate())
.addChannelNo(input.getChannelNo())
.addApproveDate(input.getApproveDate())
.addReply(input.getReply())
.addMarketingCenterId(input.getMarketingCenterId()).build()));
}
public class PersonLoanInvoiceQueryPojo implements Serializable{
private static final long serialVersionUID = -408985049449365784L;
private String loanInvoiceId;
private String userId;
private String userName;
public static class Builder {
private PersonLoanInvoiceQueryPojo instance = new PersonLoanInvoiceQueryPojo();
private Builder(){}
public static Builder getInstance() {
return new Builder();
}
public static Builder getInstance(PersonLoanInvoiceQueryPojo instance){
Builder builder = new Builder();
builder.instance = instance;
return builder;
}
public Builder addLoanInvoiceId(String loanInvoiceId) {
this.instance.setLoanInvoiceId(loanInvoiceId);
return this;
}
public Builder addUserId(String userId) {
this.instance.setUserId(userId);
return this;
}
public Builder addUserName(String userName) {
this.instance.setUserName(userName);
return this;
}
public PersonLoanInvoiceQueryPojo build() {
return this.instance;
}
}
setters();&getters();
}
示例三:方法引用
方法引用主要有三类:
(1)指向静态方法的方法引用,(例如:Integer中的parseInt方法,写作Integer::parseInt)
(2)指向任意类型实例方法的方法引用(例如String中的length方法,写作String::length)
(3)指向现有对象的实例方法的方法引用(如下例)
import com.google.common.collect.Iterators;
import com.google.common.collect.Lists; List<CreditPersonalInfoChangeApplySerial> applySerialList = new ArrayList<>();
List<String> operatorNoList = Lists.newArrayList(
Iterators.transform(applySerialList.iterator(), CreditPersonalInfoChangeApplySerial::getOperatorNo)); //这个叫做lambda的方法引用,注意方法引用的这个方法不需要()
示例四:
Lambad将List转换成Map
import com.google.common.collect.Maps; List<QueryUserAppInfoByUserIdListPojo> operatorInfoList = new ArrayList<>();
Map<String, QueryUserAppInfoByUserIdListPojo> operatorMap
= Maps.uniqueIndex(operatorInfoList.iterator(), QueryUserAppInfoByUserIdListPojo::getUserId); public class QueryUserAppInfoByUserIdListPojo implements Serializable {
private static final long serialVersionUID = 6876288995978264269L;
private String userId; public String getUserId() {
return this.userId;
} public void setUserId(String userId) {
this.userId = userId;
} }
示例五:
List<UserPojo> list = new ArrayList<>();
list.forEach(input -> {
if (input.getCertificateKind().equals(EnumCertificateKind.RESIDENT_IDENTITY_CARD)) {
userCertificateMap.put(pojo.getUserId(), input);
}
});
示例六:
遍历的时候需要使用到元素的索引,很可惜,Java8 的 Iterable 并没有提供一个带索引的 forEach 方法,自动动手写一个满足自己的需求。
import java.util.Objects;
import java.util.function.BiConsumer; /**
* Iterable 的工具类
*/
public class Iterables { public static <E> void forEach(
Iterable<? extends E> elements, BiConsumer<Integer, ? super E> action) {
Objects.requireNonNull(elements);
Objects.requireNonNull(action); int index = 0;
for (E element : elements) {
action.accept(index++, element);
}
}
}
public static void main(String[] args) throws Exception {
List<String> list = Arrays.asList("a", "b", "b", "c", "c", "c", "d", "d", "d", "f", "f", "g");
Iterables.forEach(list, (index, str) -> System.out.println(index + " -> " + str));
}
示例七:Iterators.find

注意:find()函数有两个重载方法,其中一个是带 defaultValue 的,注意如果别迭代的集合没有符合条件的数据的话,一定要定义一个默认值。否则会报NoSuchElementException异常
Iterators.find(pojoList.iterator(), input -> input != null, null);
参考:
Java8-Guava实战示例的更多相关文章
- vue用mand-mobile ui做交易所移动版实战示例
vue用mand-mobile ui做交易所移动版实战示例 先展示几个界面: 目录结构: main.js // The Vue build version to load with the `impo ...
- 148_赠送300家门店260亿销售额的零售企业Power BI实战示例数据
焦棚子的文章目录 一背景 2022年即将到来之际,笔者准备在Power BI中做一个实战专题,作为实战专题最基础的就是demo数据,于是我们赠送大家一个300家门店,260亿+销售额,360万行+的零 ...
- Guava cache 示例
pom.xml <!-- guava --> <dependency> <groupId>com.google.guava</groupId> < ...
- 基于Casperjs的网页抓取技术【抓取豆瓣信息网络爬虫实战示例】
CasperJS is a navigation scripting & testing utility for the PhantomJS (WebKit) and SlimerJS (Ge ...
- 断言封装之key检查及kv实战示例
️️️️️️️️️️️️️️️️️️️️️️️️️️️️️ 测试: 断言处理: demo_04.pyimport jsonjson_obj = {"access_token":&q ...
- 乐字节-Java8核心特性实战之Lambda表达式
大家好,小乐又来给大家分享Java8核心特性了,上一篇文章是<乐字节|Java8核心实战-接口默认方法>,这次就来讲Java8核心特征之Lambda表达式. Java8 引入Lambda表 ...
- Guava的常用方法示例
Guava Maven Dependency <dependency> <groupId>com.google.guava</groupId> <artifa ...
- Java8函数式编程以及Lambda表达式
第一章 认识Java8以及函数式编程 尽管距离Java8发布已经过去7.8年的时间,但时至今日仍然有许多公司.项目停留在Java7甚至更早的版本.即使已经开始使用Java8的项目,大多数程序员也仍然采 ...
- Java8 Stream流
第三章 Stream流 <Java8 Stream编码实战>的代码全部在https://github.com/yu-linfeng/BlogRepositories/tree/master ...
随机推荐
- Redis Quick Start [遇到问题解决问题版]
http://redis.io/topics/quickstart make test 时的问题: 问题:gcc: Command not found 解决:yum install gcc [root ...
- ENVI数据格式
选择一个或多个感兴趣的图层: 分类:目视解译.非监督分类 投影 哪里需要七参数,没有必要七参数吧?如果精度要求不高的话
- Android ContentProvider和Uri详解
一.使用ContentProvider(内容提供者)共享数据 ContentProvider在android中的作用是对外共享数据, 也就是说你可以通过ContentProvider把应用中的数据共享 ...
- hibernate 一对一 one to one的两种配置方式
hibernate中one-to-one两种配置方式 标签: hibernateHibernateone-to-one 2013-02-19 17:44 11445人阅读 评论(1) 收藏 举报 分 ...
- swipe js bug
最近因为要写新的mobile site页面,有好几个页面上面必须用到photo slider. 使用插件: /* * Swipe 2.0 * * Brad Birdsall * Copyright 2 ...
- CUDA开发 - CUDA 版本
"CUDA runtime is insufficient with CUDA driver"CUDA 9.2: 396.xx CUDA 9.1: 387.xx CUDA 9.0: ...
- 项目笔记---WPF之Metro风格UI
写在前面 作为新年开篇的文章,当然要选择比较“Cool”的东西来分享,这自然落到了WPF身上,WPF技术自身可塑性非常强,其强大的绘图技术以及XAML技术比WinForm而言有本质的飞跃. 切入正题, ...
- C# Socket 实现WebSocket服务器端
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.N ...
- java spring boot 开启监控信息
效果: 配置 // pom <dependency> <groupId>org.springframework.boot</groupId> <artifac ...
- Wpf 导出CSV文件
/// <summary> /// 将DataTable中数据写入到CSV文件中 /// </summary> /// <param name="dt" ...