Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 - Filter a string-X from a list using startsWith and Save the String-X to a list

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.

For example:

List<String> internalIpAddresses= new ArrayList<>();
List<String> externalIpAddresses = new ArrayList<>();

List<String> ipAddresseses = new ArrayList<String>();
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");
     }
}

Result:

internalIpAddresses: 10.11.12.13
externalIpAddresses : 5.6.7.8 

Is there a way this can be achieved in a simpler way using stream in java 8?

Like:

ipAddresseses.stream()
     .filter(ipAddress -> 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

In the end I want to save the clientIpAddress ("10.11.12.13" or "5.6.7.8"), not the values from the ipAddresses ("10." or "192.168.") list.

like image 562
AMagic Avatar asked Jan 08 '19 17:01

AMagic


People also ask

How do you filter a string in Java 8?

Using Java 8 In Java 8 and above, use chars () or codePoints () method of String class to get an IntStream of char values from the given sequence. Then call the filter () method of Stream for restricting the char values to match the given predicate.

How to filter a list with a predicate in Java 8?

First the list is converted into a stream. This stream is then filtered with the predicate and the stream is converted back into a list with the collect () method. Thanks to the lambda notation of Java 8, the Predicate can also be passed as a simple function, which shortens the expression even more: makes the filtering even clearer.

What are the Java 8 streams filter examples?

Java 8 Streams filter examples. 1 1. Streams filter () and collect () 2 2. Streams filter (), findAny () and orElse () 3 3. Streams filter () and map ()

How to filter string list by starting value in Python?

To filter String list by starting value, use filter () and startsWith () − list.stream ().filter ((b) -> b.startsWith ("w")) The following is an example to filter string list by starting value −


2 Answers

Simply, your iterative code using streams could be represented as :

List<String> ipAddresses = Arrays.asList("10.", "132.174.", "192.168.");

List<String> internalIpAddresses = ipAddresses.stream()
        .filter("10.11.12.13"::startsWith)
        .map(ipAddress -> "10.11.12.13")
        .collect(Collectors.toList());

List<String> externalIpAddresses = ipAddresses.stream()
        .filter(ipAddress -> !"5.6.7.8".startsWith(ipAddress)) // I doubt this should be '.filter("5.6.7.8"::startsWith)'
        .map(ipAddress -> "5.6.7.8")
        .collect(Collectors.toList());

A general approach as suggested in comments for solving this could be using:

List<String> internalIpAddresses = Stream.of("10.11.12.13") // can add more addresses
        .filter(ip -> ipAddresses.stream().anyMatch(ip::startsWith))
        .collect(Collectors.toList());
like image 166
Naman Avatar answered Sep 17 '22 16:09

Naman


You can try :

ipAddresseses.stream()
 .collect(Collectors.partitioningBy(ipAddress -> ipAddress.startsWith("10.11.12.13")));

This will give a Map with two elements, one with good ipAddress and one with bad.

like image 25
fastcodejava Avatar answered Sep 17 '22 16:09

fastcodejava