Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Lambda to traverse two List matching data in Java8

Shulou Source: shulou.com Published: 2022-06-01 10:38:32 10月04日 Update

Most people do not understand the knowledge points of this article "how to use Lambda to traverse two List matching data in Java8", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this article "how to use Lambda to traverse two List matching data in Java8".

Lambda traverses two List matching data 1. Define a static method / * by traversing two List that are equal by id attribute to resultList * @ param oneList * @ param twoList * / public static List compareListHitData (List oneList, List twoList) {List resultList = oneList.stream (). Map (map-> twoList.stream () .filter (m-> Objects.equals (m.get ("id")) Map.get ("id")) .findFirst () .map (m-> {map.putAll (m)) Return map;}) .orElse (null) .filter (Objects::nonNull) .filter (Collectors.toList ()); return resultList;} 2. Main method testing public static void main (String [] args) {List oneList = new ArrayList (); Map oneMap = new HashMap (); oneMap.put ("id", 111) OneMap.put ("userName", "he Jinrong"); Map twoMap = new HashMap (); twoMap.put ("id", 2222); twoMap.put ("userName", "Hejinrong"); oneList.add (oneMap); oneList.add (twoMap); List twoList = new ArrayList (); Map threeMap = new HashMap (); threeMap.put ("id", 111) ThreeMap.put ("userName", "he Jinrong"); Map fourMap = new HashMap (); fourMap.put ("id", 333); fourMap.put ("userName", "Hejinrong"); twoList.add (threeMap); twoList.add (fourMap); List resultList = compareListHitData (oneList, twoList); System.out.println (resultList);} 3. Output result

Jdk1.8 's stream processing of two List traversal matching data import java.util.ArrayList;import java.util.List;import java.util.Objects;import java.util.stream.Collectors; public class testStream {public static void main (String [] args) {List prizeRecords = new ArrayList (6); List stockDTOList = new ArrayList (); for (int I = 0; I

< 6; i++) { AwardInfo AwardInfo = new AwardInfo(); AwardInfo.setStockNo((i+1)+""); prizeRecords.add(AwardInfo); } for (int i = 0; i < 3; i++) { StockInfo stockDTO = new StockInfo(); stockDTO.setStockNo((i+1)+""); stockDTO.setThirdStockNo("third"+(i+1)); stockDTOList.add(stockDTO); } StockInfo stockDTO1 = new StockInfo(); stockDTO1.setStockNo((44)+""); stockDTO1.setThirdStockNo("third"+44); stockDTOList.add(stockDTO1); StockInfo stockDTO2 = new StockInfo(); stockDTO2.setStockNo((55)+""); stockDTO2.setThirdStockNo("third"+55); stockDTOList.add(stockDTO2); //prizeRecords与stockDTOList求差集 List resultList1 = prizeRecords.stream() .map(map ->

/ * difference set: refer to * [AwardInfo {userId='null', stockNo='1', thirdStockNo='null'}, * AwardInfo {userId='null', stockNo='2', thirdStockNo='null'}, * AwardInfo {userId='null', stockNo='3', thirdStockNo='null'}, * AwardInfo {userId='null', stockNo='4', thirdStockNo='null'} for failure result * AwardInfo {userId='null', stockNo='5', thirdStockNo='null'}, * AwardInfo {userId='null', stockNo='6', thirdStockNo='null'}] * / System.out.println (resultList1.toString ()) * / List resultList2 = prizeRecords.stream (). Map (m-> {stockDTOList.stream (). Filter (M2-> Objects.equals (m.getStockNo (), m2.getStockNo () .forEach (s-> m.setThirdStockNo (s.getThirdStockNo (); return m;}) .destroy (Collectors.toList ()); / * * the result of stockNo=4,5,6 is not removed! * [AwardInfo {userId='null', stockNo='1', thirdStockNo='third1'}, * AwardInfo {userId='null', stockNo='2', thirdStockNo='third2'}, * AwardInfo {userId='null', stockNo='3', thirdStockNo='third3'}, * AwardInfo {userId='null', stockNo='4', thirdStockNo='null'}, * AwardInfo {userId='null', stockNo='5', thirdStockNo='null'}, * AwardInfo {userId='null' StockNo='6', thirdStockNo='null'}] * / System.out.println (resultList2.toString ()) }) .orElse (null)) .filter (Objects::nonNull) .filter (Collectors.toList ()) The result of / * stockNo=4,5,6 has been removed * [AwardInfo {userId='null', stockNo='1', thirdStockNo='third1'}, * AwardInfo {userId='null', stockNo='2', thirdStockNo='third2'}, * AwardInfo {userId='null', stockNo='3', thirdStockNo='third3'}] * / System.out.println (list3.toString ()) } static class StockInfo {private String stockNo; private String stockName; private String thirdStockNo; public String getStockNo () {return stockNo;} public void setStockNo (String stockNo) {this.stockNo = stockNo;} public String getStockName () {return stockName } public void setStockName (String stockName) {this.stockName = stockName;} public String getThirdStockNo () {return thirdStockNo;} public void setThirdStockNo (String thirdStockNo) {this.thirdStockNo = thirdStockNo }} static class AwardInfo {private String userId; private String stockNo; private String thirdStockNo; public String getUserId () {return userId;} public void setUserId (String userId) {this.userId = userId;} public String getStockNo () {return stockNo } public void setStockNo (String stockNo) {this.stockNo = stockNo;} public String getThirdStockNo () {return thirdStockNo;} public void setThirdStockNo (String thirdStockNo) {this.thirdStockNo = thirdStockNo } the above is about the content of this article on "how to use Lambda to traverse two List matching data in Java8". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Tags: Two data content results subtractions articles methods knowledge articles Jinrong value most namely attributes more steps knowledge points industry information information channels Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Microsoft Apple NVidia Shulou Information