Java - How to find the number of items in a list in another list

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.

+4
source share
7 answers

If you want to create another collection, you can do the following:

 List<String> productsInCart = new ArrayList<String>(products); productsInCart.retainAll(cart); 

This will give you all the entries that appear both in the basket and in the products.

+11
source
 int count = 0; for (String item : cart) { if (products.contains(item)) count++; } } 
+9
source

For one extra line of code to sort (plus the cost of execution time) and then calling another method to check containment, you can get something that can be very effective for a product list that is very long (as since the list is effective for random access, and the order product lists does not matter for the application):

 Collections.sort(products); // or maintain products in sort order at all times int count = 0; for(String i: cart) { if (Collections.binarySearch(products, i) >= 0) { count++; } } 
+4
source

I am sure that the static frequency method in Collections will come in handy here:

 List<String>products = ... List<String>cart = ... for (String cartItem : cart) { int occurrences = Collections.frequency(products, cartItem); if (occurrences > 0) { System.out.println(cartItem + ": " + occurrences); } } 

This is jdk 1.6 straight up. If the item in the cart list exists in the products, it will be printed with the number of entries. Just remove if -clause if you want to print 0-occurrences as well.

+2
source

If your data set is small or you do not need speed, the answer to the bart should be sufficient. But when you have a large data set and you do not want O (N * N) complexity, you can use this (if you do not duplicate the product name)

 Set<String> productsSet = new HashSet<String>(products); 

Then use the Bart code with products replaced by productsSet .

This will give you the result in O (N) time, although it will cost more memory.

+2
source

If creating another collection is not a limitation, simply add list items from both lists to Set. Then the difference in the final size and the sum of the sizes of the two lists is your answer

+1
source

Many of the published solutions work for "I need to find the number of items in the basket list in the product list." However, they may be based on different assumptions.

Some work, if you assume that each element of the product or basket should be unique. In this case, you can force this by using a HashSet instead of an ArrayList.

Others work even for repeated items in the cart, assuming that if the item is in the product, there is an unlimited amount of that specific product. This will not work if we cannot accept it, that is, suppose that the number of times an element appears in a product means the number of shares in the company. In this case, you might consider using other data structures such as HashMap.

I think in the end it comes down to the assumptions made. You must find out the exact conditions and determine which solution is best for your problem.

+1
source

Source: https://habr.com/ru/post/1307612/


All Articles