Check list only contains a certain set of values in Java

In this post we will provide two examples on how to verify that a list only contains a certain set of values in Java.

List only contains a certain set of values:

Example 1:

The example below shows how to verify a list only contains certain values using a the latest stream API with filtering.

Running the above code produces the following:

Example 2:

The next example converts the list into a set, so we only have distinct values. It then compares the set with another set of valid values.

Running the above code produces the following output:

Similar posts:

  1. How to split a string in Java
  2. How to use a CountDownLatch in Java
  3. How to sort an Array in Java
  4. Java 8 reduce operation

 

Leave a Reply