java guava 使用_Java8-Guava实战示例
发布日期:2021-06-24 13:19:14 浏览次数:3 分类:技术文章

本文共 4188 字,大约阅读时间需要 13 分钟。

示例一:

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

List invoiceQueryBeanList= new ArrayList<>();

List invoices =Lists.newArrayList(Iterators.transform(

invoiceQueryBeanList.iterator(),new Function() {

@Nullable

@OverridepublicString apply(@Nullable InvoiceQueryBean input) {if(StringUtils.isNotBlank(input.getLoanInvoiceId())) {returninput.getLoanInvoiceId();

}else{return null;

}

}

}));

//去除空的

Iterators.removeIf(invoices.iterator(), StringUtils::isBlank);

示例二:

public static List getInvoiceQueryPojoList(ListinvoiceQueryBean) {returnLists.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 implementsSerializable{private static final long serialVersionUID = -408985049449365784L;privateString loanInvoiceId;privateString userId;privateString userName;public static classBuilder {private PersonLoanInvoiceQueryPojo instance = newPersonLoanInvoiceQueryPojo();privateBuilder(){}public staticBuilder getInstance() {return newBuilder();

}public staticBuilder getInstance(PersonLoanInvoiceQueryPojo instance){

Builder builder= newBuilder();

builder.instance=instance;returnbuilder;

}publicBuilder addLoanInvoiceId(String loanInvoiceId) {this.instance.setLoanInvoiceId(loanInvoiceId);return this;

}publicBuilder addUserId(String userId) {this.instance.setUserId(userId);return this;

}publicBuilder addUserName(String userName) {this.instance.setUserName(userName);return this;

}publicPersonLoanInvoiceQueryPojo build() {return this.instance;

}

}

setters();&getters();

}

示例三:方法引用

方法引用主要有三类:

(1)指向静态方法的方法引用,(例如:Integer中的parseInt方法,写作Integer::parseInt)

(2)指向任意类型实例方法的方法引用(例如String中的length方法,写作String::length)

(3)指向现有对象的实例方法的方法引用(如下例)

importcom.google.common.collect.Iterators;importcom.google.common.collect.Lists;

List applySerialList= new ArrayList<>();

List operatorNoList =Lists.newArrayList(

Iterators.transform(applySerialList.iterator(), CreditPersonalInfoChangeApplySerial::getOperatorNo)); //这个叫做lambda的方法引用,注意方法引用的这个方法不需要()

示例四:

Lambad将List转换成Map

importcom.google.common.collect.Maps;

List operatorInfoList= new ArrayList<>();

MapoperatorMap=Maps.uniqueIndex(operatorInfoList.iterator(), QueryUserAppInfoByUserIdListPojo::getUserId);public class QueryUserAppInfoByUserIdListPojo implementsSerializable {private static final long serialVersionUID = 6876288995978264269L;privateString userId;publicString getUserId() {return this.userId;

}public voidsetUserId(String userId) {this.userId =userId;

}

}

示例五:

List list= new ArrayList<>();

list.forEach(input->{if(input.getCertificateKind().equals(EnumCertificateKind.RESIDENT_IDENTITY_CARD)) {

userCertificateMap.put(pojo.getUserId(), input);

}

});

示例六:

遍历的时候需要使用到元素的索引,很可惜,Java8 的 Iterable 并没有提供一个带索引的 forEach 方法,自动动手写一个满足自己的需求。

importjava.util.Objects;importjava.util.function.BiConsumer;/*** Iterable 的工具类*/

public classIterables {public static voidforEach(

Iterable extends E> elements, BiConsumeraction) {

Objects.requireNonNull(elements);

Objects.requireNonNull(action);int index = 0;for(E element : elements) {

action.accept(index++, element);

}

}

}

public static void main(String[] args) throwsException {

List 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

6b7254aa20cce50e9f577a6dda0d1689.png

注意:find()函数有两个重载方法,其中一个是带 defaultValue 的,注意如果别迭代的集合没有符合条件的数据的话,一定要定义一个默认值。否则会报NoSuchElementException异常

Iterators.find(pojoList.iterator(), input -> input != null, null);

参考:

转载地址:https://blog.csdn.net/weixin_33093437/article/details/114309074 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:python barrier option pricing_《Python金融数据分析》书内代码实战与讲解(二)金融衍生物定价...
下一篇:java integer 不变模式_Java代码的变与不变

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2024年04月06日 18时09分09秒