What do genealogyorganizational structuresDNS and federations have in common? At first glance, they seem to be extremely disconnected. However, in all cases there is the application of a very old concept in computing: a hierarchical structure of data. In general, in all of these scenarios mentioned above, the basic data units have a unique identifier, their own value and a reference to the identifier of their respective hierarchical predecessor (which we will now call “parent element”). From this chain of data it is possible to clearly identify the definition of a tree.

Image for post

Hierarchical Data

Among the similarities between these scenarios, we can also point out the problems or questions that are frequently asked. For example: Just as we want to know in a family tree who all of Linda’s descendants are, in an organizational structure we may also want to know what positions are under the responsibility of the CIO. In the same way that we want to know what is the first level in the Google DNS, we can also ask what is the highest among the federal entities in France.

The computational methods for answering these questions are quite similar and often the easiest solution is to implement recursive algorithms. We can say that there is nothing new in the manipulation of trees with recursive algorithms. However, in March 2014, version 8 of the Java language was released. This version is seen by many as one of the greatest revolutions in language since its birth. Among the most important changes, we highlight a functional programming approach, the introduction of lambda expressions and the Java Streams API.

Classes in the new _java.util.stream_ package provide a Stream API to support functional-style operations on streams of elements. The Stream API is integrated into the Collections API, which enables bulk operations on collections, such as sequential or parallel map-reduce transformations. (What’s New in JDK 8_ — Oracle)_

The purpose of this article is to present a set of possible operations on hierarchical data using recursion, functional programming techniques and the Java Streams API.

Dataset

In our example, we will use a generic interface to represent hierarchical data. As previously mentioned, what all hierarchical data has in common is basically its own identifier and a reference to the identifier of its parent item — regardless of the type of that identifier for us:

public interface IElement<R> {

    R getId();

    R getParentId();

    default boolean checkId(R id) {
        return this.getId().equals(id);
    }
}

To further enrich our interface with Java 8 concepts, we have also defined a default method that checks the equality among any object and the IElement interface identifier. The usefulness of this check will be presented a little later.

#recursion #java8 #trees #hierarchical-data #stream #data analysis

An Overview of Hierarchical Data, Recursion and Java Streams
1.55 GEEK