mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-26 20:54:07 -08:00
Removed spaces from top-level directory names.
Spaces tend to cause annoyances in a Unix-style shell environment. This change fixes that.
This commit is contained in:
46
85_Synonym/java/src/SynonymList.java
Normal file
46
85_Synonym/java/src/SynonymList.java
Normal file
@@ -0,0 +1,46 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Stores a word and a list of synonyms for that word
|
||||
*/
|
||||
public class SynonymList {
|
||||
|
||||
private final String word;
|
||||
|
||||
private final ArrayList<String> synonyms;
|
||||
|
||||
public SynonymList(String word, String[] synonyms) {
|
||||
this.word = word;
|
||||
this.synonyms = new ArrayList<>(Arrays.asList(synonyms));
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the word passed to this method exists in the list of synonyms
|
||||
* N.B. Case insensitive
|
||||
*
|
||||
* @param word word to search for
|
||||
* @return true if found, otherwise false
|
||||
*/
|
||||
public boolean exists(String word) {
|
||||
return synonyms.stream().anyMatch(str -> str.equalsIgnoreCase(word));
|
||||
}
|
||||
|
||||
public String getWord() {
|
||||
return word;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return synonyms.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all synonyms for this word in string array format
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String[] getSynonyms() {
|
||||
// Parameter to toArray method determines type of the resultant array
|
||||
return synonyms.toArray(new String[0]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user