0

Find what items in the store match the items in the list. Save this list as the best list of items so far

That is the line in the pseudocode that I need to translate into java code. I have two lists one called itemsNeeded and one called itemsFound. I need to see if the itemsNeeded list contains any items in the itemsFound list. Then I need to save that list. Can anyone demonstrate this?

2 Answers2

1

You could take a brute force approach and do it this way...

Say you have a List to store the matches in, called saveList. I'm also assuming the items in the lists are of type String, but this should work with any Object.

for(String item : itemsNeeded) {
  if(itemsFound.contains(item)) {
    saveList.add(item);
  }
}
Ryan Thames
  • 3,204
  • 6
  • 31
  • 32
0

You could use: http://commons.apache.org/collections/apidocs/org/apache/commons/collections/CollectionUtils.html

predicatedCollection method

and an EqualPredicate with itemsNeeded list

Ignasi
  • 5,887
  • 7
  • 45
  • 81