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

@@ -10,5 +10,4 @@
- [Guide to Splitting a String by Whitespace in Java](https://www.baeldung.com/java-splitting-a-string-by-whitespace)
- [Check if the First Letter of a String Is a Number](https://www.baeldung.com/java-check-if-string-starts-with-number)
- [Print “” Quotes Around a String in Java](https://www.baeldung.com/java-string-print-quotes)
- [Remove Punctuation From a String in Java](https://www.baeldung.com/java-remove-punctuation-from-string)
- [Find the Longest Word in a Given String in Java](https://www.baeldung.com/java-longest-word-string)
- [Remove Punctuation From a String in Java](https://www.baeldung.com/java-remove-punctuation-from-string)

View File

@@ -1,36 +0,0 @@
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

@@ -1,62 +0,0 @@
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");
}
}