Functional Approach To String Manipulation in Java
This article delves into a few methods within the String class that enable processing to be conducted in a functional programming manner.
Join the DZone community and get the full member experience.
Join For FreeIn the most recent updates to Java, the String class has undergone a series of significant method additions. Certain methods now yield instances of the Stream class, while some of them are Higher Order functions. The intention behind incorporating these methods is to offer a streamlined approach for handling strings in a stream-oriented manner.
Handling strings in a stream-oriented manner brings the advantage of simplifying code and enhancing expressiveness. This makes it easier to apply operations like filtering, mapping, reduction, and more.
Another advantage is that the Stream API enables parallel processing, allowing the utilization of parallel streams with these methods that return streams. This makes it possible to leverage multicore processors for the efficient handling of large strings.
This article delves into a few methods within the String class that enable processing to be conducted in a functional programming manner.
# chars (): The 'chars ()' method facilitates effective character management in Java by returning an IntStream. This IntStream represents a sequence of Integer values, each corresponding to the Unicode code point of the characters within the provided string.
A code point is a numeric identifier assigned to a character in the Unicode standard, serving the purpose of character encoding.
Let’s understand the ‘chars ()’ by an example. Write a program that removes the given character from the string. Let's explore tackling this challenge through an imperative, non-functional approach, avoiding the use of the chars() or stream methodology.
private static String removeChar(String input, char c){
StringBuilder sb = new StringBuilder();
char[] charArray = input.toCharArray();
for (char ch : charArray) {
if (ch != c) {
sb.append(ch);
}
}
return sb.toString();
}
Let's compare this with the functional approach:
private static String removeChar(String str, char c){
return str.chars()
.filter(ch -> ch != c)
.mapToObj(ch -> String.valueOf((char) ch))
.collect(Collectors.joining());
}
The imperative, non-functional approach involves traditional iteration over the characters using a StringBuilder to build the modified string. On the other hand, the functional approach leverages the chars() method and the Stream API, providing a more concise and expressive solution.
# transform ( ): The transform function is a higher-order function that accepts Function as an argument. The transform function offers a more concise and functional way to apply transformations to the string.
The transform function can be used in chaining transformations in string for example, consider a scenario where you want to clean and format user input entered in a form. Users might input their names with extra white spaces, mixed capitalization, and unnecessary characters. String chaining can be employed to standardize and clean up this input.
String userInput = " JoHN-dOe ";
String cleanedInput = userInput
.transform(String :: trim)
.transform(String :: toLowerCase)
.transform(user -> user.replaceAll("-", ""));
cleanedInput // johndoe
# lines (): The lines function returns a stream of lines extracted from the given string, separated by line terminators such as \n, \r, and \r\n. The Java String lines() method proves advantageous over the split() method due to its lazy element supply and faster detection of line terminators. In cases where the string is empty, the lines’ function returns zero lines.
String text = "The lines function returns a stream of lines extracted ,\nThe Java String lines() method proves advantageous ;\nIn cases where the string is empty,\n"
+ "the lines’ function returns zero lines.";
text.lines()
.map(String :: toUpperCase)
.filter(line -> line.contains("I"))
.forEach(System.out::println);
The ‘text’ string contains multiple lines of text. We use the lines() method to obtain a stream of lines from the text. We then use the map operation to convert each line to the uppercase. The filter operation is applied to keep only the lines containing the letter 'I', and the forEach operation prints the modified lines.
The functions explained provide a powerful and concise way to work with strings. They offer a functional approach by leveraging streams for efficient manipulation and filtering, promoting immutability. Chaining these functions with other stream operations allows for complex yet concise transformations, promoting a cleaner and more functional style.
Opinions expressed by DZone contributors are their own.
Comments