Predicate.not() Method in Java with Examples6 Jan 2025 | 4 min read Java 11 introduces a tool called Predicate.not() to make negating predicates easier. Predicates, which are frequently employed in filtering and conditional logic, are functional interfaces that express boolean-valued functions of a single argument. Negating a predicate in Java 11 required a significantly lengthier method. Predicate.not() makes the syntax clearer and easier to read. In this section, we will discuss the Predicate.not() method in detail, along with examples to show how to use it. What is a Predicate?A functional interface defined in the java.util.function package is known as a predicate in Java. It stands for a function with a single argument that yields a boolean result. Test(T t), the main method in the Predicate interface, assesses the predicate based on the supplied argument. Traditional Predicate NegationPrior to Java 11, the negate() method was required in order to negate a predicate. Negating a predicate that determines whether a string is empty, for instance, would look like this: Although this method is effective, it can be laborious when combining several predicates or using it in more complicated expressions. Introduction of Predicate.not()Java 11 introduced the Predicate.not() method, providing a more straightforward way to negate predicates. The Predicate.not() method is a static method that takes a predicate and returns its negation. This method simplifies the syntax for negating predicates, making the code more readable and concise. Advantages of Predicate.not() Method
Example 1: Filtering a List of StringsSuppose, we have a list of strings, and we want to filter out the empty strings. Using Predicate.not(), we can achieve this as follows: File Name: PredicateNotExample.java Output: [Java, Predicate, Not] Explanation The PredicateNotExample class shows how to filter out empty strings from a list using the Predicate.not() function. The first step of the class is to import the packages needed to work with streams, predicates, and lists. The main method creates a list of strings that includes both empty and non-empty strings. After converting this list to a stream, the code excludes empty strings using Predicate.not(String::isEmpty) in conjunction with the filter() method. Comparing this to other techniques, the negative is clearer and easier to comprehend. The output [Java, Predicate, Not] is produced by gathering the filtered results back into a list and printing it. Example 2: Negating a Custom PredicateConsider a custom predicate that checks if a number is even. Using Predicate.not(), you can easily create a predicate to check for odd numbers: File Name: PredicateNotExample.java Output: [1, 3, 5] Explanation The PredicateNotExample class provides an example of how to filter out even numbers from a list using the Predicate.not() function, so retaining just the odd numbers. The class imports the essential predicates, stream operations, and list handling packages. A predicate called isEven is defined in the main() method to determine whether a given number is even. Next, an integer list ranging from 1 to 6 is generated. After converting this list to a stream, Predicate.not(isEven) is used in the filter method to remove even numbers from the list, leaving only odd numbers. The output is produced by compiling the filtered stream back into a list and printing it [1, 3, 5]. ConclusionPredicate negation code can now be written in a more expressive and readable manner thanks to the addition of the Predicate.not() method to the Java 11 standard library. Developers can build more readable and concise code by utilising Predicate.not(), especially when working with streams and conditional logic. Predicate.not() provides a simple and elegant predicate negation solution, whether you're filtering collections or defining complex conditions. |
Java, one of the most widely used programming languages, has continuously evolved to enhance developer productivity and code readability. With the release of Java 10, the introduction of the var keyword allowed developers to declare local variables without explicitly specifying their data types. This feature brought...
4 min read
Difference Between () and Line() Methods in Java Java () Method The () method in java is present in the Scanner class and is used to get the input from the user. In order to use this method, a Scanner object needs to be created. The method can...
5 min read
Java is among the most accessed programming languages. Java is known for its tendency to run on multiple operating systems without requiring any modifications to the Java application. This post will aid one in verifying their Java version in macOS, understanding its importance, using multiple versions,...
4 min read
Java's Arrays class in the java.util package offers a range of static methods facilitating operations on arrays. It provides functionality to fill, sort, search, and more. The methods enhance array manipulation, contributing to cleaner and more efficient code. Let's examine the operations provided by the Arrays class...
11 min read
In this section, we will learn what is a Goldbach number and also create Java programs to check if the given number is Goldbach or not. The Goldbach number Java program frequently asked in Java coding tests to check the logic of the programmer. In 1742, German...
5 min read
In the world of Java programming, graphical user interfaces (GUIs) play a crucial role in providing a user-friendly and interactive experience. GUI components are the building blocks of these interfaces, allowing developers to design and create sophisticated applications. Among these components, two fundamental concepts stand out:...
3 min read
In Java, the ">>>" operator is the right shift zero fill operator. When using the right shift operator in Java, the bits of a number are shifted to the right, and any bits that are shifted beyond the right end are discarded. The bits shifted from...
3 min read
In programming, converting one type to/ from a type is a crucial task. Sometimes we require, conversion from one type to another type. In the Java conversion section, we have discoed various types of conversions. In this section, we can discuss how to convert binary to...
5 min read
It is a problem frequently asked in interviews of top IT companies like Google, Amazon, TCS, Accenture, etc. To solve the problem, one wants to check the logical ability, critical thinking, and problem-solving skills of the interviewee. So, in this section, we are going to solve...
5 min read
When it comes to developing and maintaining Java applications, tools that aid in dependency analysis and identifying deprecated APIs are invaluable. Two such tools provided by the Java platform are Jdeps and Jdeprscan. Despite their seemingly similar purposes, these tools have distinct functionalities and use cases....
3 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India