Reputation: 14699
For example, let's assume that the following data structures all implement the mapping of Lemmas->PartsOfSpeech->ListOfWords (topic choice is arbitrary and has nothing to do with question):
Map<ArrayList<Lemma>, Map<PartOfSpeech, ArrayList<Word>>> mainMap = new HashMap<>();
Map<ArrayList<Lemma>, PartOfSpeechWordMap> mainMap = new HashMap<>();
LemmaMap mainMap = new LemmaMap();
Where PartOfSpeechWordMap
contains and manipulates Map<PartOfSpeech, ArrayList<Word>>
, and LemmaMap
contains and manipulates one of the above structures.
The list goes from most transparent and complicated, to least transparent and simplest; however, I'm not sure which is best from an object oriented perspective. As you go down the list, it gets more and more difficult to figure out what's going on, despite being more concise and having simpler syntax.
I understand that this question may be a little subjective, but I'd like to know if there are standard industry practices when dealing with something like this.
EDIT:
I wasn't thinking and didn't consider the issue with mutable keys. Let's assume that I'm using ImmutableList
per Guava. Nonetheless, the point of my question was which structure makes most sense from an OOP perspective with regards to the varying levels of transparency.
Upvotes: 2
Views: 106
Reputation: 18148
I would create a
class LemmaMap implements Map<List<Lemma>, Map<PartOfSpeech, List<Word>>>
where LemmaMap
wraps a HashMap
, but also provides some additional methods e.g.
public List<Word> getAllWords(List<Lemma> key) {
List<Word> returnValue = new ArrayList<>();
for(List<Word> list : map.get(key).values()) {
returnValue.addAll(list);
}
return returnValue;
}
This provides transparency while also hiding the map from callers who e.g. only want the list of all words associated with a key without caring how to get it from the underlying map
Upvotes: 2
Reputation: 32949
I will say that this is not the appropriate site for this question but I believe most people would suggest using the simplest to use API. This gives the advantage to reducing the likelihood that it will be used improperly (such as adding elements to the key array). It also allows you to change the underlying implementation detail should the need arise.
Upvotes: 1