Found 4348 Articles for Java 8

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

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

467 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

191 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

2K+ 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

566 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

499 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

How to use IntStream in lambdas and method references in Java?

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

5K+ Views

An IntStream interface extends the BaseStream interface in Java 8. It is a sequence of primitive int-value elements and a specialized stream for manipulating int values. We can also use the IntStream interface to iterate the elements of a collection in lambda expressions and method references.Syntaxpublic interface IntStream extends BaseStreamExampleimport java.util.stream.IntStream; public class StringToIntegerStreamTest {    public static void main(String[] args) {       String str = "Tutorials Point";       IntStream stream = str.chars();       stream.forEach(element -> System.out.println(((char)element))); // using lambda expression    } }OutputT u t o r i a l s P o i n tExampleimport java.util.*; ... Read More

How to use BooleanSupplier in lambda expression in Java?

raja
Updated on 13-Jul-2020 08:34:39

2K+ Views

BooleanSupplier is a functional interface defined in the "java.util.function" package. This interface can be used as an assignment target for a lambda expression or method reference. BooleanSupplier interface has only one method getAsBoolean() and returns a boolean result, true or false.Syntax@FunctionalInterface public interface BooleanSupplier {    boolean getBoolean(); }Exampleimport java.util.function.BooleanSupplier; public class BooleanSupplierLambdaTest {    public static void main(String[] args) {       BooleanSupplier Obj1 = () -> true;       BooleanSupplier Obj2 = () -> 5 < 50; // lambda expression       BooleanSupplier Obj3 = () -> "tutorialspoint.com".equals("tutorix.com");       System.out.println("Result of Obj1: " + Obj1.getAsBoolean());       ... Read More

How to use FileFilter interface in lambda expression in Java?

raja
Updated on 13-Jul-2020 08:30:13

517 Views

A FileFilter is a functional interface from the "java.io" package. It can be used as the assignment target for a lambda expression or method reference. An instance of the FileFilter interface passed to the listFiles() method of the File class. FileFilter interface having one abstract method accept() and it tests whether or not the specified abstract pathname has included in a pathname list.Syntax@FunctionalInterface public interface FileFilterExampleimport java.io.File; import java.io.FileFilter; public class FileFilterTest {    public static void main(String[] args) {       File dir = new File("C:/Program Files/Java/jdk1.8.0_211");       File[] subDir = dir.listFiles((file) -> {    // lambda expression         ... Read More

How to implement PropertyChangeListener using lambda expression in Java?

raja
Updated on 13-Jul-2020 08:13:49

3K+ Views

A PropertyChangeListener is a functional interface from java.beans package. It has one abstract method propertyChange() and gets called when a bound property is changed. This method takes a PropertyChangeEvent argument that has details about an event source and the property that has changed. A PropertyChangeSupport can be used by beans that support bound properties. It can manage a list of listeners and dispatches property change events. The PropertyChangeListener's implementer does the role of an Observer and bean wrapping the PropertyChangeSupport is Observable.Syntaxvoid propertyChange(PropertyChangeEvent evt)Exampleimport java.beans.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; public class PropertyChangeListenerLambdaTest {    public static void main(String [] args) {   ... Read More

Previous 1 ... 6 7 8 9 10 ... 435 Next
Advertisements