title: Java 8: More efficient way of comparing lists of different types?
body: <p>In a unit test, I want to verify that two lists contain the same elements. The list to test is build of a list of <code>Person</code> objects, where one field of type <code>String</code> is extracted. The other list contains <code>String</code> literals.</p>
  
  <p>One often finds the following code snippet to accomplish this task (see <a href="https://stackoverflow.com/questions/27641261/comparing-lists-of-different-types/27641329#27641329">this answer</a>):</p>
  
  <pre><code>List&lt;Person&gt; people = getPeopleFromDatabasePseudoMethod();
  List&lt;String&gt; expectedValues = Arrays.asList("john", "joe", "bill");
  
  assertTrue(people.stream().map(person -&gt; person.getName()).collect(Collectors.toList()).containsAll(expectedValues));
  </code></pre>
  
  <p>The <code>Person</code>class is defiend as:</p>
  
  <pre><code>public class Person {
  
      private String name;
      private int age;
  
      public String getName() {
          return name;
      }
  
      public void setName(final String name) {
          this.name = name;
      }
  
      // other getters and setters
  }
  </code></pre>
  
  <p>In the example above, the list of persons (or people) is transformed to a list of Strings using Java 8 techniques and the comparision is done in the old-fashioned way.</p>
  
  <p>Now I wonder, if there is a more direct or more efficient way of doing the comparison using other Java 8 statements, for example <code>allMatch()</code> or some <code>Predicate&lt;T&gt;</code> or something else.</p>