Skip to content

Commit

Permalink
Merge pull request #22 from brunobaiano/tip-19
Browse files Browse the repository at this point in the history
tip-19
  • Loading branch information
brunobaiano committed Nov 24, 2023
2 parents 2e71d1d + 4c8f687 commit 6b5b107
Show file tree
Hide file tree
Showing 26 changed files with 1,943 additions and 0 deletions.
56 changes: 56 additions & 0 deletions _posts/2023-11-24-daily-tip-19.md
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)
33 changes: 33 additions & 0 deletions tip-19-spring/.gitignore
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 added tip-19-spring/.mvn/wrapper/maven-wrapper.jar
Binary file not shown.
2 changes: 2 additions & 0 deletions tip-19-spring/.mvn/wrapper/maven-wrapper.properties
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
Loading

0 comments on commit 6b5b107

Please sign in to comment.