Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Tagging feature for a recipe book structure  RSS feed

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I'm currently making a recipe book on android.
I'd like to be able to tag recipes however not sure what would be the best way to do this.

I was thinking of a table that will contain tag objects.
We could have positions 0 to 25 be the first letter of a tag, or just a hash function of a string.
Each position would have an arraylist of tags.
Each tag object would have a arraylist of recipes.

So to add a recipe just search if the tag is there, if not create the tag and add recipe to the tag object.
Search by first letter of the tag (or hash function) to get to the right column of the table, look for the tag and display all the recipes that have the tag.

Is there a better way to do this?
Any advice would be appreciated!
Thanks.
 
Saloon Keeper
Posts: 8589
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Having an entire key just for the first letter of a tag, which again acts as a key to a bunch of keys seems overly complex to me. Besides, can't recipes have more than one tag?

This seems like a classic many-to-many relationship to me, including a way to search through tags. I would use a so-called radix tree for this. Besides the basic tree structure to hold the tags, you can associate each node with a set of recipes that is associated with the node's tag. You can also add a set of tags to each recipe, so you can perform the lookup both ways.

Apache commons has a class that does this. That means all you need is a PatriciaTrie<String, Set<Recipe>> and you're done. For instance, if you want all recipes containing a tag that starts with "strawb", you can just perform recipesByTag.prefixMap("strawb").
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!