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

On the Job training is always best
On the job training always provides an opportunity to learn the best industry practices. While you work on real time you would encounter many challenges that will force you to learn many new things. Class...
M

Comparable vs Comparator
java.lang.Comparable java.util.Comparator For comparing some other object with its own object. Ex. I am comparing myself to some other employee. Method signature is: int compareTo (T object). For...

Importance of Constructor's Visibility.
While developing program or implementing Singleton pattern we have learnt to mention constructor as private and known reason is -> to blocked its direct call from outside of class. But did we think...
M

Session Tracking In Java Servlets
Session Tracking: HTTP is a stateless protocol. Each request is independent of the previous one. However, in some applications, it is necessary to save state information so that information can be collected...

Top Programming Languages 2017: Every Beginner Should Learn
Every year a plethora of job opportunities are being created for skilled programmers. So if you are thinking of honing your coding skills it is really a bright idea. But with so many programming languages...
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