Java8-Guava实战示例

示例一:

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

List<InvoiceQueryBean> invoiceQueryBeanList = new ArrayList<>();
List<String> invoices = Lists.newArrayList(Iterators.transform(invoiceQueryBeanList.iterator(), new Function<InvoiceQueryBean, String>() {@Nullable@Overridepublic 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);

 

参考:

转载于:https://www.cnblogs.com/happyflyingpig/p/9004534.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/353639.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java项目构建部署包

博客分类&#xff1a; JAVA Java 工程在生产环境运行时&#xff0c;一般需要构建成一个jar&#xff0c;同时在运行时需要把依赖的jar添加到classpath中去&#xff0c;如果直接运行添加classpath很不方便&#xff0c;比较方便的是创建一个shell脚本。在公司项目中看到把工程代码…

不错的自学网站合集

一、网站类1、假期在家如何查找论文资料&#xff1f;只需登录中国图书馆http://t.cn/hYmDq&#xff08;需注册&#xff09;&#xff0c;即可免费下载各种期刊和学位论文2、全球免费开放的电子图书馆http://t.cn/h4hJUf3、给大家推荐个神网站&#xff0c;只要是外文书籍基本上都…

c++中关于字符串的读入——cin、getline、get、gtes(查询+思考+总结)

1、cin读入一个字符&#xff1a;char c;cin>>c;2、cin读入一个字符串&#xff1a;char s[10];cin >> s;&#xff08;c风格字符串&#xff09; string str;cin >> str;&#xff08;c的string&#xff09;3、cin.get()读入一个字符&#xff1a;char c;ccin.…

java文档注释和标题注释_Java注释:探究和解释

java文档注释和标题注释Java 5 SE的许多出色功能之一是Annotations构造的引入。 注释是一些标签&#xff0c;可以将其插入到程序源代码中&#xff0c;以使用某种工具对其进行处理并使其变得有意义。 注释处理工具通常使用&#xff08;Java 5 SE的&#xff09;Reflection API在J…

shell 脚本初步,启动可执行 jar 文件

可能很多同学在看到这篇文章的时候是第一次接触 shell 脚本。所以我们首先需要了解什么是 shell 脚本。在 Windows 里我们经常会看到一种扩展名为 .bat 的文件&#xff0c;它称为批处理文件。批处理文件的作用是把许多个命令放在一个文件里&#xff0c;当运行这个文件的时候就执…

使用Java创建DynamoDB表

在这篇文章中&#xff0c;我们将使用java方法在DynamoDB数据库上创建表。 在开始之前&#xff0c;我们需要安装本地dynamodb&#xff0c;因为我们要避免使用dynamodb的任何费用。 有一个以前的岗位上本地dynamodb。 如果您使用docker&#xff0c;则可以找到本地dynamodb映像&a…

表达式前后缀表达形式 [zz]

(2012-09-12 13:08:39) 转载▼标签&#xff1a; 杂谈 转自&#xff1a;http://blog.csdn.net/whatforever/article/details/673853835,15,,80,70,-,*,20,/ //后缀表达方式(((3515)*(80-70))/20&#xff09;25 //中缀表达方式 /,*,,35,15,-,80,70, 2…

引用:初探Sql Server 执行计划及Sql查询优化

引用:初探Sql Server 执行计划及Sql查询优化 原文:引用:初探Sql Server 执行计划及Sql查询优化初探Sql Server 执行计划及Sql查询优化 收藏MSSQL优化之————探索MSSQL执行计划作者&#xff1a;no_mIss最近总想整理下对MSSQL的一些理解与感悟&#xff0c;却一直没有心思和时间…

Cities

问题 C: Cities 时间限制: 1 Sec 内存限制: 128 MB提交: 87 解决: 61[提交][状态][讨论版][命题人:admin]题目描述 There are n cities in Byteland, and the ith city has a value ai. The cost of building a bidirectional road between two cities is the sum of their v…

spring观察者模式_Spring事件的观察者模式

spring观察者模式介绍 观察者模式的本质是“定义对象之间的一对多依赖关系&#xff0c;以便当一个对象改变状态时&#xff0c;其所有依赖关系都会被通知并自动更新”。 GoF。 观察者模式是发布/订阅模式的子集&#xff0c;它允许许多观察者对象查看事件。 可以在不同的情况下使…

Lombok,自动值和不可变项

我喜欢布兰登&#xff08;Brandon &#xff09;在博客文章中比较Project Lombok &#xff0c; AutoValue和Immutables的建议 &#xff0c;而这篇文章试图做到这一点。 我已经简要概述了Project Lombok &#xff0c; AutoValue和Immutables &#xff0c;但是这篇文章有所不同&am…

linux对于zombie的处理

(Linux基础)[僵尸进程处理] 今天在服务器上推送项目的时候&#xff0c;突然发现很卡。就用top查看了一下&#xff0c;果然此事不简单啊。 top - 10:39:16 up 20 days, 23:11, 2 users, load average: 1.13, 1.09, 1.03 Tasks: 204 total, 2 running, 196 sleeping, 1 sto…

POJ - 1847 Tram(dijkstra)

题意&#xff1a;有向图有N个点&#xff0c;当电车进入交叉口&#xff08;某点&#xff09;时&#xff0c;它只能在开关指向的方向离开。 如果驾驶员想要采取其他方式&#xff0c;他/她必须手动更换开关。当驾驶员从路口A驶向路口B时&#xff0c;他/她尝试选择将他/她不得不手动…

用interrupt()中断Java线程

Javathread 最近在学习Java线程相关的东西&#xff0c;和大家分享一下&#xff0c;有错误之处欢迎大家指正&#xff0e; 假如我们有一个任务如下&#xff0c;交给一个Java线程来执行&#xff0c;如何才能保证调用interrupt()来中断它呢&#xff1f; Java代码 class ATask imple…

activemq和jms_保证主题,JMS规范和ActiveMQ的消息传递

activemq和jms最近&#xff0c;一位客户要求我仔细研究ActiveMQ的“持久”消息的实现&#xff0c;它如何应用于主题以及在存在非持久订户的故障转移方案中会发生什么。 我已经了解到&#xff0c;JMS语义规定&#xff0c;即使面对消息代理提供程序故障&#xff0c;也只能保证主题…

JAVA分代收集机制详解

Java堆中是JVM管理的最大一块内存空间。主要存放对象实例。在JAVA中堆被分为两块区域&#xff1a;新生代&#xff08;young&#xff09;、老年代&#xff08;old&#xff09;。堆大小新生代老年代&#xff1b;&#xff08;新生代占堆空间的1/3、老年代占堆空间2/3&#xff09;新…

FizzBu​​zz Kata与Java流

在柔道练习仅几周之后&#xff0c;我的儿子感到无聊。 他抱怨说自己没有学任何东西&#xff0c;因为他一遍又一遍地做着同样的事情。 混淆学习和做新事物的不仅仅是幼儿。 例如&#xff0c;有多少软件开发人员通过执行kata或参加dojos来进行刻意练习的麻烦&#xff1f; 重复您…

高可用架构

转载于:https://www.cnblogs.com/138026310/p/9088341.html

vc6.0快捷键

2010-09-14 17:46 F1 显示帮助,如果光标停在代码的某个字符上,显示MSDN中相应的帮助内容&#xff08;需要安装MSDN才能使用&#xff09; F2 书签功能: CtrlF2 --在某行设置一个书签(再按一次是取消) F2 --跳到下一个书签位置 ShiftF2 --跳到上一个书签位置 CtrlShiftF2 --删除…

ES6 各浏览器支持情况

http://kangax.github.io/compat-table/es6/转载于:https://www.cnblogs.com/likwin/p/9091008.html