generated from skills/github-pages
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from brunobaiano/tip-19
tip-19
- Loading branch information
Showing
26 changed files
with
1,943 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
--- | ||
title: "Daily tip #19" | ||
date: 2023-11-24 | ||
--- | ||
|
||
## Tip of the day #19 | ||
|
||
Use the joining collector and stop to write loops to concatenate strings. | ||
|
||
|
||
### What is it? | ||
|
||
The joining collector is a new collector that was introduced in Java 8. | ||
It is a special collector that is used to concatenate strings. | ||
|
||
We have 3 different ways to use it: | ||
|
||
1 - join all strings without any delimiter | ||
|
||
```java | ||
// 1 | ||
String result = Stream.of("a", "b", "c") | ||
.collect(Collectors.joining()); | ||
// abc | ||
``` | ||
|
||
2 - join all strings with a delimiter | ||
|
||
```java | ||
// 2 | ||
String result = Stream.of("a", "b", "c") | ||
.collect(Collectors.joining(", ")); | ||
// a, b, c | ||
``` | ||
|
||
3 - join all strings with a delimiter and a prefix and suffix | ||
|
||
```java | ||
// 3 | ||
String result = Stream.of("a", "b", "c") | ||
.collect(Collectors.joining(", ", "[", "]")); | ||
// [a, b, c] | ||
``` | ||
Important to note that all then join the strings in the same order that they are in the stream. | ||
|
||
They are also null-safe, so if you have a null value in the stream, it will be ignored. | ||
|
||
After today, do not use more StringBuilders to concatenate strings anymore, use the joining collector instead. | ||
|
||
### How to | ||
|
||
Take a look in [repo-tip-19](https://github.com/brunobaiano/tip-of-the-day/tree/main/tip-19) to see a real example of today's tip. | ||
|
||
Because @QuarkusTest and @ParameterizedTest has some issues, I created a new repo to show the example using Spring Boot. | ||
Here we can use records. | ||
[repo-tip-19-spring](https://github.com/brunobaiano/tip-of-the-day/tree/main/tip-19-spring) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
HELP.md | ||
target/ | ||
!.mvn/wrapper/maven-wrapper.jar | ||
!**/src/main/**/target/ | ||
!**/src/test/**/target/ | ||
|
||
### STS ### | ||
.apt_generated | ||
.classpath | ||
.factorypath | ||
.project | ||
.settings | ||
.springBeans | ||
.sts4-cache | ||
|
||
### IntelliJ IDEA ### | ||
.idea | ||
*.iws | ||
*.iml | ||
*.ipr | ||
|
||
### NetBeans ### | ||
/nbproject/private/ | ||
/nbbuild/ | ||
/dist/ | ||
/nbdist/ | ||
/.nb-gradle/ | ||
build/ | ||
!**/src/main/**/build/ | ||
!**/src/test/**/build/ | ||
|
||
### VS Code ### | ||
.vscode/ |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.5/apache-maven-3.9.5-bin.zip | ||
wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar |
Oops, something went wrong.