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 ...
随机推荐
- deploy myeclipse j2ee project to server 按了没反应 怎么办
解决办法: 1.如果工作空间的问题,那么需要删除你工作空间的一个文件就可以解决了. 这个文件在Myeclipse工作区(workspace) .metadata\.plugins\org.eclips ...
- Codeforces777B Game of Credit Cards 2017-05-04 17:19 29人阅读 评论(0) 收藏
B. Game of Credit Cards time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- matlab的特殊字符(上下标和希腊字母等)
'T=25\circC',(摄氏度) 下标用 _(下划线) 上标用^ (尖号) 希腊字母等特殊字符用 α \alpha β \beta γ \gamma θ \theta Θ \Theta Г \Ga ...
- SSH 安全建议
当你查看你的 SSH 服务日志,可能你会发现充斥着一些不怀好意的尝试性登录.这里有 5 条常规建议(和一些个别特殊策略)可以让你的 OpenSSH 会话更加安全. 强化密码登录 密码登录很方便,因为你 ...
- 国内云计算的缺失环节: GPU并行计算(转)
[IT时代周刊编者按]云计算特有的优点和巨大的商业前景,让其成为了近年来的IT界最热门词汇之一.当然,这也与中国移动互联网的繁荣紧密相关,它们需要有相应的云计算服务作为支撑.但本文作者祁海江结合自身的 ...
- Simple Package Tool 学习
Simple Package Tool 学习 1.getattr内置函数 getattr(object, name[, default]) python Packages.py install - ...
- Java反射reflection与注解annotation的应用(自动测试机)
一.关于自动测试机 1.什么是自动测试机? 对类中的指定方法进行批量测试的工具 2.自动测试机有什么用? a.避免了冗长的测试代码 当类中的成员方法很多时,对应的测试代码可能会很长,使用测试能够让测试 ...
- python3字符集之间--encode与decode之间的转码详解
encode是编码,里面传入的参数是需要转成的字符集,decode是解码,里面传入的参数是本身的字符集,用本身的字符集解码为unicode字符集再转码 字符集之间的爱恨纠缠 # -*- coding: ...
- Effective Java(1)-创建和销毁对象
Effective Java(1)-创建和销毁对象
- /bin/sh: cc: 未找到命令
redis 4安装出现 /bin/sh cc: 未找到命令 解决: yum -y install gcc automake autoconf libtool make