title: Fetch subset from a superset using Java Streams
body: <p>I have a requirement to fetch the sublist from a given super list as explained below.</p>
  
  <p>Let's say I have a super list of "Person" objects as follows:</p>
  
  <pre><code>List&lt;Person&gt; personsList = new ArrayList&lt;&gt;();
  personsList.add(new Person("John", 28));
  personsList.add(new Person("Paul", 29));
  personsList.add(new Person("Adam", 30));
  personsList.add(new Person("Peter", 31));
  personsList.add(new Person("Pat", 32));
  </code></pre>
  
  <p>Let the Person class be defined as follows:</p>
  
  <pre><code>class Person {
      public String name;
      public int age;
  
      public Person(String name, int age) {
          this.name = name;
          this.age = age;
      }
  
      @Override
      public String toString() {
          return name + ": " + age;
      }
  }
  </code></pre>
  
  <p>Given a sub list like this:</p>
  
  <pre><code>String [] names = {"John", "Paul", "Adam"};
  List&lt;String&gt; namesList = Arrays.asList(names);
  </code></pre>
  
  <p>I would like to fetch the Person Objects where name is <code>John</code>, <code>Paul</code> and <code>Adam</code>.</p>
  
  <p>I am new to Java streams and this is what I have tried so far to meet my requirement.</p>
  
  <pre><code>List&lt;Person&gt; finalList = personsList.stream().filter(p -&gt; namesList.contains(p.name))
                  .collect(Collectors.toCollection(() -&gt; new ArrayList&lt;Person&gt;()));
  </code></pre>
  
  <p>I got the desired result, but I would like to know if there is any other efficient method to achieve this (I am mostly looking at namesList.contains(p.name) as a bit inefficient).</p>
  
  <p>Please let me know if there is a better way to do this.</p>