The Predicate functional interface from the java.util.function package that was introduced in Java 8 contains a default method called negate() which returns a logical negation of the predicate function test().
default Predicate<T> negate()
Let's take a look at a few simple examples with negate() as lambda expressions.
Example 1: Check if Number is Not Even using negate()
package org.code2care.examples;
import java.util.function.Predicate;
/**
*
* Java 8 Predicate negate() Example
* version: v1.0
* Author: Code2care.org
* Date: 11 Oct 2023
*
*/
public class PredicateNegateExample {
public static void main(String... args) {
Predicate<Integer> isEven = num -> num % 2 == 0;
//Using negate()
Predicate<Integer> isNotEven = isEven.negate();
int no1 = 12;
int no2 = 19;
String result1 = isEven.test(no1) ? "even" : "not even";
String result2 = isNotEven.test(no2) ? "not even" : "even";
System.out.println(no1 + " is " + result1 + ".");
System.out.println(no2 + " is " + result2 + ".");
}
}
Output:
Example 2: Products not on Sale
package org.code2care.examples;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
class Item {
private String deviceName;
private boolean onSale;
public Item(String deviceName, boolean onSale) {
this.deviceName = deviceName;
this.onSale = onSale;
}
public boolean isOnSale() {
return onSale;
}
public String getName() {
return deviceName;
}
}
public class ShoppingCartExample {
public static void main(String[] args) {
List<Item> applePC = new ArrayList<>();
applePC.add(new Item("Macbook", false));
applePC.add(new Item("Mac Mini", true));
applePC.add(new Item("iMac", false));
applePC.add(new Item("Mac Studio", true));
Predicate<Item> isOnSale = Item::isOnSale;
//Negate
Predicate<Item> isNotOnSale = isOnSale.negate();
applePC.stream()
.filter(isNotOnSale)
.forEach(item -> System.out.println(item.getName() + " is not on sale."));
}
}
Output:
Facing issues? Have Questions? Post them here! I am happy to answer!
Author Info:
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org
More Posts related to Java,
- Get the current timestamp in Java
- Java Stream with Multiple Filters Example
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Fix: UnsupportedClassVersionError: Unsupported major.minor version 63.0
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- 7 deadly java.lang.OutOfMemoryError in Java Programming
- How to Calculate the SHA Hash Value of a File in Java
- Java JDBC Connection with Database using SSL (https) URL
- How to Add/Subtract Days to the Current Date in Java
- Create Nested Directories using Java Code
- Spring Boot: JDBCTemplate BatchUpdate Update Query Example
- What is CA FE BA BE 00 00 00 3D in Java Class Bytecode
- Save Java Object as JSON file using Jackson Library
- Adding Custom ASCII Text Banner in Spring Boot Application
- [Fix] Java: Type argument cannot be of primitive type generics
- List of New Features in Java 11 (JEPs)
- Java: How to Add two Maps with example
- Java JDBC Transition Management using PreparedStatement Examples
- Understanding and Handling NullPointerException in Java: Tips and Tricks for Effective Debugging
- Steps of working with Stored Procedures using JDBCTemplate Spring Boot
- Java 8 java.util.Function and BiFunction Examples
- The Motivation Behind Generics in Java Programming
- Get Current Local Date and Time using Java 8 DateTime API
- Java: Convert Char to ASCII
- Deep Dive: Why avoid java.util.Date and Calendar Classes
More Posts:
- Python: Merge two dictionaries into one dictionary - Python
- How to undo last Git Commit on Local Repository? - Git
- Execute .bin and .run file Ubuntu Linux - Linux
- SwissCovid App Launched in Switzerland - News
- Not receiving email notification alert in SharePoint Online workflow - Power Automate, FLOW - SharePoint
- List of jar files for Jax-ws (SOAP) based Java Web Services - Java
- How to Print to stderr in Python - Python
- Using Java 8 Predicate with 2 Arguments Example - Java