
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Custom UnaryOperator implementation in java.
The java.util.function.UnaryOperator interface and can be used as assignment target for lambda expressions, it represents operation on a single operand whose result will be of same type as the input. We can create our own UnaryOperator by implementing this interface.
The replaceAll() method of the List interface accept an object of the UnaryOperator representing a particular operation performs the specified operation on all the elements of the current list and replaces the existing values with the resultant values.
In the following example we are implementing the UnaryOperator interface and creating a custom unary operator object and trying to pass it as an argument to the replaceAll() method.
Example
import java.util.ArrayList; import java.util.function.UnaryOperator; class Op implements UnaryOperator<String> { public String apply(String str) { return str.toUpperCase(); } } public class Test { public static void main(String[] args) throws CloneNotSupportedException { ArrayList<String> list = new ArrayList<>(); list.add("Java"); list.add("JavaScript"); list.add("CoffeeScript"); list.add("HBase"); list.add("OpenNLP"); System.out.println("Contents of the list: "+list); list.replaceAll(new Op()); System.out.println("Contents of the list after replace operation: \n"+list); } }
Output
Contents of the list: [Java, JavaScript, CoffeeScript, HBase, OpenNLP] Contents of the list after replace operation: [JAVA, JAVASCRIPT, COFFEESCRIPT, HBASE, OPENNLP]
- Related Articles
- List.replaceAll(UnaryOperator operator) method in Java
- How to use UnaryOperator interface in lambda expression in Java?
- Custom exceptions in Java
- Custom ArrayList in Java
- OpenCV Probabilistic Hough Line Transform implementation in Java.
- What are custom exceptions in Java?
- OpenCV Hough Line Transform implementation using Java.
- Java implementation of OpenCV Hough Circle Transform.
- Difference Between Priority Queue and Queue Implementation in Java?
- Custom instance creator using Gson in Java?
- User defined and custom exceptions in Java
- RPA Implementation
- How can we implement a Custom HashSet in Java?
- How to implement custom FieldNamingStrategy using Gson in Java?
- How to create a custom unchecked exception in Java?

Advertisements