DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Enterprise AI Trend Report: Gain insights on ethical AI, MLOps, generative AI, large language models, and much more.

2024 Cloud survey: Share your insights on microservices, containers, K8s, CI/CD, and DevOps (+ enter a $750 raffle!) for our Trend Reports.

PostgreSQL: Learn about the open-source RDBMS' advanced capabilities, core components, common commands and functions, and general DBA tasks.

AI Automation Essentials. Check out the latest Refcard on all things AI automation, including model training, data security, and more.

Related

  • Express Hibernate Queries as Type-Safe Java Streams
  • All Things Java 8 [Tutorials]
  • Optimizing Java Applications: Parallel Processing and Result Aggregation Techniques
  • The Long Road to Java Virtual Threads

Trending

  • Exploring the Frontiers of AI: The Emergence of LLM-4 Architectures
  • Modern Python: Patterns, Features, and Strategies for Writing Efficient Code (Part 1)
  • JUnit, 4, 5, Jupiter, Vintage
  • Securing Cloud Infrastructure: Leveraging Key Management Technologies
  1. DZone
  2. Coding
  3. Java
  4. Functional Approach To String Manipulation in Java

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.

By 
Sameer Shukla user avatar
Sameer Shukla
DZone Core CORE ·
Dec. 11, 23 · Tutorial
Like (7)
Save
Tweet
Share
8.8K Views

Join the DZone community and get the full member experience.

Join For Free

In 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.

API Character encoding Java (programming language) Stream (computing) Strings Function type

Opinions expressed by DZone contributors are their own.

Related

  • Express Hibernate Queries as Type-Safe Java Streams
  • All Things Java 8 [Tutorials]
  • Optimizing Java Applications: Parallel Processing and Result Aggregation Techniques
  • The Long Road to Java Virtual Threads

Partner Resources


Comments

ABOUT US

  • About DZone
  • Send feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: