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 ...
随机推荐
- 代码自动生成工具MyGeneration之一
前段时间用C#做网站,用到了大量数据库相关的东西.网站采用3层结构,即数据访问层(Data Access Layer),业务逻辑层(Business Logic Layer),页面表现层().做了一段 ...
- (字典树)How many--hdu--2609
http://acm.hdu.edu.cn/showproblem.php?pid=2609 How many Time Limit: 2000/1000 MS (Java/Others) Me ...
- (KMP 模板)Number Sequence -- Hdu -- 1711
http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/Other ...
- Spring cache 缓存
概述 Spring 3.1 引入了激动人心的基于注释(annotation)的缓存(cache)技术,它本质上不是一个具体的缓存实现方案(例如 EHCache 或者 OSCache),而是一个对缓存使 ...
- Android-WebViewUtils-工具类
WebViewUtils-工具类是专门处理,Android API 中的WebView使用,公共方法抽取定义: package common.library.utils; import android ...
- LeetCode134:Gas Station
题目: There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. ...
- jQuery插件开发的五种形态小结
关于jQuery插件的开发自己也做了少许研究,自己也写过多个插件,在自己的团队了也分享过一次关于插件的课.开始的时候整觉的很复杂的代码,现在再次看的时候就清晰了许多.这里我把我自己总结出来的东西分享出 ...
- 一个基于ASP.NET(C#)的ACCESS数据库操作类
using System; using System.Collections; using System.Collections.Specialized; using System.Data; usi ...
- C# winform 记住密码实现代码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 无废话网页重构系列——(3)Web重构前的分析
本篇讲重构前的分析.从“工作状态.工作环境和工作角色”和具体重构工作两方面分析. 凡是经过考验的朋友,就应该把他们紧紧地团结在你的周围 比较理想的工作状态:制定了各种设计和开发规范,各团队之间邮件.团 ...