title: Java 8 - Filter a string-X from a list using startsWith and Save the String-X to a list
body: <p>I am new to Java 8 and trying to get my head around how streams and filter works with list. I have a list of predefined strings and I have a string value which I am processing. I want to add the processed string to a new list if the string starts with any of the strings in the predefined list. If the string doesn't match any strings from the list then save it to another list. </p>
  
  <p>For example: </p>
  
  <pre><code>List&lt;String&gt; internalIpAddresses= new ArrayList&lt;&gt;();
  List&lt;String&gt; externalIpAddresses = new ArrayList&lt;&gt;();
  
  List&lt;String&gt; ipAddresseses = new ArrayList&lt;String&gt;();
  ipAddresses.add("10.");
  ipAddresses.add("132.174.");
  ipAddresses.add("192.168.");
  
  // filter internal ip addresses
  for(String ipAddress : ipAddresseses){
  
       if("10.11.12.13".startsWith(ipAddress)) {
            internalIpAddresses.add("10.11.12.13");
       }
  }
  
  // filter external ip addresses
  for(String ipAddress : ipAddresseses){
  
       if(!"5.6.7.8".startsWith(ipAddress)) {
            externalIpAddresses .add("5.6.7.8");
       }
  }
  </code></pre>
  
  <p>Result:</p>
  
  <pre><code>internalIpAddresses: 10.11.12.13
  externalIpAddresses : 5.6.7.8 
  </code></pre>
  
  <p>Is there a way this can be achieved in a simpler way using stream in java 8? </p>
  
  <p>Like:</p>
  
  <pre><code>ipAddresseses.stream()
       .filter(ipAddress -&gt; clientIpAddress.startsWith(ipAddress)
       .*if the clientIpAddress starts with any of the values in the list then add to internalIpAddresses List
       .*if clientIpAddress doesn't start with any values in list then add to externalIpAddresses List
  </code></pre>
  
  <p>In the end I want to save the <code>clientIpAddress</code> ("10.11.12.13" or "5.6.7.8"), not the values from the <code>ipAddresses</code> ("10." or "192.168.") list.</p>