Java 8 Hands-on


Hi, I am Malathi Boggavarapu working at Volvo Group and i live in Gothenburg, Sweden. I have been working on Java since several years and had vast experience and knowledge across various technologies.

This post is all about different usecases which use Java 8 features which could be helpful for anyone.Please post your comments below or if you need to address any legacy code using Java 8 features.

Java 8 practical examples


Scenario 1

The following code excerpt explains the following scenario and also demonstrates the usage of Java 8 Stream, Filter and map.


- Split the IP Address with respect to a delimeter (,) and store it into a Collection. 
- Filter out proxy addresses from the resulting Collection.
- Join the result with delimeter (,).

public static String filteredIpAddresses(){

 String proxyAddresses = "10.12.12.13, 10.12.12.14";
 String ipAdress = "46.150.151.10, 46.150.151.11";

 Set<String> ipAdresses =                 Arrays.stream(ipAdress.split(",")).map(String::trim).collect(Collectors.toSet())

 Set<String> proxies = Arrays.stream(proxyAddresses.split(",")).map(String::trim).collect(Collectors.toSet())

List<String> filteredIpAddresses
ipAdresses.stream().filter(ipAdr -> !proxies.contains(ipAdr)).collect(Collectors.toList());

return String.join(",", filteredIpAddresses);
}

Scenario 2

If we want to split the String and store the values into the HashMap, below code excerpt demonstrates the usage of stream and map.

String exampleString = 1:Service1;2:Service2;3:Service3
Arrays.stream(exampleString.split(";")).
map(s -> s.split(":")).collect(Collectors.toMap(e -> e[0], e ->e[1]))


Scenario 3

If we want to filter internal proxy addresses from real client IP address using stream, Map and Filter and Predicate, the following example would be useful


public static String filteredIpAddresses(){

 String proxyAddresses = "10.12.12.13, 10.12.12.14";
 String ipAdress = "46.150.151.10, 46.150.151.11";

 Set<String> ipAdresses =                 Arrays.stream(ipAdress.split(",")).map(String::trim).collect(Collectors.toSet())

 Set<String> proxies = Arrays.stream(proxyAddresses.split(",")).map(String::trim).collect(Collectors.toSet())

List<String> filteredIpAddresses
ipAdresses.stream().filter(FilterProxyPredicate.internalProxyAddress(proxies)).collect(Collectors.toList());

return String.join(",", filteredIpAddresses);
}

The following Predicate is passed to the Filter inorder to filter out internal proxy addresses.

public class FilterProxyPredicate(){
    public static Predicate<String> internalProxyAddress(List<String> proxies){
        return proxy -> {
             for(String proxyAddress : proxies){
                   SubnetUtils subnetUtil = new SubnetUtils(proxyAddress);
                   if(subnetUtil .isInRange(proxy))
                        return false;
              }
              return true;
       };
    }     
}

Scenario 4

This is generic to Java and is not particularly related to Java 8. How to convert a single object to Set, Map or List using Collections? See below code excerpt.

Product product = new Product();
product.setName("Name 1");
product.setId(123L);

Set<Product> productSet = Collections.singleton(product);

Similarly for List and Map we have following.
Set<Product> productList = Collections.singletonList(product);
Map<Long, String> productMap = Collections.singletonMap(product.getId(), product.getName());



-- to be continued

Comments

Popular posts from this blog

Bash - Execute Pl/Sql script from Shell script

How to get client Ip Address using Java HttpServletRequest

How to install Portable JDK in Windows without Admin rights