Answer the question
In order to leave comments, you need to log in
How to merge 2 ArrayList into 1 by matching value (id)?
There are 2 ArrayLists
List<Product> productListWithUrlName = new ArrayList<>();
List<Product> productListWithPrice = new ArrayList<>();
Answer the question
In order to leave comments, you need to log in
Good afternoon!
One possible solution:
- create a Map<Long, Product>
Long - product id
- Add the elements of the first List to the Map.
- Iterate over the second List. Inside the loop, get the item from the Map by the object ID from the second List.
- Use setters to assign values.
- If there is no product with this ID, then add it to the Map
Here, a similar question - https://stackoverflow.com/questions/12670756/join-...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question