
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
Implement LongPredicate Using Lambda and Method Reference in Java
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 Expression
import java.util.function.LongPredicate; public class LongPredicateLambdaTest { public static void main(String args[]) { LongPredicate longPredicate = (long input) -> { // lambda expression if(input == 50) { return true; } else return false; }; boolean result = longPredicate.test(50); System.out.println(result); } }
Output
true
Example of method reference
import java.util.function.LongPredicate; public class LongPredicateMethodRefTest { public static void main(String args[]) { LongPredicate intPredicate = LongPredicateMethodRefTest::test; // method reference boolean result = intPredicate.test(30); System.out.println(result); } static boolean test(long input) { if(input == 50) { return true; } else return false; } }
Output
false
Advertisements