test the methods with Unit test
This commit is contained in:
@@ -12,22 +12,7 @@ import java.util.stream.Stream;
|
||||
|
||||
public class MatchWords {
|
||||
|
||||
public static void main(String[] args) {
|
||||
String[] words = {"hello", "Baeldung"};
|
||||
String inputString = "hello there, Baeldung";
|
||||
|
||||
containsWords(inputString, words);
|
||||
|
||||
containsWordsJava8(inputString, words);
|
||||
|
||||
containsWordsPatternMatch(inputString, words);
|
||||
|
||||
containsWordsAhoCorasick(inputString, words);
|
||||
|
||||
containsWordsIndexOf(inputString, words);
|
||||
}
|
||||
|
||||
private static boolean containsWordsIndexOf(String inputString, String[] words) {
|
||||
public static boolean containsWordsIndexOf(String inputString, String[] words) {
|
||||
boolean found = true;
|
||||
for (String word : words) {
|
||||
int index = inputString.indexOf(word);
|
||||
@@ -39,7 +24,7 @@ public class MatchWords {
|
||||
return found;
|
||||
}
|
||||
|
||||
private static boolean containsWords(String inputString, String[] items) {
|
||||
public static boolean containsWords(String inputString, String[] items) {
|
||||
boolean found = true;
|
||||
for (String item : items) {
|
||||
if (!inputString.contains(item)) {
|
||||
@@ -50,7 +35,7 @@ public class MatchWords {
|
||||
return found;
|
||||
}
|
||||
|
||||
private static boolean containsWordsAhoCorasick(String inputString, String[] words) {
|
||||
public static boolean containsWordsAhoCorasick(String inputString, String[] words) {
|
||||
Trie trie = Trie.builder()
|
||||
.onlyWholeWords()
|
||||
.addKeywords(words)
|
||||
@@ -71,7 +56,7 @@ public class MatchWords {
|
||||
return found;
|
||||
}
|
||||
|
||||
private static boolean containsWordsPatternMatch(String inputString, String[] words) {
|
||||
public static boolean containsWordsPatternMatch(String inputString, String[] words) {
|
||||
|
||||
StringBuilder regexp = new StringBuilder();
|
||||
for (String word : words) {
|
||||
@@ -85,14 +70,14 @@ public class MatchWords {
|
||||
return false;
|
||||
}
|
||||
|
||||
private static boolean containsWordsJava8(String inputString, String[] words) {
|
||||
public static boolean containsWordsJava8(String inputString, String[] words) {
|
||||
ArrayList inputStringList = new ArrayList<>(Arrays.asList(inputString.split(" ")));
|
||||
ArrayList<String> wordsList = new ArrayList<>(Arrays.asList(words));
|
||||
|
||||
return wordsList.stream().allMatch(inputStringList::contains);
|
||||
}
|
||||
|
||||
private static boolean containsWordsArray(String inputString, String[] words) {
|
||||
public static boolean containsWordsArray(String inputString, String[] words) {
|
||||
ArrayList inputStringList = new ArrayList<>(Arrays.asList(inputString.split(" ")));
|
||||
ArrayList<String> wordsList = new ArrayList<>(Arrays.asList(words));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user