UrbanPro
true

Learn Java Training from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Java 8 Predicates

Shiva Kumar
11/10/2018 0 0

In the previous lession, we have learnt how to use filters and collectors. In filter we have passed the condition to evaluate whether the object is eligible to be filtered or not. Code given below for reference and highlighted in bold.

public class NowJava8 {

public static void main(String[] args) {

List lines = Arrays.asList("abc", "xyz", "shiva");

List result = lines.stream() 
.filter(line -> !"shiva".equals(line)) 
.collect(Collectors.toList()); 

result.forEach(System.out::println); //output : abc,xyz

}

}

Now, what if I would like to same condition in different places to filter on different collections. We would end up repleating same piece of code.
What does it mean? we break DRY (Do not repeat yourself).

Here to solve this we can use predicates. Example given below.
 import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class PredicatesExample
{
public static Predicate<String> isShiva() {
return s -> s.equalsIgnoreCase("shiva");
}


public static void main(String[] args) {
List<String> lines = Arrays.asList("abc", "xyz", "shiva");

List<String> result = lines.stream()
.filter(isShiva().negate())
.collect(Collectors.toList());

result.forEach(System.out::println); //output : abc,xyz
 
List<String> names = Arrays.asList("abc", "dhdh", "shiva","ddds","edsd");
 
 result =names.stream() 
.filter(isShiva().negate())
.collect(Collectors.toList()); 

result.forEach(System.out::println); //output : abc,dhdh,ddds,edsd
}
}  

As you see, we have used same predicate for two different collections.
0 Dislike
Follow 1

Please Enter a comment

Submit

Other Lessons for You

Amazon written test for experience
For the below written test answer I defined with "*" at the end of the options NULL is the same as 0 for integer the same as blank for character * the same as 0 for integer and blank for character the...

Most Important Java Interview Topics
Dear All, I am here to help on java interview preparation for fresher and experienced people, please get in touch with me for further details. All interview preparation will be with unique teaching style...

Java Overview
Java platform overview Java platform components The Java platform consists of the following components. The Java language Like any programming language, the Java language has its own structure, syntax...
A

Akshay Shende

0 0
0

Understanding concept of Inheritance in Java
Inheritance: Acquiring a properties and methods from Parent class to Child class. For example, In some cases we can hear from any one of your relative or your friends will say like that "You are bahaving...

CoreJAVA
Core Java Training High Level Course Content Trained by Java Architect 1. Core Java Programming Introduction of Java 2. Data types and Operators 3. Control Flow statements 4. OOPS and its application...
A
X

Looking for Java Training Classes?

The best tutors for Java Training Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Java Training with the Best Tutors

The best Tutors for Java Training Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more