Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use stream to merge object attributes in jdk8

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

The knowledge of this article "how to use stream to merge object attributes in jdk8" is not understood by most people, 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 "how to use stream to merge object attributes in jdk8" article.

Java uses stream to merge object attributes in list: merging into a List based on the same field in two List, including fields in two List

I. Preface

Why do you use Lambda expressions and Stream streams for collection processing? Because of high efficiency, simple code, high-end atmosphere!

(1) description: for example, List in a List collection, object 1 contains columns A, B, and C, and List in another List collection, object 2 contains columns An and D.

(2) problem: two List composite List needs to be returned. This object 3 contains columns A, B, C, and D.

(3) implementation: using stream to merge two list collections into one list set

Brief description: the use of filter is: filter (logical judgment of item-> item), in which filter will keep the record whose judgment condition is true.

Example 1:java8 merges two list < map >

Java8 merges two list < map > and merges all attributes of the same userId into one map

Properties of objects in list1: userId, userName

Properties of objects in list2: userId, gender, age

Properties of objects in the most total collection: userId, userName, gender, age

Import java.util.*;import java.util.stream.Collectors;/** * @ author qzz * / public class ListTest {public static void main (String [] args) {mergeList () } / * * @ Description: merge two list and merge other attributes with the same userId * @ Title: mergeList * @ param: @ return * @ return: List * @ throws * / public static List mergeList () {/ / build the List collection 1 List list1 = new ArrayList (); Map data=new HashMap (); data.put ("userId", "100001") Data.put ("userName", "Tang Monk"); list1.add (data); data=new HashMap (); data.put ("userId", "100002"); data.put ("userName", "Bajie"); list1.add (data); data=new HashMap (); data.put ("userId", "100003"); data.put ("userName", "Wukong") List1.add (data); data=new HashMap (); data.put ("userId", "100004"); data.put ("userName", "Sha Wujing"); list1.add (data); / / build List collection 2 List list2 = new ArrayList (); data=new HashMap (); data.put ("userId", "100001") Data.put ("gender", "male"); data.put ("age", 20); list2.add (data); data=new HashMap (); data.put ("userId", "100002"); data.put ("gender", male); data.put ("age", 1000); list2.add (data); data=new HashMap () Data.put ("userId", "100003"); data.put ("gender", "male"); data.put ("age", 100003); list2.add (data); data=new HashMap (); data.put ("userId", "100004"); data.put ("gender", "male"); data.put ("age", 800); list2.add (data) / use stream stream to merge list1 and list2 into a list collection based on attribute userId List list = list1.stream (). Map (m-> {list2.stream (). Filter (M2-> Objects.equals (m.get ("userId"), m2.get ("userId")) .forEach (m2-> {m.put ("gender", m2.get ("gender") M.put ("age", m2.get ("age");}); return m;}) .destroy (Collectors.toList ()); for (Map map:list) {System.out.println (map.toString ());} return list;}}

Merge operation results:

Example 2:java8 merges two list < T >

Java8 merges two list < T > and merges all attributes of the same tickeId into one T

Properties of objects in list1: ticketId, ticketName

Properties of objects in list2: ticketId, saleNum, batchAvailableNum

Properties of objects in the most total collection: ticketId, ticketName, saleNum, batchAvailableNum

Import java.util.*;import java.util.stream.Collectors;/** * @ author qzz * / public class ListTest1 {public static void main (String [] args) {mergeList () } / * * @ Description: merge two list and merge other attributes with the same ticketId * @ Title: mergeList * @ param: @ return * @ return: List * @ throws * / public static List mergeList () {/ / build the List collection 1 List list1 = new ArrayList (); Ticket data=new Ticket (); data.setTicketId ("100001") Data.setTicketName (Tang Monk); list1.add (data); data=new Ticket (); data.setTicketId ("100002"); data.setTicketName ("Bajie"); list1.add (data); data=new Ticket (); data.setTicketId ("100003"); data.setTicketName (Wukong); list1.add (data); data=new Ticket () Data.setTicketId ("100004"); data.setTicketName ("Sha Wujing"); list1.add (data); / / build the List collection 2 List list2 = new ArrayList (); data=new Ticket (); data.setTicketId ("100001"); data.setSaleNum ("20"); data.setBatchAvailableNum ("10"); list2.add (data) Data=new Ticket (); data.setTicketId ("100001"); data.setSaleNum ("20"); data.setBatchAvailableNum ("10"); list2.add (data); data=new Ticket (); data.setTicketId ("100002"); data.setSaleNum ("1000"); data.setBatchAvailableNum ("1000"); list2.add (data); data=new Ticket () Data.setTicketId ("100003"); data.setSaleNum ("100th"); data.setBatchAvailableNum ("100th"); list2.add (data); data=new Ticket (); data.setTicketId ("100004"); data.setSaleNum ("100004"); data.setBatchAvailableNum ("300"); list2.add (data) / use stream stream to merge list1 and list2 into a list collection based on attribute ticketId List list = list1.stream (). Map (m-> {list2.stream (). Filter (M2-> Objects.equals (m.getTicketId (), m2.getTicketId () .forEach (M2-> {m.setSaleNum (m2.getSaleNum ()); m.setBatchAvailableNum (m2.getBatchAvailableNum () For (Ticket ticket:list) {System.out.println (ticket.getTicketId () + "," + ticket.getTicketName () + "," + ticket.getSaleNum () + "," + ticket.getBatchAvailableNum ());} return list;}}

Merge operation results:

Example 3:java8 merges two list < T >. The number of collections in the former is less than the latter, and the latter is needed.

Properties of objects in list1: ticketId, ticketName

Properties of objects in list2: ticketId, batchId

Properties of objects in the most total collection: ticketId, ticketName, batchId

List1:

TicketId:10001, ticketName: Tang Monk

TicketId:10002, ticketName: Bajie

List2:

TicketId:10001, batchId:1

TicketId:10001, batchId:2

TicketId:10002, batchId:1

TicketId:10002, batchId:2

TicketId:10003, batchId:2

Desired result: list1 merged into list 2

TicketId:10001, ticketName: Tang Monk, batchId:1

TicketId:10001, ticketName: Tang Monk, batchId:2

TicketId:10002, ticketName: Bajie, batchId:1

TicketId:10002, ticketName: Bajie, batchId:2

TicketId:10003, ticketName: Bajie, batchId:2

Code implementation:

Import java.util.ArrayList;import java.util.List;import java.util.Objects;import java.util.stream.Collectors;/** * @ author qzz * / public class ListTest2 {public static void main (String [] args) {mergeList () } / * * @ Description: merge two list and merge other attributes with the same userId * @ Title: mergeList * @ param: @ return * @ return: List * @ throws * / public static List mergeList () {List list1 = new ArrayList (); Ticket data=new Ticket (); data.setTicketId ("100001"); data.setTicketName ("Tang Monk") List1.add (data); data=new Ticket (); data.setTicketId ("100002"); data.setTicketName ("Bajie"); list1.add (data); List list2 = new ArrayList (); data=new Ticket (); data.setTicketId ("100001"); data.setBatchId ("1"); list2.add (data); data=new Ticket () Data.setTicketId ("100001"); data.setBatchId ("2"); list2.add (data); data=new Ticket (); data.setTicketId ("100002"); data.setBatchId ("1"); list2.add (data); data=new Ticket (); data.setTicketId ("100002"); data.setBatchId ("2"); list2.add (data) Data=new Ticket (); data.setTicketId ("100002"); data.setBatchId ("3"); list2.add (data) / / merge list1 into the list2 collection using the stream stream, according to the ticketId attribute List list = list2.stream (). Map (m-> {list1.stream (). Filter (M2-> Objects.equals (m.getTicketId (), m2.getTicketId () .forEach (M2-> {m.setTicketId (m2.getTicketId ()); m.setTicketName (m2.getTicketName ());}) Return m;}) .duration (Collectors.toList ()); for (Ticket ticket:list) {System.out.println (ticket.getTicketId ()) + "," + ticket.getTicketName () + "," + ticket.getBatchId ());} return list;}}

Merge operation results:

Example 4:java8 merges two list < T >. The number of collections of the former is larger than that of the latter.

Properties of objects in list1: ticketId, ticketName

Properties of objects in list2: ticketId, batchId

Properties of objects in the most total collection: ticketId, ticketName, batchId

List1:

TicketId:10001, ticketName: Tang Monk

TicketId:10002, ticketName: Bajie

List2:

TicketId:10001, batchId:1

TicketId:10001, batchId:2

Desired result: list1 is merged into list 2 to filter out data that does not exist in batch

TicketId:10001, ticketName: Tang Monk, batchId:1

TicketId:10001, ticketName: Tang Monk, batchId:2

Code implementation:

Import java.util.ArrayList;import java.util.List;import java.util.Objects;import java.util.stream.Collectors;/** * @ author qzz * / public class ListTest3 {public static void main (String [] args) {mergeList () } / * * @ Description: merge two list and merge other attributes with the same userId * @ Title: mergeList * @ param: @ return * @ return: List * @ throws * / public static List mergeList () {List list1 = new ArrayList (); Ticket data=new Ticket (); data.setTicketId ("100001"); data.setTicketName ("Tang Monk") List1.add (data); data=new Ticket (); data.setTicketId ("100002"); data.setTicketName ("Bajie"); list1.add (data); List list2 = new ArrayList (); data=new Ticket (); data.setTicketId ("100001"); data.setBatchId ("1"); list2.add (data); data=new Ticket () Data.setTicketId ("100001"); data.setBatchId ("2"); list2.add (data); / / data=new Ticket (); / / data.setTicketId ("100002"); / / data.setBatchId ("1"); / / list2.add (data); / data=new Ticket (); / / data.setTicketId ("100002") / / data.setBatchId ("2"); / / list2.add (data); / data=new Ticket (); / / data.setTicketId ("100002"); / / data.setBatchId ("3"); / / list2.add (data) / / merge list1 into the list2 collection using the stream stream, according to the ticketId attribute List list = list2.stream (). Map (m-> {list1.stream (). Filter (M2-> Objects.equals (m.getTicketId (), m2.getTicketId () .forEach (M2-> {m.setTicketId (m2.getTicketId ()); m.setTicketName (m2.getTicketName ());}) Return m;}) .duration (Collectors.toList ()); for (Ticket ticket:list) {System.out.println (ticket.getTicketId ()) + "," + ticket.getTicketName () + "," + ticket.getBatchId ());} return list;}}

Merge operation results:

The above is about the content of this article on "how to use stream to merge object attributes in jdk8". I believe we all have a certain 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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report