From daf01922dea076d757d3c499bfa97c95850d9949 Mon Sep 17 00:00:00 2001 From: "thibault.faure" Date: Mon, 1 May 2023 13:01:42 +0200 Subject: [PATCH] BAEL-6438 Code for the Find the longest word in a given string article --- .../longestword/LongestWordFinder.java | 36 +++++++++++ .../LongestWordFinderUnitTest.java | 62 +++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 core-java-modules/core-java-string-operations-5/src/main/java/com/baeldung/longestword/LongestWordFinder.java create mode 100644 core-java-modules/core-java-string-operations-5/src/test/java/com/baeldung/longestword/LongestWordFinderUnitTest.java diff --git a/core-java-modules/core-java-string-operations-5/src/main/java/com/baeldung/longestword/LongestWordFinder.java b/core-java-modules/core-java-string-operations-5/src/main/java/com/baeldung/longestword/LongestWordFinder.java new file mode 100644 index 0000000000..770984ac29 --- /dev/null +++ b/core-java-modules/core-java-string-operations-5/src/main/java/com/baeldung/longestword/LongestWordFinder.java @@ -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 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 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()); + } + +} diff --git a/core-java-modules/core-java-string-operations-5/src/test/java/com/baeldung/longestword/LongestWordFinderUnitTest.java b/core-java-modules/core-java-string-operations-5/src/test/java/com/baeldung/longestword/LongestWordFinderUnitTest.java new file mode 100644 index 0000000000..9d5f82d493 --- /dev/null +++ b/core-java-modules/core-java-string-operations-5/src/test/java/com/baeldung/longestword/LongestWordFinderUnitTest.java @@ -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"); + } + +}