0

i would like to make some sort of a structure where i can have duplicate keys but the values differ. im using java and the hashmap is nice but doesnt allow for duplicate keys, i imported the google guava libraries for multimaps but cant seem to make it work any better ideas or suggestions ? i want to save multple objects then search for them using a key but some objects will have the same key.

looking for a java dictionary

this is an example of what i want to insert

    NP|DET|NOM|
    NP|PROPERNOUN|
    NOM|NOUN|NOUN|
    S|NP|VP|
    VP|VERB|NP|
    VP|VERB|PP|

the far left value being the key and the right terminal being the values

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Rhyno_H
  • 49
  • 1
  • 8

5 Answers5

2

I would recommend using HashMap and put some sort of List in it for example a

HashMap<String, ArrayList<TheKindOfStuffYouWantToSave>>
LionC
  • 3,106
  • 1
  • 22
  • 31
2

By definition a Map cannot contain non-unique keys. You need to store the values as a Collection (e.g. ArrayList).

private Map<String, Collection<String>> myDictionary = new HashMap<>();

public void addWord(String word, String definition) {
    Collection definitions = myDictionary.get(word);
    if(definitions == null) {
         definitions = new ArrayList<String>();
         myDictionary.put(word, definitions);
    }
    definitions.add(definition);
}
dtech
  • 13,741
  • 11
  • 48
  • 73
1

Java has no set or map structure to store duplicate keys.

If you want to store multiple values under one key, you should store Collection as value.

For example:

private Map<K, Collection<V>> map = new HashMap<K, Collection<V>> ();

public void multiPut(K k, V v) {
  Collection<V> c = map.get(k);
  if (c == null) {
    c = new ArrayList<V>();
    map.put(k, c);
   }
 c.add(v);
}
Danubian Sailor
  • 1
  • 38
  • 145
  • 223
1

You will loose the O(1) lookup time if you have to do this. Best thing you can do is, implement a binary tree to have this and have best lookup and insertion times if your keys are going to be duplicate. You can also extend Hashmap and override put function to handle collisions.

If you can use Apache Commons, please check following example of Multimap and MultiHashmap. org.apache.commons.collections.MultiHashMap

MultiMap mhm = new MultiHashMap();
mhm.put(key, "A");
mhm.put(key, "B");
mhm.put(key, "C");
Collection coll = (Collection) mhm.get(key);

Source: Multimap & Multihashmap documentation of Apache Commons

Note: Please check if there is already an existing question before posting it.

Buddha
  • 4,339
  • 2
  • 27
  • 51
  • I'd advise using Google Guava `MultiMap` (http://guava-libraries.googlecode.com/svn/tags/release03/javadoc/com/google/common/collect/Multimap.html) instead of Apache Commons. Guava is more common nowadays but more importantly, uses generics. – dtech May 17 '13 at 14:32
  • Thanks, I have never used Guava libraries, but I have been hearing this library many times... Looks like I have some learning to do. – Buddha May 17 '13 at 14:33
  • Guava is usually the better choice for new projects: http://stackoverflow.com/questions/4542550/what-are-the-big-improvements-between-guava-and-apache-equivalent-libraries and Google's own reasons for making a new library: https://code.google.com/p/google-collections/wiki/Faq – dtech May 17 '13 at 14:35
  • @dtech That is an excellent link.. Thanks. – Buddha May 17 '13 at 14:39
  • i have used guava but find it difficult to understand – Rhyno_H May 17 '13 at 15:01
  • @Rhyno_H if you can let us know what issue you ran into while using Guava to use multimap, we can help you solving that. – Buddha May 17 '13 at 15:32
0

I can think of a custom logic as mentioned below

Create a HashMap>. Here key is the word, value is the arraylist of meanings.

Write a method to add a dictionary entry in your code. Method should check if a key entry does not exist , initialize an arraylist, add the value to this list, and finally save this arraylist in hashmap against the new key. If key exists, fetch the arraylist and add an entry to it.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136