Merge pull request #13934 from thibaultfaure/articles/BAEL-6438-find-longest-word-in-given-string

BAEL-6438 Code for the Find the longest word in a given string article
This commit is contained in:
davidmartinezbarua 2023-05-09 00:41:01 -03:00 committed by GitHub
commit 1fd15c9557
2 changed files with 98 additions and 0 deletions

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");
}
}