日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > java >内容正文

java

java guava 使用_Java8-Guava实战示例

發(fā)布時(shí)間:2025/3/21 java 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java guava 使用_Java8-Guava实战示例 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

示例一:

跟示例三對(duì)比一下,盡量用示例三

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)指向靜態(tài)方法的方法引用,(例如:Integer中的parseInt方法,寫作Integer::parseInt)

(2)指向任意類型實(shí)例方法的方法引用(例如String中的length方法,寫作String::length)

(3)指向現(xiàn)有對(duì)象的實(shí)例方法的方法引用(如下例)

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

List applySerialList= new ArrayList<>();

List operatorNoList =Lists.newArrayList(

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

示例四:

Lambad將List轉(zhuǎn)換成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);

}

});

示例六:

遍歷的時(shí)候需要使用到元素的索引,很可惜,Java8 的?Iterable?并沒(méi)有提供一個(gè)帶索引的?forEach?方法,自動(dòng)動(dòng)手寫一個(gè)滿足自己的需求。

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

注意:find()函數(shù)有兩個(gè)重載方法,其中一個(gè)是帶 defaultValue 的,注意如果別迭代的集合沒(méi)有符合條件的數(shù)據(jù)的話,一定要定義一個(gè)默認(rèn)值。否則會(huì)報(bào)NoSuchElementException異常

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

參考:

總結(jié)

以上是生活随笔為你收集整理的java guava 使用_Java8-Guava实战示例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。