0

The problem:

Let's say I want to create a java app (currently using java 8) that works like a dictionary. I want the app to give the user the capacity to add and search for words, every word must contain a name, a meaning and zero or more examples. In addition, I want the user to be able to add more examples to a word that is already stored in the dictionary and to add multiple meanings to the same word, for this, the dictionary must detect when a user tries to add a duplicated word and instead of creating a new entry, it should add the new information for the already existing word. Finally, I need the app to store all of this information so it don't forget it whenever the user exits the application.

My approach:

Obviously, I'm not asking you to implement this app, so here's my idea. I know java has a Dictionary class, so I could create a Hashmap<String, Word> to manage all the dictionary entries, where the key would be the 'name' of the word, and Word would be an object containing the meanings and the examples as two lists of strings. That way I could easily search for a word, and add or retrieve information from the Word object. Also, this way, if the user tries to add a word, if the Hashmap already contains the word, it will not create a new Word object, but add the info to an existing one (kind of what you would do with a Flyweight).

Now, for storing the dictionary, given that my idea is to work with Word objects, I was thinking on using a JSON file to store the dictionary as an array of words.

The question:

Now, as I said, I'm not asking for an implementation of this app. What I want to know is if my approach is a good idea, and maybe discuss some alternative implementations. So, is a Hashmap a good structure to maintain a dictionary? Is it a good idea to use a Word object? Is using a JSON file a good way to store this kind of data?

Bonus: What if I want to add the words in alphabetic order?

  • 7
    In order for us to answer questions about "goodness," "suitability," or notions about being "proper," you need to define specifically what those terms mean to you within the context of your question, without resorting to tautologies like "best practice." – Robert Harvey Jul 21 '17 at 15:15
  • Hashmaps by definition can not store things in alphabetical order. It also seems like this would lead to some duplication for example by different tenses of the same word (ie. run, ran, running) which all have the same base meaning by are at different points in time. There is also the case of something like good, better and best which just denotes how extreme it is. I can't answer the question in detail because I am nowhere near an expert on the english language, but this design doesn't seem easily extensible – yitzih Jul 21 '17 at 15:27
  • 1
    @RobertHarvey Sorry, I'll try to be more specific. Basically what I want is efficiency (i.e. speed) in accessing the data. I'll modify the question to add more detail. – Roäc Jul 21 '17 at 15:28
  • Hashmaps are about as fast as you can get, unless you think the data set(s) will be non-trivially large or you need sorting capabilities. – Robert Harvey Jul 21 '17 at 15:31
  • 2
    According to Oxford Dictionaries, there are some ~200,000 words in English. Hardly big data territory, so your concern for efficiency may be premature. – Erik Eidt Jul 21 '17 at 15:31
3

Your design has multiple entities with rigid relationships between them. You have a set of words, each of which has a set of meanings, a set of related words (eg different tenses of verbs, plurals of nouns, etc), and so on. Meanings have definitions and citations. You may have cross references between entities. All of these things are well defined relations between entities of finite types with well known attributes that can be determined ahead of time. Furthermore, you do not have entities nesting inside themselves but instead have a flat, predefined hierarchy of statically nested types. You also want persistence, but your data is large enough that loading and saving it inits entirety could cause performance problems, so you need fast access to subsets of the data to work with.

All of these factors suggest using a relational database rather than a recursively defined structure like JSON, which is likely to be harder to work with and less performant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.