Pular para o conteúdo principal

4 Real World Lambdas with Collections examples

I love functional programming and I'm exciting not only about Lambda, but also about the stream API  and the new functional interfaces that comes with Java 8, which was released recently. In the next lines I'll show some codes I took from some Java projects in github and rewrote it using Java 8. Notice obviously I had to mock some classes just to focus on the code rewrite.

If you find a better way to do this, please let me know!

Code on Github



1) Concatenating a List into a String


Original


public static String matrixparamtest(PathSegment id){
         StringBuffer sb = new StringBuffer();
         sb.append("matrix=");
         sb.append("/" + id.getPath());
         Map matrix = id.getMatrixParameters();
         Set keys = matrix.keySet();
         for (Object key : keys)
         {   
            sb.append(";" + key.toString() + "=" +
                    matrix.get(key.toString()));

         }   
         return sb.toString();
} 

My Java 8 Equivalent:


public static String matrixparamtestlambda(PathSegment id){
        Map matrix = id.getMatrixParameters();
        String keys = matrix.keySet().stream()
                    .map(k -> k + "=" + matrix.get(k))
                    .collect(Collectors.joining(";", ";", ""));
        return "matrix=/" + id.getPath() + keys;

}

What's going on here?

Take the Stream from the keys: matrix.keySet().stream()
Map to a Stream with format "param=val":  map(k -> k + "=" + matrix.get(k)
Join the stream into a big string starting with ";" and separated by ";": collect(Collectors.joining(";", ";", ""))


2) Mapping not null elements from a List

Original

public static Map mapElements(){
        final Map map = new HashMap();
        for (final Element element : values()) {
                final String name = element.getLocalName();
                if (name != null)
                        map.put(name, element);
        }   
        return map;  
}    

My Java 8 Equivalent:

public static Map mapElementsLambda(){
        return values().stream()
                .filter(e -> e.getLocalName() != null)
                .collect(Collectors.toMap(Element::getLocalName, e -> e));
}

What's going on here?

Taking the stream object from values: values().stream()
Filtering them to avoid null values: filter(e -> e.getLocalName() != null)
Collecting a map using as the key the localName attribute and as value the element itself: collect(Collectors.toMap(Element::getLocalName, e -> e));

3) Transforming a Map

Original

public static Map filterLoadedValues(Map in) {   
        Map out = new HashMap<>();
        for (Iterator iterator = in.entrySet().iterator(); iterator.hasNext();){   
                Map.Entry entry = (Map.Entry) iterator.next();
                out.put(entry.getKey().toString(), Float.valueOf(entry.getValue().toString()));
        }   
        return out;
}

My Java 8 Equivalent:

public static Map filterLoadedValuesLambda(Map in) {   
        return in.entrySet().stream().collect(
                        Collectors.toMap(
                                e -> e.getKey().toString(),
                                e -> Float.valueOf(e.getValue().toString())    
                )); 
} 

What's going on here?


  • We are taking a Stream of the entries of the map: in.entrySet().stream()
  • Collecting the entries: collect
  • Generating a new one a transforming the value from String to Float: Collectors.toMap( e -> e.getKey().toString(),e -> Float.valueOf(e.getValue().toString()) ;

4) Summing

Original

public static float getTotalPrice() {
        float totalPrice = 0;
        if (lineItems != null) {
                for (LineItem item : lineItems) {
                        if (item.getPrice() != null)
                                totalPrice += item.getPrice();
                }   
        }   
        return totalPrice;
}  

My Java 8 Equivalent:

public static double getTotalPriceLambda() {
        return lineItems.stream().mapToDouble(LineItem::getPrice).filter(Objects::nonNull).sum();
} 

What's going on here?

  • Map the stream to a DoubleStream: mapToDouble(LineItem::getPrice)
  • Filter the Stream and remove possible null values: filter(Object::nonNull)
  • Sum all the elements: sum();

Conclusion

Java 8 brings lots of advantages to programming when dealing with lists. As you can see, I took code from "real world" and not sample applications to demonstrate Lambda. It shows that Lambda brings productivity to our daily code.


Comentários

Postagens mais visitadas deste blog

Creating custom Work Item Handler in BPM Suite/jBPM 6

Hi all! In this post I am going to share my experience creating a work item handler for BPM Suite 6.0.3 (which is very similar to jbpm as explained in a previous post ). The Hello World Work Item Handler Let's create a really simple WorkItemHandler just to print "Hello World" in the console. Of course you can implement more feature and make it receive parameters, access database, web services, etc. But in this post we will keep it simple. Really simple. First thing to do is to create a maven project for my WorkItemHandler. Here's the pom.xml of my Maven project: Notice that we are importing jbpm dependency so we can find the java interface that is used to create work item handlers. Now we can start coding. Let's create a class name HelloWorkItemHandler in package org.jugvale.jbpm, here is the simple code: Now we can build our project and go to BPM Suite to configure it so we can use this workitem. Registering the WorkItemHandler UPDATE...

Using the REST Task from jBPM 6

In this post I am going to share my experience using the BPM Suite 6 REST Task. BPM Suite  X jBPM jBPM is a JBoss community project to offer an open source and efficient BPM Suite and BPM Suite is a product created by Red Hat based on jBPM. In this post I will be using BPM Suite because I have it installed already, but what I am going to described should work for jBPM as well.   REST Task  The REST Task is used to invoke a REST resource (or perform an HTTP Request) from your Business Process. You can set the URL, HTTP Method, timeout and credentials from the Process Modeller interface and when the process gets into that task, it will generate a HTTP Request and return the response as String. It is available on the left side pallet under the "Service Tasks" tab: The Target REST Web Service The service we are going to access from our process is a really simple one, that's why it is called simple-hello-rs and it is based on JBoss helloworld-rs qu...

Simplest JavaFX ComboBox autocomplete

Based on this Brazilian community post , I've created a sample Combobox auto complete. What it basically does is: When user type with the combobox selected, it will work on a temporary string to store the typed text; Each key typed leads to the combobox to be showed and updated If backspace is type, we update the filter Each key typed shows the combo box items, when the combobox is hidden, the filter is cleaned and the tooltip is hidden:   The class code and a sample application is below. I also added the source to my personal github , sent me PR to improve it and there are a lot of things to improve, like space and accents support.