Convenient method to check if a list contains multiple elements

java, list, string

Solution

`List.containsAll` function can actually achieve your requirement. However, using `containsAll` with list type can degrade in performance as the order is `O(NK)` where `N` is the number of elements of the list and K is the number of target string. Try using a `HashSet` to improve the performance:

   HashSet<String>set = new HashSet<>(list);
   if(set.containsAll(Arrays.asList("asd", "efg")))
        // do my job; 

Because, `HashSet<E>` class offers constant time performance for the basic operations (`add, remove, contains and size`). As `containsAll` inherently uses the `contains` function to look up the element the order can be expected as `O(N+K)`

Problem

How can I check if a list of strings contains some specific strings at the same time, without having to do multiple contains. For example something like this: ``` if (templist.contains("aaa") && templist.contains("bbb")) { } ```

Original source