title: How to get the count from a stream with an if condition inside the map?
body: <pre><code>List&lt;Employee&gt; Employees = getEmployeeList();
  AtomicInteger count = new AtomicInteger();
  
  Employees.stream()
          .map(item -&gt; {
              UserProfile user = userDao.findById(item.getUserProfileId());
              if((&quot;domainName&quot;).equals(user.getDomain()) &amp;&amp; (&quot;agencyCode&quot;).equals(user.getAgencyNumber())){
                  count.getAndIncrement();
              }
              return item;
          }).collect(Collectors.toList());
  </code></pre>
  <p>I need the count of the employee which satisfies the <code>if</code> condition. How can I remove the <code>count</code> variable and instead use the <code>count()</code> method of the Stream API?</p>
  <p>Clarification : I wanted to filter out the employees which satisfy the current conditions mentioned in If block while i just have to call it once. what i was doing earlier, i will show.</p>
  <pre><code>List&lt;Employee&gt; Employees = getEmployeeList();
                  Employee employee = Employees.stream()
                  .filter(item -&gt; (&quot;domainName&quot;).equals(userDao.findById(item.getUserProfileId()).getDomain()))
                  .filter(item -&gt; (&quot;agencyCode&quot;).equals(userDao.findById(item.getUserProfileId()).getAgencyNumber()))
                  .findAny().orElse(null);
  </code></pre>
  <p>So i got the requirement to call the userDao method only once.</p>