Found 7442 Articles for Java

How to generate prime numbers using lambda expression in Java?

raja
Updated on 13-Jul-2020 11:32:07

2K+ Views

A prime number is a number that is greater than 1 and divided by 1 or itself only. It other words, it can't be divided by other numbers than itself or 1. The generation of prime numbers is 2, 3, 5, 7, 11, 13, 17 and etc.In the below example, we can generate the prime numbers with the help of Stream API and lambda expressions.Exampleimport java.util.*; import java.util.stream.*; public class PrimeNumberLambdaTest {    public static void main(String[] args) {       List generate = PrimeNumberLambdaTest.generate(10);       System.out.println(generate);    }    public static List generate(int series) {       Set set = new TreeSet(); ... Read More

How to implement the Fibonacci series using lambda expression in Java?

raja
Updated on 13-Jul-2020 09:24:29

3K+ Views

The Fibonacci is a sequence of numbers in which every number after the first two numbers is the sum of the two preceding numbers like 0, 1, 1, 2, 3, 5, 8, 13, 21 and so on. The sequence of Fibonacci numbers defined by using "F(n)=F(n-1)+F(n-2)".In the below example, we can implement the Fibonacci series with the help of Stream API and lambda expression. The Stream.iterate() method returns an infinite sequential ordered stream produced by iterative application of a function to an initial element seed, producing a stream consisting of seed, f(seed), f(f(seed)), etc.Exampleimport java.util.List; import java.util.stream.*; public class FibonacciTest {    public static void ... Read More

How to implement IntFunction with lambda expression in Java?

raja
Updated on 13-Jul-2020 09:25:13

471 Views

IntFunction interface is a functional interface in Java 8 and defined in java.util.function package. This interface accepts an int-valued parameter as input and returns a value of type R. IntFunction interface can be used as an assignment target for a lambda expression or method reference and holds only one abstract method, apply().Syntax@FunctionalInterface public interface IntFunction {    R apply(int value); }Exampleimport java.util.HashMap; import java.util.Map; import java.util.function.IntFunction; public class IntFunctionTest {    public static void main(String[] args) {       IntFunction getMonthName = monthNo -> {  // lambda expression          Map months = new HashMap();          months.put(1, "January");       ... Read More

How to sort a list using Comparator with method reference in Java 8?

raja
Updated on 13-Jul-2020 09:23:06

2K+ Views

Java 8 introduced changes in the Comparator interface that allows us to compare two objects. These changes help us to create comparators more easily. The first important method added is the comparing() method. This method receives as a parameter Function that determines the value to be compared and creates Comparator. Another important method is the thenComparing() method. This method can be used to compose Comparator.In the below example, we can sort a list by using the first name with comparing() method and then the last name with the thenComparing() method of Comparator interface.Exampleimport java.util.*; public class MethodReferenceSortTest {    public static void main(String[] ... Read More

How to implement ToIntFunction using lambda and method reference in Java?

raja
Updated on 13-Jul-2020 09:07:52

676 Views

ToIntFunction is the built-in functional interface defined in java.util.function package. This functional interface expects an argument as input and produces an int-valued result. It can be used as an assignment target for a lambda expression or method reference. The ToIntFunction interface holds only one method, applyAsInt(). This method performs an operation on the given argument and returns the int-valued result.Syntax@FunctionalInterface public interface ToIntFunction {    int applyAsInt(T value); }In the below example, we can implement ToIntFunction by using lambda expression and method reference.Exampleimport java.util.function.ToIntFunction; import java.time.LocalDate; public class ToIntFunctionInterfaceTest {    public static void main(String[] args) {       ToIntFunction lambdaObj = value -> ... Read More

How to implement the ObjIntConsumer interface using lambda expression in Java?

raja
Updated on 13-Jul-2020 09:09:29

356 Views

The ObjIntConsumer interface is a kind of functional interface and defined in java.util.function package. This functional interface expects an object-valued and int-valued argument as input and does not produce any output. It holds only one functional method, accept(Object, int).Syntax@FunctionalInterface    public interface ObjIntConsumer {       void accept(T t, int value) }In the below examples, we can implement the ObjIntConsumer interface by using a lambda expression.Example-1import java.util.function.*; public class ObjIntConsumerInterfaceTest1 {    public static void main(String args[]) {       ObjIntConsumer objIntConsumberObj = (t, value) -> {   // lambda expression          if(t.length() > value) {         ... Read More

How to populate a Map using a lambda expression in Java?

raja
Updated on 13-Jul-2020 09:11:10

2K+ Views

A Map is a collection object that maps keys to values in Java. The data can be stored in key/value pairs and each key is unique. These key/value pairs are also called map entries.In the below example, we can populate a Map using a lambda expression. We have passed Character and Runnable arguments to Map object and pass a lambda expression as the second argument in the put() method of Map class. We need to pass command-line arguments whether the user enters 'h' for Help and 'q' for quit with the help of Scanner class.Exampleimport java.util.*; public class PopulateUsingMapLambdaTest {    public static void main(String[] args) {       Map map = new ... Read More

How to reverse a string using lambda expression in Java?

raja
Updated on 13-Jul-2020 09:03:02

3K+ Views

A String is an object that represents a sequence of characters and immutable in Java. We can reverse a string entered by the user using the charAt() method of String class to extract characters from the string and append them in reverse order to reverse the entered string.In the below example, we need to reverse a string using lambda expression with the help of the Scanner class.Exampleimport java.util.Scanner; interface StringFunc {    String func(String n); } public class StringFuncLambdaTest {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       StringFunc reverse = (str) -> {   // ... Read More

How to serialize a lambda function in Java?

raja
Updated on 13-Jul-2020 08:45:42

810 Views

The Serialization is a process for writing the state of an object into a byte stream so that we can transfer it over the network. We can serialize a lambda expression if its target type and its captured arguments have serialized. However, like inner classes, the serialization of lambda expressions is strongly discouraged.In the below example, we can serialize and deserialize a lambda function using a Function interface.Exampleimport java.io.*; import java.util.function.Function; interface MyInterface {    void hello(String name); } class MyImpl implements MyInterface {    public void hello(String name) {       System.out.println("Hello " + name);    } } public class SerializeDeSerializeLambdaTest {    public static void main(String[] args) ... Read More

How to use IntSupplier in lambda expression in Java?

raja
Updated on 13-Jul-2020 08:41:18

975 Views

An IntSupplier is a functional interface defined in "java.util.function" package. This interface represents an operation that takes without arguments and returns the result of int type. IntSupplier interface has only one method, getAsInt() and returns a result. This functional interface can be used as an assignment target for lambda expressions or method references.Syntax@FunctionalInterface public interface IntSupplier {    int getAsInt(); }Exampleimport java.util.function.IntSupplier; public class IntSupplierTest {    public static void main(String[] args) {       IntSupplier intSupplier1 = () -> Integer.MAX_VALUE;  // lamba expression       System.out.println("The maximum value of an Integer is: " + intSupplier1.getAsInt());       IntSupplier intSupplier2 = () -> ... Read More

Advertisements