跟示例三對比一下,儘可能用示例三java
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(); }
方法引用主要有三類:app
(1)指向靜態方法的方法引用,(例如:Integer中的parseInt方法,寫做Integer::parseInt)ide
(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轉換成Mapui
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
方法,自動動手寫一個知足本身的需求。this
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)); }
注意:find()函數有兩個重載方法,其中一個是帶 defaultValue 的,注意若是別迭代的集合沒有符合條件的數據的話,必定要定義一個默認值。不然會報NoSuchElementException異常google
Iterators.find(pojoList.iterator(), input -> input != null, null);
參考:spa