Part 2
Interview questions on Java 8 Predicate Functional Interface for experienced covered in this post:
What are the default methods defined in
Predicate functional interface?
-
There are 3 default methods declared inside Predicate functional
interface.
1.
Negate():
negate() returns the logical negation of existing result.
package
simplifiedjava.crackedInterview.java8; import java.util.Arrays;
import
java.util.List; import java.util.function.Predicate; public class PredicateDefaultMethodDemo {
public
static
void
main(String[] args) { List<String> list = Arrays.asList("Yogesh","Arpita","Shweta","Shruti","Ananya","Asmita","Yogita");
Predicate<String> p1
= s
-> s.startsWith("S"); Predicate<String> p3 = p1.negate();
for(String s
: list) {
if(p1.test(s)) {
System.out.println(s);
}
}
}
} |
2.
And(): And() works like logical && just combine two conditions.
package simplifiedjava.crackedInterview.java8;
import
java.util.Arrays;
import
java.util.List; import java.util.function.Predicate; public class PredicateDefaultMethodDemo {
public
static
void
main(String[] args) {
List<Integer> list
= Arrays.asList(10,20,30,40,50,60,70,80,90,100);
Predicate<Integer> p2 = n -> n < 100; Predicate<Integer> p3 = p1.and(p2);
for(Integer s
: list) {
if(p3.test(s)) {
System.out.println(s);
}
}
}
}
Output:
60
70
80
90 |
3. Or(): Or() works like logical || just combine tow conditions.
package simplifiedjava.crackedInterview.java8;
import
java.util.Arrays;
import
java.util.List; import java.util.function.Predicate; public class PredicateDefaultMethodDemo {
public
static
void
main(String[] args) { List<String> list = Arrays.asList("Yogesh","Arpita","Shweta","Shruti","Ananya","Asmita","Yogita");
Predicate<String> p1
= s
-> s.startsWith("S"); Predicate<String> p2 = s -> s.startsWith("Y");
Predicate<String> p3
= p1.negate(); Predicate<String> p4 = p1.or(p2);
for(String s
: list) {
if(p4.test(s)) {
System.out.println(s);
}
}
}
}
Output:
Yogesh
Shweta
Shruti
Yogita
|
What are the static methods defined in
Predicate functional interface?
-
Predicate has defined isEqual () static method inside it.
-
Please refer to the below example for syntax.
package simplifiedjava.crackedInterview.java8; import java.util.function.Predicate; public class PredicateStaticMethodDemo {
public
static
void
main(String[] args) {
Predicate<String> p
= Predicate.isEqual("Java");
System.out.println(p.test("java"));
System.out.println(p.test("Java"));
System.out.println(p.test("Databse"));
}
}
Output:
false
true false |
Wap to implement Predicate interface?
package simplifiedjava.crackedInterview.java8;
import
java.util.ArrayList;
import
java.util.List; import java.util.function.Predicate; public class FindPositiveOrNegativeNoUsingPredicate { public static void main(String[] args) {
List<Integer> list
= new
ArrayList<Integer>();
list.add(-10);
list.add(20); list.add(30); Predicate<Integer> p = no -> no > 0;
for(Integer i
: list) {
if(p.test(i)) {
System.out.println("Positive Number");
}else
{
System.out.println("Negative Number");
}
}
}
}
Output:
Negative Number
Positive Number
Positive Number |
-
Basic core java interview questions and answers for freshers - Click here
-
Core java interview questions for 3 years experience - Click here
-
Core java interview questions and answers for 3-5 years exp - Click here
-
Core java interview questions and Answers for 5 - 7 Years exp - Click here
-
Basic Java Interview Questions and Answers - Click here
-
Java interview questions and answers on oops - Click here
-
Java interview questions and answers on Strings - Click here
-
Java interview questions on exception handling - Click here
-
Interview questions on multithreading in java for experienced - Click here
-
Interview questions on serialization in java for experienced
- Click here
-
Interview questions on inner class in java for experienced
- Click here
-
Interview questions on Collections in java for experienced
- Click here
Thank you techies for visiting this blog. I hope you enjoyed this blog and got more technical knowledge. I have tried to cover all types of questions and provided examples tested on eclipse as much as I can. Guys, please don’t just mug up the questions and answers. Try to clear your concepts with examples. Try to write a code on eclipse once you read the concepts. It will help you to memorize the concepts for a very long time. Simultaneously you will be prepared for interview programs as well. It will help you to survive in the IT sector for a long time. It may be easy to crack an interview but it's really tough to survive in the IT industry with inadequate knowledge and skills. I have collected all the questions from actual interviews attended by my friends, colleagues, college mate and classmate. I have covered frequently asked questions as well as challenging questions. I have included many programs to understand the concept thoroughly. I will try to explain the concept with the help of a real-time program in eclipse. You can share more questions which are not covered in this blog or post. Always welcome your suggestions and queries. I will definitely try to resolve it. Please comment your queries and new set of questions under the comment sections. I will create a new post for those questions.
My total experience is 10. Initially I had worked on
some support projects and then I moved to java
projects. I had worked with many multi-national
companies like R-Systems, Tata Consultancy Services,
Cybage Softwares. Fortunately, TCS and
Cybage has given me an opportunity to take interviews
for experienced candidates. I have conducted more than
1000 interviews by now. Mock sessions will be conducted for minimal charges. I will guide you personally on how to crack interviews. All sessions will be online sessions only. My interview book will be provided free of cost if you enroll for personal training. Once you have done practice then I assured you will definitely crack almost all java interviews. I have published my book named with "All MNC Java Interview" which is available on amazon. You can go through it or you can directly contact to me if you are interested to read the book. I will give you special discount. I have covered near about 550 questions and answers with program tested on eclipse and necessary diagram. I have covered interview questions asked in all reputed MNC like TCS, Infosys, Capgemini, Tech Mahindra, Cognizant, City Bank, Barclays, Amdocs, Mindtree etc. My purpose behind this book is, help you to get a job in your dream company. I can understand this is a struggling period for job seekers and freshers. Everybody must have gone from this phase. so never give up. Keep upgrading yourself. I am sure you will get a job in your dream company. All the best!!! Please reach out to me for personal training for interview preparation. You can reach out to me at mncjavainterview@gmail.com. |
0 Comments