Found 7442 Articles for Java

How to implement an instance method reference using a class name in Java?

raja
Updated on 13-Jul-2020 05:40:25

831 Views

Method reference is a simplified form of the lambda expression. It can specify a class name or instance name followed by the method name. The "::" symbol can separate a method name from the name of an object or class.An instance method reference refers to an instance method of any class. In the below example, we can implement an instance methods reference using the class name.Syntax::Exampleimport java.util.*;; import java.util.function.*; public class ClassNameRefInstanceMethodTest {    public static void main(String args[]) {       List empList = Arrays.asList(          new Employee("Raja", 15000),          new Employee("Adithya", 12000),          new Employee("Jai", 9000),   ... Read More

How can we write Callable as a lambda expression in Java?

raja
Updated on 13-Jul-2020 05:32:03

7K+ Views

A Callable interface defined in java.util.concurrent package. An object of Callable returns a computed result done by a thread in contrast to a Runnable interface that can only run the thread. The Callable object returns Future object that provides methods to monitor the progress of a task executed by a thread. An object of the Future used to check the status of a Callable interface and retrieves the result from Callable once the thread has done.In the below example, we can write a Callable interface as a Lambda Expression.Exampleimport java.util.concurrent.*; public class LambdaCallableTest {    public static void main(String args[]) throws InterruptedException {       ExecutorService executor = Executors.newSingleThreadExecutor();   ... Read More

What are the SAM Interfaces in Java?

raja
Updated on 13-Jul-2020 05:26:06

5K+ Views

An interface having only one abstract method is known as a functional interface and also named as Single Abstract Method Interfaces (SAM Interfaces). One abstract method means that either a default method or an abstract method whose implementation is available by default is allowed. The instances of SAM interfaces are java.lang.Runnable, java.awt.event.ActionListener,  java.util.Comparator and java.util.concurrent.Callable. The SAM interfaces can be implemented using lambda expressions or method references.Syntax@FunctionalInterface public interface Changeable {  public void change(T o); }Example@FunctionalInterface interface MyInterface {    String reverse(String n); } public class LambdaReverseTest {    public static void main( String[] args ) {       MyInterface myInterface = (str) -> { // ... Read More

How to implement reference to an instance method of a particular object in Java?

raja
Updated on 11-Jul-2020 12:53:39

652 Views

Method reference is a simplified form of a lambda expression that can execute one method. It can be described using "::" symbol. A reference to the instance method of a particular object refers to a non-static method that is bound to a receiver.SyntaxObjectReference::instanceMethodNameExample - 1import java.util.*; public class InstanceMethodReferenceTest1 {    public static void main(String[] args) {       String[] stringArray = { "India", "Australia", "England", "Newzealand", "SouthAfrica", "Bangladesh", "WestIndies", "Zimbabwe" };       Arrays.sort(stringArray, String::compareToIgnoreCase);       System.out.println(Arrays.toString(stringArray));    } }Output[Australia, Bangladesh, England, India, Newzealand, SouthAfrica, WestIndies, Zimbabwe]Example - 2@FunctionalInterface interface Operation {    public int average(int ... Read More

Differences between Method Reference and Constructor Reference in Java?

Aishwarya Naglot
Updated on 01-Sep-2025 13:30:13

3K+ Views

The Method Reference and Constructor Reference are part of Java 8's functional programming features, they used for refering to methods and constructors without executing them. They are often used in conjunction with functional interfaces, such as those defined in the java.util.function package. Method Reference A method reference is a shorthand representation of a lambda expression for calling a method. A method reference refers to a method without executing it. Method references can refer to static methods, instance methods, and constructors. Constructor Reference A constructor reference is a unique kind of method reference that is a reference to a constructor. ... Read More

What are the rules for formal parameters in a lambda expression in Java?

raja
Updated on 11-Jul-2020 12:50:17

358 Views

A lambda expression is similar to a method that has an argument, body, and return type. It can also be called an anonymous function (method without a name). We need to follow some rules while using formal parameters in a lambda expression.If the abstract method of functional interface is a zero-argument method, then the left-hand side of the arrow (->) must use empty parentheses.If the abstract method of functional interface is a one-argument method, then the parentheses are not mandatory.If the abstract method of functional interface is a multiple argument method, then the parentheses are mandatory. The formal parameters are comma-separated and can be in the same order of the ... Read More

What are the rules for the body of lambda expression in Java?

raja
Updated on 11-Jul-2020 12:48:49

1K+ Views

A lambda expression is an anonymous function (nameless function) that has passed as an argument to another function. We need to follow some rules while using the body of a lambda expression.Rules for the body of a lambda expressionThe body of the lambda expression can be either a single expression or more statements.If we are using a single expression as the body of a lambda expression, then no need to enclose the body with curly braces ({}).If we are using one or more statements as the body of a lambda expression, then enclosing them within curly braces({}) can be mandatory.Syntax(parameters) OR () -> {body with statements separated by;} OR Single StatementExampleinterface Message {   ... Read More

How to sort a collection by using Stream API with lambdas in Java?

raja
Updated on 11-Jul-2020 12:43:12

528 Views

A Stream API is a powerful way to achieve functional programming in Java. It usually works in conjunction with a lambda expression and provides an efficient way to perform data manipulation operations like sort, filter, map, reduce and etc.In the below example, we can sort a collection using Stream API. It provides sorting logic by using the sorted() method of the Comparator interface. If we have two Comparator interface instances and need to do sorting by composite condition (by the first comparator and then by the second comparator), we can use both comparators by invoking the thenComparing() method on the first instance and passing in the second instance.Exampleimport java.util.*; import java.util.stream.*; ... Read More

Why we use lambda expressions in Java?

raja
Updated on 11-Jul-2020 12:43:42

4K+ Views

A lambda expression can implement a functional interface by defining an anonymous function that can be passed as an argument to some method.Enables functional programming: All new JVM based languages take advantage of the functional paradigm in their applications, but programmers forced to work with Object-Oriented Programming (OOPS) till lambda expressions came. Hence lambda expressions enable us to write functional code.Readable and concise code: People have started using lambda expressions and reported that it can help to remove a huge number of lines from their code.Easy-to-Use APIs and Libraries: An API designed using lambda expressions can be easier to use and support other API.Enables support for ... Read More

Importance of Predicate interface in lambda expression in Java?

raja
Updated on 11-Jul-2020 12:38:14

2K+ Views

Predicate is a generic functional interface that represents a single argument function that returns a boolean value (true or false). This interface available in java.util.function package and contains a test(T t) method that evaluates the predicate of a given argument.Syntaxpublic interface Predicate {  boolean test(T t); }Exampleimport java.util.*; import java.util.functionPredicate; public class LambdaPredicateTest {    public static void main(String args[]) {       Employee emp1 = new Employee("Raja", 26);       Employee emp2 = new Employee("Jaidev", 24);       Employee emp3 = new Employee("Adithya", 30);       List empList = new ArrayList();       empList.add(emp1);       empList.add(emp2); ... Read More

Advertisements