title: Using AssertJ, how do I perform complex assertions on the contents of a list?
body: <p>In AssertJ you can do the following to assert the contents of a list:</p>
  
  <pre><code>assertThat(list).extracting("name").containsExactlyInAnyOrder("Alice", "Bob");
  </code></pre>
  
  <p>I often find myself wanting to do more complex assertions on the elements themselves, e.g., asserting that Alice is a tall brunette and Bob is tiny and bald.
  What is the best way to do this using AssertJ?</p>
  
  <p>My own solution is to do:</p>
  
  <pre><code>assertThat(list).extracting("name").containsExactlyInAnyOrder("Alice", "Bob");
  list.stream()
      .filter(person -&gt; "Alice".equals(person.getName()))
      .forEach(alice -&gt; {
          assertThat(alice).extracting("size").isEqualTo("tall")
          assertThat(alice).extracting("hair").isEqualTo("brunette")
      });
  list.stream()
      .filter(person -&gt; "Bob".equals(person.getName()))
      .forEach(bob -&gt; {
          assertThat(bob).extracting("size").isEqualTo("tiny")
          assertThat(bob).extracting("hair").isNull()
      });
  </code></pre>
  
  <p>or equivalently (java 7) :</p>
  
  <pre><code>assertThat(list).extracting("name").containsExactlyInAnyOrder("Alice", "Bob");
  for(Person person : list){
      switch (testCase.getName()){
          case "Alice":
              assertThat(person).extracting("size").isEqualTo("tall")
              assertThat(person).extracting("hair").isEqualTo("brunette")
              break;
          case "Bob":
              assertThat(person).extracting("size").isEqualTo("tiny")
              assertThat(person).extracting("hair").isNull()
              break;
      }
  }
  </code></pre>
  
  <p>but I am wondering if there is a better solution.</p>
  
  <p>I like the fact that this solution makes a distinction between the expected elements being in the list and the elements themselves being correct.</p>