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 142 of 440
1K+ Views
The method reference is similar to a lambda expression that refers to a method without executing it and "::" operator can be used to separate a method name from the name of an object or class in a method reference.The methods can be referenced with the help of this and super keywords in Java. The super keyword can be used as a qualifier to invoke the overridden method in a class or an interface.syntaxthis::instanceMethod TypeName.super::instanceMethodExampleimport java.util.function.Function; interface Defaults { default int doMath(int a) { return 2 * a; } } public class Calculator implements Defaults { ... Read More
6K+ Views
Given a long number containing digits of a credit card number; the task is to find whether the credit card number is valid or not with a program.For checking a credit card is valid or not, the following are the validations we have to be sure for declaring the result.A credit card’s number must have 13 to 16 digits, it must start with the following digits.All the visa cards start from 4 All the master cards start from 537 is the starting for American express cardsAll the discover cards start from 6Steps to check whether the credit card is valid or ... Read More
3K+ Views
A functional Interface for which a lambda expression has invoked is called target type of lambda expression. It means that if a lambda expression has invoked for some "X" interface then "X" is the target type of that lambda expression. Hence, we conclude that lambda expressions can be used only in those situations where java compiler can determine the Target Type.In the below example, the target type of lambda expression is BiFunction. An instance of a class is automatically created that implements the functional interface and lambda expression provides an implementation of the abstract method declared by the functional interface.Exampleinterface BiFunction { ... Read More
3K+ Views
The conditional operator is used to make conditional expressions in Java. It is also called a Ternary operator because it has three operands such as boolean condition, first expression, and second expression.We can also write a conditional expression in lambda expression in the below program.Exampleinterface Algebra { int substraction(int a, int b); } public class ConditionalExpressionLambdaTest { public static void main(String args[]) { System.out.println("The value is: " + getAlgebra(false).substraction(20, 40)); System.out.println("The value is: " + getAlgebra(true).substraction(40, 10)); } static Algebra getAlgebra(boolean reverse) { Algebra alg = reverse ... 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
719 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
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