1

I have data:

Item: {String name,String count}

List<Item> listA =[{"a",10},{"b",10},{"c",10},{"d",10},{"e",10},{"f",10}]

List<Item> listB =[{"b",1},{"d",3},{"f",4},{"h",5}]

I want map data from listB to listA so I used code:

for (int i = 0; i < listB.size(); i++) {
   Item item= listB.get(i); // get element in listB
   for (int j = 0; j < listA.size(); j++) {
     if (item.getName().equals(listA.get(j).getName())) {
        listA.get(j).setCount(item.getCount());
     }
   }
}

My result:

listA =[{"a",10},{"b",1},{"c",10},{"d",3},{"e",10},{"f",4}]

My code working but I want do it better. Because it will duplicate item in for of listA. How I can do it better? Please help me. Thank you so much.

4
  • That's unclear what you want... The result list should contain one or two entries for "b" ? (because there are one in listA and one in listB) If only one, how do you choose which one you take ? Commented Apr 11, 2019 at 8:15
  • could you add expected result? Commented Apr 11, 2019 at 8:22
  • @You'reawesome I believe the expected result is "My Result" but The OP is looking for a more efficient way. Commented Apr 11, 2019 at 8:25
  • It will replace count from B to A if name ìn B and A sample. In my example count of b,d,f in A will replace with count of b,d,f in B. Commented Apr 11, 2019 at 8:37

2 Answers 2

1

I'm not sure of your Java version, but if you are using a higher version than Java 8, could you try this code below?

// Map is useful to remove duplicate data,
// so we will convert the list type to map.
Map<String, Integer> mapA = listA.stream()
    .collect(Collectors.toMap(Item::getName, Item::getCount));
Map<String, Integer> mapB = listB.stream()
    .collect(Collectors.toMap(Item::getName, Item::getCount));

// Let's put the data from mapA to mapB
mapB.entrySet().stream()
        .filter(entry -> mapA.containsKey(entry.getKey()))
        .forEach(entry -> mapA.put(entry.getKey(), entry.getValue()));

// Your expected result is list type, like below,
// [{"a": 10},{"b": 1},{"c": 10},{"d": 3},{"e": 10},{"f": 4}]
// convert it to list again!
List<Item> list = mapA.entrySet().stream()
    .map(o -> new Item(o.getKey(), o.getValue())).collect(Collectors.toList());
Sign up to request clarification or add additional context in comments.

Comments

0

Instead of a List try to create a HashMap. Then, loop through the entries of the mapB and update the mapA. It will automatically replace the values for keys that exist in the map and generate the entries that don't exist.

Example Code:

Map<String, Integer> mapA = createMapA() , mapB = createMapB();
mapB.entrySet().foreach(entry -> mapA.put(entry.getKey(), entry.getValue());

The lambda code style is in Java8 but the general idea remains the same if you have Java7.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.