0% found this document useful (0 votes)
8 views

String Operations With Java and Stream API Bael…

The document provides an overview of Java 8's Stream API, focusing on string operations such as joining, splitting, and converting string arrays to maps. It includes code examples demonstrating how to use the Stream API for these tasks, along with testing methods to verify functionality. The conclusion emphasizes the efficiency of the Stream API in data processing and memory management.

Uploaded by

Joshua Castillo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

String Operations With Java and Stream API Bael…

The document provides an overview of Java 8's Stream API, focusing on string operations such as joining, splitting, and converting string arrays to maps. It includes code examples demonstrating how to use the Stream API for these tasks, along with testing methods to verify functionality. The conclusion emphasizes the efficiency of the Stream API in data processing and memory management.

Uploaded by

Joshua Castillo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

String

Operations with
Java Streams
Last modified: August 6, 2020

by baeldung

Java +

Java 8 Java Streams

Get started with Spring 5 and


Spring Boot 2, through the
Learn Spring course:

> CHECK OUT THE COURSE

1. Overview

Java 8 has introduced a new Stream API


that lets us process data in a declarative
manner.
In this quick article, we would learn how to
use the Stream API to split a comma-
separated String into a list of Strings and
how to join a String array into a comma-
separated String.
We'll also look at how to convert a string
array to map using Stream API.
Nearly all of the time we face situations,
where we need to iterate some Java
Collections and filter the Collection based
on some filtering logic. In a traditional
approach for this type of situation, we
would use lots of loops and if-else
operations to get the desired result.
If you want to read more about the Stream
API, check this article.

2. Joining Strings With the


Stream API

Let's use the Stream API to create a


function which would join a String array into
a comma-separated String:

public static String join


join(String[]
arrayOfString){
return
Arrays.asList(arrayOfString)
.stream()
//.map(...)

.collect(Collectors.joining(","));
}

Points to note here:


The stream() function converts any
Collection into a stream of data
map() function is used to process the
data
There is also another function, named
filter(), where we can include filtering
criteria
There can be scenarios, where we may
want to join a String with some fixed prefix
and postfix. With the Stream API we can do
that in the following way:

public static String


joinWithPrefixPostfix
joinWithPrefixPostfix(String[]
arrayOfString){
return
Arrays.asList(arrayOfString)
.stream()
//.map(...)
.collect(Collectors.joining(",","
[","]"));
}

As we can see in the Collectors.joining()


method, we are declaring our prefix as ‘[‘
and postfix as ‘]'; hence the generated
String will be created with declared […..]
format.

3. Splitting Strings With


Stream API

Now, let's create a function, which would


split a comma separated String into a list of
String using Stream API:

public static List<String>


split
split(String str){
return Stream.of(str.split(","))
.map (elem -> new String
String(elem))
.collect(Collectors.toList());
}

It's also possible to directly convert a String


to a Character list using the Stream API:

public static List<Character>


splitToListOfChar
splitToListOfChar(String str) {
return str.chars()
.mapToObj(item -> (char
char) item)
.collect(Collectors.toList());
}

One interesting fact to note here is that the


chars() method converts the String into a
stream of Integer where each Integer value
denotes the ASCII value of each and every
Char sequence. That's why we need to
explicitly typecast the mapper object in the
mapToObj() method.

4. String Array to Map


With Stream API

We can also convert a String array to map


using split and Collectors.toMap, provided
each item in the array contains a key-value
entity concatenated by a separator:

public static Map<String, String>


arrayToMap
arrayToMap(String[] arrayOfString) {
return
Arrays.asList(arrayOfString)
.stream()
.map(str -> str.split(":"))
.collect(toMap(str -> str[0],
str -> str[1]));
}

Here, “:” is the key-value separator for all


the elements in String array.
Please remember that in order to avoid
compilation error, we need to ensure that
code is compiled using Java 1.8. To do this,
we need to add the following plugin in
the pom.xml:

<build
build>
<plugins
plugins>
<plugin
plugin>

<groupId
groupId>org.apache.maven.plugins</gro
gro
upId
upId>
<artifactId
artifactId>maven-compiler-
plugin</artifactId
artifactId>
<version
version>3.3</version
version>
<configuration
configuration>
<source
source>1.8</source
source>
<target
target>1.8</target
target>
</configuration
configuration>
</plugin
plugin>
</plugins
plugins>
</build
build>

5. Testing

Since we are done creating the functions,


let's create test cases to verify the
outcome.
First, let's test our simple joining method:

@Test
public void
givenArray_transformedToStream_conv
ertToString
ertToString() {
String[] programmingLanguages =
{"java", "python", "nodejs", "ruby"};
String expectation =
"java,python,nodejs,ruby";

String result =
JoinerSplitter.join(programmingLanguage
s);
assertEquals(result, expectation);
}

Next, let's create another one to test our


simple splitting functionality:

@Test
public void
givenString_transformedToStream_con
vertToList
vertToList() {
String programmingLanguages =
"java,python,nodejs,ruby";

List<String> expectation = new


ArrayList
ArrayList<>();
expectation.add("java");
expectation.add("python");
expectation.add("nodejs");
expectation.add("ruby");

List<String> result =
JoinerSplitter.split(programmingLanguag
es);

assertEquals(result, expectation);
}

Finally, let's test our String array to map


functionality:

@Test
public void
givenStringArray_transformedToStrea
m_convertToMap
m_convertToMap() {

String[] programming_languages =
new String
String[]
{"language:java","os:linux","editor:ema
cs"};

Map<String,String> expectation=new
new
HashMap
HashMap<>();
expectation.put("language",
"java");
expectation.put("os", "linux");
expectation.put("editor", "emacs");

Map<String, String> result =


JoinerSplitter.arrayToMap(programming_l
anguages);
assertEquals(result, expectation);

In the same way, we need to create the rest


of the test cases.

6. Conclusion

Stream API provides us with sophisticated


data processing techniques. This new way
of writing code is very e"cient in terms of
heap memory management in a multi-
threaded environment.
Like always, the full source code is
available over on Github.

Get started with Spring 5


and Spring Boot 2,
through the Learn Spring
course:

>> CHECK OUT THE COURSE

Learning to build your


API
with Spring?
Download the E-book

Comments are closed on this article!

COURSES

ALL COURSES
ALL BULK COURSES
THE COURSES PLATFORM

SERIES

JAVA “BACK TO BASICS” TUTORIAL


JACKSON JSON TUTORIAL
APACHE HTTPCLIENT TUTORIAL
REST WITH SPRING TUTORIAL
SPRING PERSISTENCE TUTORIAL
SECURITY WITH SPRING
SPRING REACTIVE TUTORIALS

ABOUT

ABOUT BAELDUNG
THE FULL ARCHIVE
EDITORS
JOBS
OUR PARTNERS
PARTNER WITH BAELDUNG

TERMS OF SERVICE
PRIVACY POLICY
COMPANY INFO
CONTACT

You might also like