title: Java 8 Streams - Compare two Lists' object values and add value to new List?
body: <p>I have two <code>List</code>s containing objects of this class:</p>
  <pre><code>public class SchoolObj
  {
      private String name;
      private String school;
  
      public SchoolObj()
      {
          this(null, null);
      }
  
      public SchoolObj(String nameStr, String schoolStr)
      {
          this.setName(nameStr);
          this.setSchool(schoolStr);
      }
  
      public String getName()
      {
          return this.name;
      }
  
      public void setName(String name)
      {
          this.name = name;
      }
  
      public String getSchool()
      {
          return this.school;
      }
  
      public void setSchool(String school)
      {
          this.school = school;
      }
  
      @Override
      public String toString()
      {
          return this.getName() + ' ' + this.getSchool();
      }
  }
  </code></pre>
  <p>I want to compare the objects in those two lists by <code>name</code> and <code>school</code>. If they are equal I need to create a new <code>List</code> containing those <code>SchoolObj</code> objects which are found in both Lists.</p>
  <p>I know we can use two <code>for</code> loops and do it is in the <code>createSharedListViaLoop</code> method below.</p>
  <p>My question is, how can I accomplish the same thing with Java streams?</p>
  <p>I tried with <code>createSharedListViaStream</code> below, but it is not working as expected.</p>
  <pre><code>import java.util.ArrayList;
  import java.util.List;
  import java.util.stream.Collectors;
  
  public class StreamTest
  {
      public static void main(String[] args)
      {
          List&lt;SchoolObj&gt; listOne = new ArrayList&lt;SchoolObj&gt;();
          // TODO: Add sample data to listOne.
          listOne.add(new SchoolObj(&quot;nameA&quot;, &quot;schoolX&quot;));
          listOne.add(new SchoolObj(&quot;nameC&quot;, &quot;schoolZ&quot;));
  
          List&lt;SchoolObj&gt; listTwo = new ArrayList&lt;SchoolObj&gt;();
          // TODO: Add sample data to listTwo.
          listTwo.add(new SchoolObj(&quot;nameA&quot;, &quot;schoolX&quot;));
          listTwo.add(new SchoolObj(&quot;nameB&quot;, &quot;schoolY&quot;));
  
          // Print results from loop method.
          System.out.println(&quot;Results from loop method:&quot;);
          List&lt;SchoolObj&gt; resultsViaLoop = StreamTest.createSharedListViaLoop(listOne, listTwo);
          for (SchoolObj obj : resultsViaLoop)
          {
              System.out.println(obj);
          }
  
          // Print results from stream method.
          System.out.println(&quot;Results from stream method:&quot;);
          List&lt;SchoolObj&gt; resultsViaStream = StreamTest.createSharedListViaStream(listOne, listTwo);
          for (SchoolObj obj : resultsViaStream)
          {
              System.out.println(obj);
          }
      }
  
      public static List&lt;SchoolObj&gt; createSharedListViaLoop(List&lt;SchoolObj&gt; listOne, List&lt;SchoolObj&gt; listTwo)
      {
          List&lt;SchoolObj&gt; result = new ArrayList&lt;SchoolObj&gt;();
  
          for (SchoolObj one : listOne)
          {
              for (SchoolObj two : listTwo)
              {
                  if (one.getName().equals(two.getName()) &amp;&amp; one.getSchool().equals(two.getSchool()))
                  {
                      result.add(one);
                  }
              }
          }
  
          return result;
      }
  
      public static List&lt;SchoolObj&gt; createSharedListViaStream(List&lt;SchoolObj&gt; listOne, List&lt;SchoolObj&gt; listTwo)
      {
          List&lt;SchoolObj&gt; listOneList = listOne.stream().filter(two -&gt; listTwo.stream()
                .anyMatch(one -&gt; one.getName().equals(two.getName()) &amp;&amp; two.getSchool().equals(one.getSchool()))) 
                .collect(Collectors.toList());
  
          return listOneList;
      }
  }
  </code></pre>