Reputation: 18614
Consider the following TreeSet:
This set has 2 main words blue and red along with different key words.
I need to group by those main words so that I get a list with all possible key words. Something like:
I think the steps should be as follows:
Could somebody please give me a suggestion how to accomplish it and what I need for that?
Upvotes: 1
Views: 58
Reputation: 18303
I don't think you need regex. Split each string on whitespace using String.split(" ")
, and then examine the first item to compare it your list of "main" words.
TreeSet<String> originalSet = // as per question
List<String> mainWords = Arrays.asList("blue", "red");
Map<String, Set<String>> words = new HashMap<>();
for(String mainWord : mainWords) {
words.put(mainWord, new HashSet<String>());
}
for(String line : originalSet) {
String[] items = line.split(" ");
if(words.keySet().contains(items[0])) {
for(int i = 1 ; i < items.length ; i++) {
words.get(items[0]).add(items[i]);
}
}
}
Upvotes: 1