Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Articles - Page 199 of 745
863 Views
A cast expression provides a mechanism to explicitly provide a lambda expression's type if none can be conveniently inferred from context. It is also useful to resolve ambiguity when a method declaration is overloaded with unrelated functional interface types.SyntaxObject o = () -> { System.out.println("TutorialsPoint"); }; // Illegal: Object o = (Runnable) () -> { System.out.println("TutorialsPoint"); }; // LegalExampleinterface Algebra1 { int operate(int a, int b); } interface Algebra2 { int operate(int a, int b); } public class LambdaCastingTest { public static void main(String[] args) { printResult((Algebra1)(a, b) -> a + b); // Cast Expression ... Read More
2K+ Views
An array is a fixed size element of the same type. The lambda expressions can also be used to initialize arrays in Java. But generic array initializes cannot be used.Example-1interface Algebra { int operate(int a, int b); } public class LambdaWithArray1 { public static void main(String[] args) { // Initialization of an array in Lambda Expression Algebra alg[] = new Algebra[] { (a, b) -> a+b, ... Read More
12K+ Views
A return statement is not an expression in a lambda expression. We must enclose statements in braces ({}). However, we do not have to enclose a void method invocation in braces. The return type of a method in which lambda expression used in a return statement must be a functional interface.Example 1public class LambdaReturnTest1 { interface Addition { int add(int a, int b); } public static Addition getAddition() { return (a, b) -> a + b; // lambda expression return statement } public static void main(String args[]) { System.out.println("The ... Read More
669 Views
In Java 8, lambda expression accepts an anonymous function as a parameter. In the case of providing an anonymous method, we can also pass references of existing methods using "::" symbol. A method reference enables us to do the same thing but with existing methods.We can also implement an action listener for a JButton by using static method reference and referred using the class name.Syntax:: Method-name;Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class MethodReferenceButtonListenerTest extends JFrame { private JButton button; public MethodReferenceButtonListenerTest() { setTitle("Method Reference Button Listener"); button = new JButton("Method Reference"); ... Read More
8K+ Views
Lambda expression is an anonymous method (method without a name) that has used to provide the inline implementation of a method defined by the functional interface while a method reference is similar to a lambda expression that refers a method without executing it. The arrow (->) operator can be used to connect the argument and functionality in a lambda expression while the (::) operator separates the method name from the name of an object or class in a method reference.Syntax for Lambda Expression([comma seperated argument-list]) -> {body}Syntax for Method Reference :: Exampleimport java.util.*; public class LambdaMethodReferenceTest { public static void main(String args[]) { ... Read More
1K+ Views
A method reference is a new feature in Java 8, which is related to Lambda Expression. It can allow us to reference constructors or methods without executing them. The method references and lambda expressions are similar in that they both require a target type that consists of a compatible functional interface.SyntaxClass :: instanceMethodNameExampleimport java.util.*; import java.util.function.*; public class ArbitraryObjectMethodRefTest { public static void main(String[] args) { List persons = new ArrayList(); persons.add(new Person("Raja", 30)); persons.add(new Person("Jai", 25)); persons.add(new Person("Adithya", 20)); persons.add(new Person("Surya", 35)); persons.add(new ... Read More
11K+ Views
The lambda expressions are inline code that implements a functional interface without creating an anonymous class. In Java 8, forEach statement can be used along with lambda expression that reduces the looping through a Map to a single statement and also iterates over the elements of a list. The forEach() method defined in an Iterable interface and accepts lambda expression as a parameter.Example ( List using Lambda Expression)import java.util.*; public class ListIterateLambdaTest { public static void main(String[] argv) { List countryNames = new ArrayList(); countryNames.add("India"); countryNames.add("England"); countryNames.add("Australia"); ... Read More
9K+ Views
In Java 8, we have the ability to convert an object to another type using a map() method of Stream object with a lambda expression. The map() method is an intermediate operation in a stream object, so we need a terminal method to complete the stream.SyntaxStream map(Function
1K+ Views
Type Inference states that the data type of any expression. For instance, a method return type or argument type can be understood automatically by the compiler. Types in the argument list can be omitted since java already know the types of the expected parameters for the single abstract method of functional interface.Syntax(var1, var2 …) -> { method body }In the below example, we can sort a String[] array by its last character.Exampleimport java.util.Arrays; public class TypeInferencingLambdaTest { public static void main(String[] args) { String[] names = {"Raja", "Jai", "Adithya", "Surya", "Chaitanya", "Ravi", "Krishna"}; Arrays.sort(names, (s1, ... Read More
2K+ Views
An interface defined with only one abstract method is known as Functional Interface. It's not mandatory to mark the functional interface with @FunctionalInterface annotation, the compiler doesn't throw any error. But it’s good practice to use @FunctionalInterface annotation to avoid the addition of extra methods accidentally. If an interface annotated with @FunctionalInterface annotation and more than one abstract method then it throws a compile-time error.Syntax@FunctionalInterface interface interface_name { // only one abstarct method declaration }Example@FunctionalInterface interface Shape { void printArea(int x); } public class SquareTest { public static void main (String args[]) { Shape square ... Read More