I’ve got to group multiple strings into one to make a comparison. This grouping should be done exactly like the Java OO paradigm: a string that “describes” the substring.
Example:
String one = "hammer";
String two = "screwdriver";
String three = "pliers";
Now let’s say you want to “describe” them:
String str = "tool"
All strings above are tools. Now, my code have string one, two, three and change them into “tool”. So, for example, string one become tool, the same for string two and string three.
How to “categorize” them?
Another extended example:
String one = "hammer"
String two = "screwdriver";
String three = "pliers";
String four = "horse";
String five = "cat";
String six = "dog";
public void stringConverter(String str)
{
if ("string match to an animal")
str = "animal";
if ("string match to a tool")
str = "tool";
}
Maybe it’s a stupid thing to implement but right now I don’t have any ideas! Thank you!
edit: my group is limited, I know that i have only cat, dog, horse, hammer, etc…
edit2: It’s difficult to express me! It should be something like:
Group Animal = {cat, dog, horse}
Group Tools = {hammer, screwdriver}
// methods to recognize to wich one of the two groups is categorizable
The Map is a good idea but it has to be filled at every runtime. Isn’t there something static like writing them directly into braces? It should be something like enums but never used before!
Build a
Mapmapping strings to their “category.”and then you just use
category.get(str)to get the category.If they’re static, you’re probably best served by a Guava ImmutableMap, possibly using its builder syntax.