I have two identical array lists in java each having a string value and an integer count. Now I have to merge these array lists into a single one, in which if the value is present, i will just increment the count, if the value is not present, i will just add the value and the count as such.
The question is, is there anyway I can do it graciously other than iterating in a for loop and if checking every value?
You can’t, there’s too much custom logic. Iterate, check and add – that’s the best approach, and will be more readable.
Technically, you can use a
Multisetfrom guava, but there the count is taken care of by the collection itself, rather than you, so it might require some more work.