Functional programming in Java

Why Functional Programming and Why to Bother

Before going into the details of functional programming, the first concept which needs to be discussed is why functional programming ? Traditionally, JAVA was used solely for object oriented programming which included dealing with creation and relation between objects, inheritance, polymorphism etc. But now-a-days functional programming gives us one more tool to work with. The […]

Continue Reading...

Functional Data Structures

What are functional data structures

What are the functional data structures? A functional data structure is something that is not imperative and yes, implemented in a functional way. what do I mean by that is, Functional data structures do not use any imperative features like there will be no updates and modifications, and no such operations on a data structure […]

Continue Reading...

Java Streams

Streams and Lazy

The most important property of Streams is Laziness. Streams are lazy, as the Intermediate Operations in Stream pipeline, not get evaluated until the terminal operation is invoked. Or we can say all intermediate operations on streams are lazy. In what sense? Consider the following example. Explanation: While creating the stream of filtered elements, we are […]

Continue Reading...

Java Streams

Primitive Streams

To provide support to work with primitives int, long and double, Java provides three primitive specialized implementations of Stream. IntStream LongStream DoubleStream This is how we can create primitive streams using these interfaces. Creating Primitive Stream: Using of() method | Arrays.stream() method We use of() method with primitive streams to get the stream of corresponding […]

Continue Reading...

Java Streams

Infinite Streams

With finite or Bounded streams we supply elements up to ceratin stream length. Whereas, in infinite streams, we don’t have any stream length. Ways of creating infinite streams: Stream<T> iterate(T seed, UnaryOperator<T> f): iterate() method of Stream returns an infinite sequential ordered stream. It takes a seed and UnaryOperator. Seed is the first element of […]

Continue Reading...

Java Streams

Finite Streams | Bounded Streams

Ways of creating finite streams: collection.stream() call on a Collection to get a stream of all the elements contained in that collection. This gives a Stream of elements contained in the list. We can use this stream() method with every collection, but there is no direct way to create a stream from a map because […]

Continue Reading...

Java Streams

Parallel Streams

Parallelism is dividing a problem into sub-problems and executing them on multiple resources. For example, Computers having a Multi-Core processor are faster. Similarly, our programs can be designed in a way so that they can take advantage of multi-core processing. Streams API allows us to do that. We can create Parallel Streams to take advantage […]

Continue Reading...

Java Streams

Stream flatMap()

The Stream flatMap() is used to flatten the stream of streams to stream of individual elements. What is Flattening? Flattening is like converting Collection of collection to a single collection of all those elements present in all collections. Consider two streams, that we want to merge into one. But we can not merge their elements […]

Continue Reading...

Java Streams

Methods in Stream API | filter, map, reduce

Functional programming languages typically provide several useful higher-order functions. (a function that takes one or more functions or returns a function or does both is a higher-order function). The most common Higher-order functions are Filter, Map and Reduce. filter method As the name suggests, the filter method filters the elements based upon the condition we […]

Continue Reading...