JAVA 8: Building a TriFunction functional interface

Java 8 facilitates several functional interface in java.util.function package. If we observe carefully, there are primarily four kinds of interfaces provided in this package, which are:

  • Supplier
  • Consumer
  • Function
  • Predicate

with the following signatures:

Supplier: () -> T
Consumer: T -> ()
Predicate: T -> boolean
Function: T -> R

In this post, alike java.util.function package, we define a new functional interface, called TriFrunction that accepts three arguments as parameters and returns the result after computation.

public interface TriFunction<T,U,S, R> {

* Applies this function to the given arguments.
* @param t the first function argument
* @param u the second function argument
* @param s the third function argument
* @return the function result
R apply(T t, U u, S s);

We can use this functional interface to compute the volume of a rectangular prism, by using following lambda expression.

TriFunction volume = (x,y,z) -> x*y*z

To use this lambda expression in the volume computation, we can simply use the following statement

volume.apply(2.4, 5.3, 10.4)

In this post, we have shown how to create a custom functional interface and use it to write concise lambda expressions using Java 8. It seems quite straightforward. If you have any question/remark, please leave a comment below.


Gist: JAVA 8 Stream API: Stream Initialization

Stream represents a sequence of values. Stream API, introduced in JAVA 8 (JSR-355 [1]) enables declarative specification of computation on data. It may also interesting to note that it imposes no restriction on the data source.

Following are the important facts:

  • Stream != Collection
    • It is bound to a data source which can be {collection, array, IO, iterator}
  • Stream does not hold any data
    • It is simply an intermediate data
  • Stream can’t modify its source
    • Good for parallelism.
  • Stream can be infinite.

In this post, we only discuss how to construct a Stream object.

A Stream object can be constructed in the following manner.

    Collection source = /*a collection that will be used as a source of Stream*/; 

Alternatively, Stream contains several builders, as listed below.

  • IntStream: Stream.of(1,3,4)
  • EmptyStream: Stream.empty()
  • InfiniteStream: Stream.iterate(1, n-&gt; n+10)

In the next post, we shall discuss more on how we can use a Stream to specify data transformation in a declarative manner.


[1] JSR-355: Lambda Expression for Java.

Maven: Updating Artefact Version in a Multi-Module Project

Using following Maven command, the version of artifacts can be updated in a multi-module maven project.

mvn versions:set -DnewVersion='new-version'

For instance, the following maven command updates the version in POM.xml files of the enclosed moduels to 7.1.0-DEV-SNAPSHOT.

mvn versions:set -DnewVersion='7.1.0-DEV-SNAPSHOT'

Note that, by default this command creates a backup of the existing POM files. In essence, it creates a pom.xml.versionBackup for each POM file. This behavior can be disabled as follows.

mvn versions:set -DnewVersion='7.1.0-DEV-SNAPSHOT' -DgenerateBackupPoms=false

Maven Commands for Building Specific Modules

In my job, we work with a maven multi-module project. While using maven, I have discovered several commands that are quite effective during development. In this post, and in the upcoming posts I am planning to share those.

So, today's post is regarding Maven command to build a specific project. For instance, we have a project, with three modules: A, B, and C, where C depends on B and B depends on A. And we are working with B at this moment. Using following command, we can run different maven phases on module B as follows.

mvn -pl :B clean install 

We can include multiple projects as follows :A, :B.

What is more interesting is that the following command run maven command not only this module, but also the modules that depends on it.

mvn -pl :B clean install -amd 

The above command in effect includes module C.

Using –am command, we can include all the dependencies as well, which is module A in this example.

Hope this helps!