Home » Tag Archives: Lambdas

Tag Archives: Lambdas

Decorator Design Pattern using lambdas

java-logo

With the advent of lambdas in Java we now have a new tool to better design our code. Of course the first step is using streams, method references and other neat features introduced in Java 8. Going forward I think the next step is to revisit the well established Design Patterns and see them through the functional programming lenses. For ...

Read More »

Using Java 8 Lambdas, Streams, and Aggregates

java-logo

Overview In this post, we’ll take a look at filtering and manipulating objects in a Collection using Java 8 lambdas, streams, and aggregates. All code in this post is available in BitBucket here. For this example we’ll create a number of objects that represent servers in our IT infrastructure. We’ll add these objects to a List and then we’ll use ...

Read More »

Java 8 Lambda Expression for Design Patterns – Decorator Design Pattern

java-logo

The Decorator pattern (also known as Wrapper) allows behavior to be added to an individual object, either statically or dynamically, without affecting the behavior of other objects from the same class. It can be considered as an alternative to subclassing. We know that subclassing adds behavior at compile time and the change affects all instances of the original class. On ...

Read More »

The Danger of Subtype Polymorphism Applied to Tuples

java-logo

Java 8 has lambdas and streams, but no tuples, which is a shame. This is why we have implemented tuples in jOOλ – Java 8’s missing parts. Tuples are really boring value type containers. Essentially, they’re just an enumeration of types like these: public class Tuple2<T1, T2> { public final T1 v1; public final T2 v2; public Tuple2(T1 v1, T2 ...

Read More »

How and Why to Serialize Lambdas

java-logo

Overview Serializing lambdas can be useful in a number of use cases such as persisting configuration, or as a visitor pattern to remote resources. Remote Visitors For example, so I want to access a resource on a remote Map, I can use get/put, but say I just want to return a field from the value of a Map, I can ...

Read More »

Exceptions in Lambdas: An Elegant Solution to a Bit of a Mess

java-logo

Consider the following function for writing to a file: The idea behind the method is to allow the user to pass in different implementations of InputStream to the method so that writeToFile can be called for example with a GZIPOuputStream, SnappyOuputStream (fast compression) or simply a plain FileInputStream. private static void writeToFile(File file, String value, Function<OutputStream, OutputStream> writing) throws IOException{ ...

Read More »

Cheating with Exceptions – Java 8 Lambdas

java-logo

Cheating with Exceptions – Java 8 Lambdas Leaving aside the religious debate about Checked vs Runtime exceptions, there are times where due to poorly constructed libraries, dealing with checked examples can drive you insane. Consider this snippet of code which you might want to write: public void createTempFileForKey(String key) { Map<String, File> tempFiles = new ConcurrentHashMap<>(); //does not compile because ...

Read More »

Java 8 Lambdas in One Line

java-logo

If you understand this line, or better still can write this code you can pretty much say that you have understood the essence of Java 8 Lambdas. Certainly in as much as they can be used with collections. I found this in a recent presentation by Peter Lawrey.  (Definitely worth watching the whole presentation when you have a spare hour.) Anyway ...

Read More »

Want to take your Java skills to the next level?

Grab our programming books for FREE!

Here are some of the eBooks you will get:

  • Advanced Java Guide
  • Java Design Patterns
  • JMeter Tutorial
  • Java 8 Features Tutorial
  • JUnit Tutorial
  • JSF Programming Cookbook
  • Java Concurrency Essentials