Let's say I have two lists:
List<String>products = new ArrayList<String>(); products.add("computer"); products.add("phone"); products.add("mouse"); products.add("keyboard"); List<String>cart = new ArrayList<String>(); cart.add("phone"); cart.add("monitor");
I need to find the number of items in the basket list in the product list. For the above lists, the answer will be 1 (since the phone is in products and carts). If the basket list was:
List<String>cart = new ArrayList<String>(); cart.add("desk"); cart.add("chair");
The result will be 0. If the basket has a computer, mouse, table, chair, the result will be 2 (for the computer and mouse).
Is there anything in Aparth Commons Collections or the Google Collections API ? I looked through them and looked at ways to calculate the amounts, but not from another list, although it is possible that I missed something. Right now, the only way I can think of is to iterate over the items in the basket and see if the products will contain a separate item and keep counting. I canβt use containsAll because I need a counter (and not a boolean) and this will fail if all the items in the basket don't exist in the product list (which could happen).
I use Java 1.6 if that matters.
source share