Fizz-Buzz is a some kind of test used in interviews, which I have heard. But, I was never asked this question. In Fizz-Buzz test, what you have to do is to write a code, to print Fizz, Buzz, FizzBuzz or a number. To go in detail, you have to loop over some number or test […]
Sum Of Integer Array : How To Do Functional Programming In Java
We can use functional Programming in Java to calculate sum of an int array. You can watch my live coding video to see how it is done. Traditional Way :- int sum = 0; for(int intValue : ints) sum += intValue; Functional Programming Way 1 :- Arrays.stream(ints).sum(); Functional Programming Way 2 :- Arrays.stream(ints).reduce(0, Integer::sum); Functional […]
Currying : How To Do functional Programming In Java
One of the key identifiers of functional programming is Currying. Since Java 8, the new features were added, allows us to do this operation. What is currying? currying is a name assigned to ‘half initialized functions’. A Curried function has some of the parameters fixed while other parameters will be passed by the users. We […]
How to use java.util.function.Consumer functional interface in java – And different implementations using class, lambdas and method reference
Consumer is present in java.util.functions package. It is a Functional Interface . In this article, we will use different ways to provide implementations for this interface. The Consumer implementation will take a String-input and write it to console. The System.out.println method can be replaced with other writers, like it can modified to write the contents to a file or […]
Simple implementation for Callable interface in Java, using lambda.
In this tutorial, we will learn basic implementations for Callable interface using lambda in java. Anonymous Class providing an implementation for Callable interface. Callable<String> callMyName = new Callable<String>() { @Override public String call() throws Exception { return “Anurag Anand”; } }; You can submit the above implementation to return any string value. The same can be […]
Using java lambda to create threads using runnable interface in java
I am using jdk-11 for this tutorial. We are going to use lambda functions to create a thread in java. In this tutorial, we will implement Runnable interface. Our lambda implementation will print numbers from the range 1 to 10. Step 1 :- implement Runnable interface Runnable runThroughLambda = () -> IntStream.rangeClosed(1, 10) .forEach(System.out::println); In above code, I […]
Using lambda and Functional Programming to provide implementation for Callable interface – which prints a string n number of times in java
Callable is a Functional interface, it is similar in usage to Runnable interface. But with a small difference, that Callable can return a value, while Runnable can not return a value. In this tutorial, we will create a Callable which will return a String output. The string output is going to return String Value, which will be contain a given word repeated n number of times. Given Values :- […]
Create a simple counter in java 11, using java IntSupplier – Then test it using IntStream – a functional programming way.
We will create a simple counter in java. It will have a default initialization of 0. It will have a constructor, which can be used to set the seed value of this counter. It will implement Iterator interface It will implement IntSupplier interface package com.refactored.util; import java.util.Iterator; import java.util.function.IntSupplier; public class Counter implements Iterator<Integer>, IntSupplier […]
Using java streams to make your java code lazy
What do I mean by making your code lazy? Your code won’t do a single operation, until, output generated by it’s execution is not consumed by some other operation. It would look like the code is doing some action, but in reality it is not doing anything. Let’s dig into this by using an example […]
Using java streams to create immutable class
In my post – How to create immutable classes – to make lists immutable, I created a new List-Object everytime. This is the class which, we are going to modify in this tutorial. package immutable; import java.util.ArrayList; import java.util.Date; import java.util.List; final public class Person { private int age; private String name; private Date dateOfBirth; private […]