Angelo
Angelo

Reputation: 937

Group multiple strings into a generic one in Java

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!

Upvotes: 0

Views: 1445

Answers (4)

Louis Wasserman
Louis Wasserman

Reputation: 198211

Build a Map mapping strings to their "category."

Map<String, String> category = new HashMap<String, String>();
category.put("hammer", "tool");
category.put("screwdriver", "tool");
category.put("horse", "animal");

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.

Upvotes: 1

alphazero
alphazero

Reputation: 27234

There are many ways to skin this cat (npi;) -- here is one approach. Annotations are another way to go.(e.g. Category is an annotation type with target type of field, etc.) Obviously the comparison mechanism isn't done below but that is trivial.

public enum Category {
    animal, tool
}
public interface Categorized {
    Category getCategory();
}
public enum FarmAnimals implements Categorized {
    dog, cat, horse, rabbit;
    public Category getCategory() {
        return Category.animal;
    }
}
public enum GarageTools implements Categorized {
    screwdriver, drill, wrench;
    public Category getCategory() {
        return Category.tool;
    }
}

[edit: naturally, your enums can be of form dog ("Dog"), etc. if you need embedded spaces, etc.]

Upvotes: 1

Matt Ball
Matt Ball

Reputation: 359956

I'd start something like this.

public class Category
{
    private final String name;
    private final Set<String> items;

    public Category(String name)
    {
        this.name = name;
        this.items = new HashSet<String>();
    }

    public String getName()
    {
        return name;
    }

    public void add(String... items)
    {
        for (String item : items)
        {
            this.items.add(item);
        }
    }

    public boolean contains(String item)
    {
        return this.items.contains(item);
    }
}

Then,

Category tools = new Category("tool");
tools.add("hammer", "screwdriver", "pliers");

Category animals = new Category("animal");
animals.add("horse", "dog", "cat");

And finally,

// Guava for brevity
List<Category> categories = Lists.newArrayList(tools, animals);

public void stringConverter(String str)
{
    for (Category c : categories)
    {
        if (c.contains(str)) return c.getName();
    }

    return "not found";
}

Upvotes: 6

JB Nizet
JB Nizet

Reputation: 691933

Use Map<String, String> where the keys of the map are your 6 strings, and the value is either "animal" or "tool". Use map.get(str) to get the type of the string.

Upvotes: 1

Related Questions