title: Conditional statement in java8 forEach loop
body: <p>I have string which contain  number of department separated by  comma.
  I have to  feed this data to sql 'IN' clause.</p>
  
  <p>I want conditional check for last element, I do not want to append 'comma' after 
  last element. I don`t know how to use condition inside java 8 forEach loop.</p>
  
  <pre><code>public String  getDeptInClauseParameters(String depts){
         StringBuilder deptsInParameter= new StringBuilder();
         Stream&lt;String&gt;  stream= Arrays.stream(depts.split(","));
         stream.forEach(dept -&gt;  deptsInParameter.append("'").append(dept).append("' , "));
          return deptsInParameter.toString();
      }
  </code></pre>