- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 4747 Articles for Java 8

Updated on 14-Jul-2020 13:00:28
The variables defined by the enclosing scope of a lambda expression can be accessible within the lambda expression. A lambda expression can access to both instance, static variables and methods defined by the enclosing class. It has also access to "this" variable (both implicitly and explicitly) that can be an instance of enclosing class. The lambda expression also sets the value of an instance or static variable.Exampleinterface SimpleInterface { int func(); } public class SimpleLambdaTest { static int x = 50; public static void main(String[] args) { SimpleInterface test = () -> x; // Accessing of static variable ... Read More 
Updated on 14-Jul-2020 12:54:42
Method reference is one of a way in a lambda expression to refer a method without executing it. In the body part of a lambda expression, we can call another method if they are compatible with a functional interface. We can also capture "this" and "super" keywords in method references.In the below two examples, we can create a thread with the help of "this" and "super" keywords using method reference.Example of this keywordpublic class MethodRefThisTest { public void runBody() { for(int i = 1; i < 10; i++) { System.out.println("Square of " + i + " ... Read More 
Updated on 14-Jul-2020 12:48:28
DoubleUnaryOperator is a functional interface defined in java.util.function package. This functional interface expects a parameter of type double as input but produces the output of the same type. DoubleUnaryOperator interface can be used as an assignment target for lambda expression and method reference. This interface contains one abstract method: applyAsDouble(), one static method: identity() and two default methods: andThen() and compose().Syntax@FunctionalInterface public interface DoubleUnaryOperator { double applyAsDouble(double operand); }Example of Lambda Expressionimport java.util.function.DoubleUnaryOperator; public class DoubleUnaryOperatorTest1 { public static void main(String[] args) { DoubleUnaryOperator getDoubleValue = doubleValue -> { // lambda expression return doubleValue * 2; }; ... Read More 
Updated on 14-Jul-2020 12:25:24
LongSupplier is a built-in functional interface from java.util.function package. This interface doesn’t expect any input but produces a long-valued output. Since LongSupplier is a functional interface, it can be used as an assignment target for lambda expression and method reference and contains only one abstract method: getAsLong().Syntax@FunctionalInterface public interface LongSupplier { long getAsLong(); }Example of Lambda Expressionimport java.util.function.LongSupplier; public class LongSupplierLambdaTest { public static void main(String args[]) { LongSupplier supplier = () -> { // lambda expression return 75; }; long result = supplier.getAsLong(); System.out.println(result); } }Output75Example ... Read More 
Updated on 14-Jul-2020 12:06:47
LongPredicate is a functional interface defined in java.util.function package. This interface can be used mainly for evaluating an input of type long and returns an output of type boolean. LongPredicate can be used as an assignment target for a lambda expression or method reference. It contains one abstract method: test() and three default methods: and(), negate() and or().Syntax@FunctionalInterface interface LongPredicate { boolean test(long value); }Example of Lambda Expressionimport java.util.function.LongPredicate; public class LongPredicateLambdaTest { public static void main(String args[]) { LongPredicate longPredicate = (long input) -> { // lambda expression if(input == 50) { ... Read More 
Updated on 14-Jul-2020 12:05:06
A constructor reference is similar to method reference except that the name of a method is new. We can also create a constructor reference with an array type. For instance, if we need to create an integer array by using the constructor reference: int[]:: new, where the parameter is a length of an array.SyntaxArrayTypeName[]::newExample@FunctionalInterface interface ArrayCreator { int[] makeArray(int number); } public class ArrayConstructorRefTest { public static void main(String[] args) { ArrayCreator arrayCreator = int[]::new; // Constructor Reference for an Array int[] intArray = arrayCreator.makeArray(10); for(int i = 0; i < intArray.length; ... Read More 
Updated on 14-Jul-2020 11:50:12
A method reference can also be applicable to constructors in Java 8. A constructor reference can be created using the class name and a new keyword. The constructor reference can be assigned to any functional interface reference that defines a method compatible with the constructor.Syntax::newExample of Constructor Reference with One Argumentimport java.util.function.*; @FunctionalInterface interface MyFunctionalInterface { Student getStudent(String name); } public class ConstructorReferenceTest1 { public static void main(String[] args) { MyFunctionalInterface mf = Student::new; Function f1 = Student::new; // Constructor Reference Function f2 = (name) -> new Student(name); ... Read More 
Updated on 14-Jul-2020 09:22:47
IntBinaryOperator is a functional interface in Java 8 from java.util.function package. This interface expects two parameters of type int as input and produces an int type result. IntBinaryOperator can be used as an assignment target for a lambda expression or method reference. It contains only one abstract method: applyAsInt().Syntax@FunctionalInterface public interface IntBinaryOperator { int applyAsInt(int left, int right) }Exampleimport java.util.function.*; public class IntBinaryOperatorTest { public static void main(String[] args) { IntBinaryOperator test1 = (a, b) -> a + b; // lambda expression System.out.println("Addition of two parameters: " + test1.applyAsInt(10, 20)); IntFunction test2 = new IntFunction() { ... Read More 
Updated on 14-Jul-2020 09:16:29
Function interface is a functional interface from java.util.function package. This interface expects one argument as input and produces a result. Function interface can be used as an assignment target for a lambda expression or method reference. It contains one abstract method: apply(), two default methods: andThen() and compose() and one static method: identity().Syntax@FunctionalInterface public interface Function { R apply(T t); }Exampleimport java.util.function.Function; public class FunctionTest { public static void main(String[] args) { Function f1 = i -> i*4; // lambda System.out.println(f1.apply(3)); Function f2 = i -> i+4; // lambda ... Read More 
Updated on 14-Jul-2020 09:12:01
DoubleSupplier interface is a built-in functional interface defined in java.util.function package. This functional interface doesn’t expect any input but produces a double-valued output. DoubleSupplier interface can be used as an assignment target for lambda expression and method reference. This interface contains only one abstract method: getAsDouble().Syntax@FunctionalInterface public interface DoubleSupplier { double getAsDouble(); }Example of Lambda Expressionimport java.util.concurrent.ThreadLocalRandom; import java.util.function.DoubleSupplier; public class DoubleSupplierLambdaTest { public static void main(String args[]) { DoubleSupplier getRandomDouble = () -> { // lambda expression double doubleVal = ThreadLocalRandom.current().nextDouble(0000, 9999); return Math.round(doubleVal); }; ... Read More Advertisements