Skip to content
Advertisement

how to combine two List<Map> [closed]

There are these two List<Map<String,Object>> in java

List<Map<String,Object>> voListResult = new ArrayList<Map<String,Object>>();

voListResult:

[{product_idx=1, likecheck=0, product_price=25000,  product_title=크롭트 가디건}, 
{product_idx=2, likecheck=0, product_price=25000, product_title=크롭트 가디건}, 
{product_idx=3, likecheck=0, product_price=25000,  product_title=크롭트 가디건}, 
{product_idx=4, likecheck=0, product_price=25000,  product_title=크롭트 가디건}, 
{product_idx=5, likecheck=0, product_price=25000,  product_title=크롭트 가디건}, 
{ product_idx=6, likecheck=0, product_price=25000, , product_title=크롭트 가디건},
 {product_idx=7, likecheck=0, product_price=25000,  product_title=크롭트 가디건}, 
{product_idx=8, likecheck=0, product_price=25000,  product_title=크롭트 가디건}, 
{product_idx=9, likecheck=0, product_price=25000,  product_title=크롭트 가디건}]

List<Map<String, Object>> productLikeRenew = likeDAO.SumProductLike();

productLikeRenew:

[{product_idx=3, likecheck=2}, {product_idx=10, likecheck=2},
{product_idx=12, likecheck=1}, {product_idx=5, likecheck=1},
{product_idx=7, likecheck=1}, {product_idx=11, likecheck=1},
{product_idx=13, likecheck=1},{product_idx=20, likecheck=1},
{product_idx=30, likecheck=1},{product_idx=40, likecheck=1}, 
{product_idx=59, likecheck=1}]

How can I update likecheck based on product_idx of both Lists in voListResult?

result:

[{product_idx=1, likecheck=0, product_price=25000,  product_title=크롭트 가디건}, 
{product_idx=2, likecheck=0, product_price=25000, product_title=크롭트 가디건}, 
{product_idx=3, likecheck=2, product_price=25000,  product_title=크롭트 가디건}, 
{product_idx=4, likecheck=0, product_price=25000,  product_title=크롭트 가디건}, 
{product_idx=5, likecheck=0, product_price=25000,  product_title=크롭트 가디건}, 
{ product_idx=6, likecheck=0, product_price=25000, , product_title=크롭트 가디건},
 {product_idx=7, likecheck=1, product_price=25000,  product_title=크롭트 가디건}, 
{product_idx=8, likecheck=0, product_price=25000,  product_title=크롭트 가디건}, 
{product_idx=9, likecheck=0, product_price=25000,  product_title=크롭트 가디건},
{product_idx=10, likecheck=2, product_price=25000,  product_title=크롭트 가디건}
...]

I’ve been thinking about it for a few days, but it doesn’t work.

Advertisement

Answer

You can do the following things:-

  1. Sort both List of maps based on the product_idx number.
  2. define a counter for productLikeRenew and initialize with 0.
  3. Iterate voListResult list until product_idx of voListResult <= product_idx of productLikeRenew.
  4. If product_idx of both of them is equal then combine the result.
  5. Increment the counter.
  6. Repeat steps from 3 to 5 until one of the List is ended.
Advertisement