• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Tagging feature for a recipe book structure  RSS feed

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

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!
Posts: 9494
  • 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").
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!