示例一:

跟示例三对比一下,尽量用示例三

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实战示例的更多相关文章

  1. vue用mand-mobile ui做交易所移动版实战示例

    vue用mand-mobile ui做交易所移动版实战示例 先展示几个界面: 目录结构: main.js // The Vue build version to load with the `impo ...

  2. 148_赠送300家门店260亿销售额的零售企业Power BI实战示例数据

    焦棚子的文章目录 一背景 2022年即将到来之际,笔者准备在Power BI中做一个实战专题,作为实战专题最基础的就是demo数据,于是我们赠送大家一个300家门店,260亿+销售额,360万行+的零 ...

  3. Guava cache 示例

    pom.xml <!-- guava --> <dependency> <groupId>com.google.guava</groupId> < ...

  4. 基于Casperjs的网页抓取技术【抓取豆瓣信息网络爬虫实战示例】

    CasperJS is a navigation scripting & testing utility for the PhantomJS (WebKit) and SlimerJS (Ge ...

  5. 断言封装之key检查及kv实战示例

    ️️️️️️️️️️️️️️️️️️️️️️️️️️️️️ 测试: 断言处理: demo_04.pyimport jsonjson_obj = {"access_token":&q ...

  6. 乐字节-Java8核心特性实战之Lambda表达式

    大家好,小乐又来给大家分享Java8核心特性了,上一篇文章是<乐字节|Java8核心实战-接口默认方法>,这次就来讲Java8核心特征之Lambda表达式. Java8 引入Lambda表 ...

  7. Guava的常用方法示例

    Guava Maven Dependency <dependency> <groupId>com.google.guava</groupId> <artifa ...

  8. Java8函数式编程以及Lambda表达式

    第一章 认识Java8以及函数式编程 尽管距离Java8发布已经过去7.8年的时间,但时至今日仍然有许多公司.项目停留在Java7甚至更早的版本.即使已经开始使用Java8的项目,大多数程序员也仍然采 ...

  9. Java8 Stream流

    第三章 Stream流 <Java8 Stream编码实战>的代码全部在https://github.com/yu-linfeng/BlogRepositories/tree/master ...

随机推荐

  1. struts2 file

    JavaBean 中: private File[] pic; private String[] picContentType; private String [] picFileName; sett ...

  2. 11) 生成可执行jar文件 maven-shade-plugin

    搜索 site:maven.apache.org maven-assembly-plugin http://maven.apache.org/plugins/maven-assembly-plugin ...

  3. ubuntu 14.04查看java的安装路径

    有时候,使用apt-get install 安装了某个软件之后,却不知道这个软件的安装路径在哪里. 那怎么样去找出这个软件的安装路径呢? 下面我们java 这个软件为例, 找出ubuntu 14.04 ...

  4. Ansible之ansible-playbook roles

    刚开始学习运用 playbook 时,可能会把 playbook 写成一个很大的文件,到后来可能你会希望这些文件是可以方便去重用的,所以需要重新去组织这些文件. 基本上,使用 include 语句引用 ...

  5. 讲解java异常

    J2EE项目异常处理                        为什么要在J2EE项目中谈异常处理呢?可能许多java初学者都想说:“异常处理不就是try….catch…finally吗?这谁都会 ...

  6. DBCC--CHECKDB--使用快照还是表锁

    在SQL SERVER 2005及后续版本中, DBCC CHECKDB运行时不对数据表加排他锁,而使用内部数据库快照来保持数据一致性,内部数据库快照本质是sparse Filestream,使用Sp ...

  7. Latency Compensating Methods in Client/Server In-game Protocol Design and Optimization【转】

    https://developer.valvesoftware.com/wiki/Latency_Compensating_Methods_in_Client/Server_In-game_Proto ...

  8. [翻译]第一天 - 在 Windows 下安装和运行 .NET Core

    原文: http://michaelcrump.net/getting-started-with-aspnetcore/ 免责声明:我不是 .NET Core 开发团队的一员,并且使用的是公开.可用的 ...

  9. log4net 未生成log 原因分析

    本文假定你对log4net的配置以及在代码中的使用都非常熟悉,但就是没有按预想的生成log文件,正当你抓耳挠腮之时,那以下原因很可能是你解决问题的办法: 1.log4net.dll是否生成到程序运行目 ...

  10. Kotlin 初体验

    本文由作者邹丽萍授权网易云社区发布. 背景 Kotlin 是 JetBrains 公司(著名的 IntelliJ IDEA 正是由这家公司开发的,Android Studio 也是基于 IDEA 的) ...