Q: Dynamic class creation for string grouping Here is a sketch of the problem I want to solve:


*

*I need to group several strings under another one, so that there would be a way to include new strings into this "group"

*It has to be dynamic: I read those strings from a file and create the groups on the fly


Example:
Input file:
ANIMAL: DOG CAT BIRD
PERSON: CLAIRE DEMETRIOS NEIL
ANIMAL: CROCODILE BUTTERFLY

I would need to create a "group" (don't know if a class or a structure would be better) called ANIMAL (on the fly by the code) and include the five animals in there, taking into account we are dealing with text strings. 
I am looking for a way to classify those strings and I wonder what the simplest and most efficient way would be: classes? structures? superstrings?
Thank you in advance

A: You can define a Map with String key and List value like
HashMap<String, List<String>> hashmap = new HashMap<String, List<String>>();

List<String> arraylist = new ArrayList<String>();
arraylist.add("Hello");
arraylist.add("World.");

//Put list to a group
hashmap.put("mykey", arraylist);

//Dynamically add element to a group
hashmap.get("mykey").add("Everyone");

//Retrieve all element of a group
List<String> list = hashmap.get("mykey");
System.out.println(list);

This will output:
[Hello, World., Everyone]

Update:
According to Carlos's comment, you can use a simple if-else block to dynamically create list. For example-
if (hashmap.containsKey("person")) {
  hashmap.get("person").add("Emdadul");
} else {
  List<String> lst = new ArrayList<String>();
  lst.add("Emdadul");
  hashmap.put("person", lst);
}

I hope it will solve your problem. Rest of logic should be implemented by you.
