🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
In Java, the UnaryOperator interface is a functional interface that represents an operation on a single operand that produces a result of the same type. It is part of the java.util.function package and is commonly used for operations that transform or modify an object of a certain type.
Table of Contents
- What is
UnaryOperator? - Methods and Syntax
- Examples of
UnaryOperator - Real-World Use Case
- Conclusion
1. What is UnaryOperator?
UnaryOperator is a functional interface that takes an operand of type T and returns a result of the same type. It is useful for scenarios where a single value needs to be transformed or modified.
2. Methods and Syntax
The main method in the UnaryOperator interface is:
T apply(T t): Applies this operator to the given operand and returns a result.
Syntax
UnaryOperator<T> operator = (T t) -> {
// operation on t
return result;
};
3. Examples of UnaryOperator
Example 1: Incrementing a Number
import java.util.function.UnaryOperator;
public class IncrementExample {
public static void main(String[] args) {
// Define a UnaryOperator that increments a number by 1
UnaryOperator<Integer> increment = (value) -> value + 1;
int result = increment.apply(5);
System.out.println("Incremented Value: " + result);
}
}
Output:
Incremented Value: 6
Example 2: Converting String to Uppercase
import java.util.function.UnaryOperator;
public class UppercaseExample {
public static void main(String[] args) {
// Define a UnaryOperator that converts a string to uppercase
UnaryOperator<String> toUpperCase = (str) -> str.toUpperCase();
String result = toUpperCase.apply("hello");
System.out.println("Uppercase: " + result);
}
}
Output:
Uppercase: HELLO
4. Real-World Use Case: Removing Whitespace from a String
In applications, UnaryOperator can be used to remove leading and trailing whitespace from a string.
import java.util.function.UnaryOperator;
public class TrimWhitespaceExample {
public static void main(String[] args) {
// Define a UnaryOperator to trim whitespace from a string
UnaryOperator<String> trimWhitespace = (str) -> str.trim();
String result = trimWhitespace.apply(" Hello, World! ");
System.out.println("Trimmed String: '" + result + "'");
}
}
Output:
Trimmed String: 'Hello, World!'
Conclusion
The UnaryOperator interface is a practical tool in Java for transforming or modifying objects of a certain type. It is particularly beneficial in scenarios requiring simple transformations or modifications. Using UnaryOperator can lead to cleaner and more efficient code, especially in functional programming contexts.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment