Create new module core-java-string-operations-6

Move code for the Find the Longest Word in a String article
This commit is contained in:
thibault.faure
2023-05-19 20:39:00 +02:00
parent 9001813855
commit 64d1902858
6 changed files with 40 additions and 2 deletions

View File

@@ -0,0 +1,4 @@
### Relevant Articles:
- [Find the Longest Word in a Given String in Java](https://www.baeldung.com/java-longest-word-string)

View File

@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-string-operations-6</artifactId>
<name>core-java-string-operations-6</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
</project>

View File

@@ -0,0 +1,36 @@
package com.baeldung.longestword;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
public class LongestWordFinder {
public Optional<String> findLongestWord(String sentence) {
return Optional.ofNullable(sentence)
.filter(string -> !string.trim()
.isEmpty())
.map(string -> string.split("\\s"))
.map(Arrays::asList)
.map(list -> Collections.max(list, Comparator.comparingInt(String::length)));
}
public List<String> findLongestWords(String sentence) {
if (sentence == null || sentence.trim()
.isEmpty()) {
return Collections.emptyList();
}
String[] words = sentence.split("\\s");
int maxWordLength = Arrays.stream(words)
.mapToInt(String::length)
.max()
.orElseThrow();
return Arrays.stream(words)
.filter(word -> word.length() == maxWordLength)
.collect(Collectors.toList());
}
}

View File

@@ -0,0 +1,62 @@
package com.baeldung.longestword;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;
class LongestWordFinderUnitTest {
LongestWordFinder longestWordFinder = new LongestWordFinder();
@Test
void givenNull_whenFindLongestWord_thenEmpty() {
assertThat(longestWordFinder.findLongestWord(null)).isEmpty();
}
@Test
void givenEmptyString_whenFindLongestWord_thenEmpty() {
assertThat(longestWordFinder.findLongestWord("")).isEmpty();
}
@Test
void givenStringWithOnlySpaces_whenFindLongestWord_thenEmpty() {
assertThat(longestWordFinder.findLongestWord(" ")).isEmpty();
}
@Test
void givenAPhraseWithALongestWord_whenFindLongestWord_thenLongestWordOfThePhrase() {
assertThat(longestWordFinder.findLongestWord("This is a phrase with words")).hasValue("phrase");
}
@Test
void givenAPhraseWithVariousWordsOfMaxLength_whenFindLongestWord_thenAnyOfTheLongestsWordsOfThePhrase() {
assertThat(longestWordFinder.findLongestWord("Baeldung is another word of size eight in this sentence")
.get()).isIn("Baeldung", "sentence");
}
@Test
void givenNull_whenFindLongestWords_thenEmpty() {
assertThat(longestWordFinder.findLongestWords(null)).isEmpty();
}
@Test
void givenEmptyString_whenFindLongestWords_thenEmpty() {
assertThat(longestWordFinder.findLongestWords("")).isEmpty();
}
@Test
void givenStringWithOnlySpaces_whenFindLongestWords_thenEmpty() {
assertThat(longestWordFinder.findLongestWords(" ")).isEmpty();
}
@Test
void givenAPhraseWithALongestWord_whenFindLongestWords_thenLongestWordOfThePhrase() {
assertThat(longestWordFinder.findLongestWords("This is a phrase with words")).containsExactly("phrase");
}
@Test
void givenAPhraseWithVariousWordsOfMaxLength_whenFindLongestWords_thenAllLongestsWords() {
assertThat(longestWordFinder.findLongestWords("Baeldung is another word of size eight in this sentence")).containsExactly("Baeldung", "sentence");
}
}