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 ...
随机推荐
- python编码(七)
本文中,以'哈'来解释作示例解释所有的问题,“哈”的各种编码如下: 1. UNICODE (UTF8-16),C854:2. UTF-8,E59388:3. GBK,B9FE. 一.python中的s ...
- HDU6024 Building Shops 2017-05-07 18:33 30人阅读 评论(0) 收藏
Building Shops Time Limit: 2000/1000 MS ...
- Android热补丁技术—dexposed原理简析(阿里Hao)
本文由嵌入式企鹅圈原创团队成员.阿里资深工程师Hao分享. 上篇文章<Android无线开发的几种常用技术>我们介绍了几种android移动应用开发中的常用技术,其中的热补丁正在被越来越多 ...
- C++ 中数组做参数的分析
C++ 中数组做参数的分析 1.数组降价问题? "数组引用"以避免"数组降阶",数组降阶是个讨厌的事,这在C语言中是个无法解决的问题,先看一段代码,了解什么是& ...
- ibatIs中的isNotNull、isEqual、isEmpty
isNull判断property字段是否是null,用isEmpty更方便,包含了null和空字符串 例子一:isEqual相当于equals,数字用得多些,一般都是判断状态值<isEqual ...
- 移动端Retina屏边框线1px 显示为2px或3px问题解决方法
我们在开发移动端web项目时经常遇到设置border:1px,但是显示的边框却为2px或是3px粗细,这是因为设备像素比devicePixelRatio为2或3引起的. 1.何为“设备像素比dev ...
- Emmet常用语法
Emmet常用语法1.输入!和html:5(不能大写),按下TAB 键,快速生成一个 HTML5 的标准文档初始结构. html:xt 生成 HTML4 过渡型 html:4s 生成 HTML4 严格 ...
- JS代码指导原则
一.什么是平稳退化? 如果含有JS代码的网页在用户浏览器不支持JS(或者禁用JS)时,用户仍然能够顺利浏览(网站功能正常,只是视觉效果可能差一些),那么这个网页就能够平稳退化 网页能够平稳退化是很必要 ...
- dorado-menu
1.menu控件是一个下拉菜单控件,可以设置数icon(图标),click事件,Dorado事件中都有self和arg两个参数,其中self是当前控件本身 2.menu控件可以和toolBar结合使用 ...
- Could not load file or assembly '$SharePoint.Project.AssemblyFullName$'
The fix is simple, do the following: 1. Open your project file in NotePad 2. Find the PropertyGrou ...