I need to create a method public static TreeSet<String> getSet(String s) in Java that takes an inputted string and does the following:
- Converts to lower case
- Remove Punctuation marks
- Returns a TreeSet of the words
I know that I can use replaceAll("[^a-zA-Z ]", "").toLowerCase().split("\\s "); to do everything besides add the words to a TreeSet and return that list. How can I combine these two snippet into a function that will return a TreeSet rather than an array or string?
CodePudding user response:
Just push the array elements into a set
public static Set<String> getSet(String s) {
String[] words = s.replaceAll("[^a-zA-Z ]", "").toLowerCase().split("\\s ");
return new TreeSet<>(Arrays.asList(words));
}
