-1

I need something like this:

SomeMap someMap= new someArray;
someArray.put("color","red");
someArray.put("color","black");
someArray.put("brand","coca-cola")
someArray.put("brand","pepsi")

I don't care what the order is. These are the methods I need:

Put key and value:

someArray.put("color","red");

Get by key:

someArray.get("color");

Get all:

someArray.getAll();

Remove all:

someArray.removeAll();

And I also need to be able to copy the structure:

someMap2 = someMap; (not shallow copy)

How can I do it?

Boann
  • 48,794
  • 16
  • 117
  • 146
Yoni
  • 1,346
  • 3
  • 16
  • 38

2 Answers2

4

The part about allowing 'duplicate keys' in your question has me thinking what you really want is a MultiMap, where a given key can map to more than one value. Google has this in their Guava library - or you can roll your own with a Map whose values are a List implementation. Here's an example.

The declaration could be something like:

 Map<String, List<String>> map = new HashMap<String, List<String>>()
Amir Afghani
  • 37,814
  • 16
  • 84
  • 124
  • I tried to work with Guava but is too big library so i tried to find something else – Yoni Jul 29 '17 at 17:08
  • OK, so roll your own, as I suggest in the second part of my answer. I'll update to include an example. – Amir Afghani Jul 29 '17 at 17:09
  • i tried to do it but i have a problem when i need to copy this hashmap something like that: Map> map = new HashMap>() Map> mapCopy = new HashMap>() map = copyMap; and any change i do in map change also the copyMap how i can copy without to change him? – Yoni Jul 29 '17 at 17:14
  • Huh? What makes you think that declaring a new variable called mapCopy and initializing it is enough to copy the contents of the old map?? – Amir Afghani Jul 29 '17 at 17:15
  • You need to write a method to copy the contents. Why don't you wrap this thing in a class and write a copy constructor? – Amir Afghani Jul 29 '17 at 17:17
  • Have you maybe an example for me? – Yoni Jul 29 '17 at 17:18
  • Here's an example of a copy constructor: http://www.javapractices.com/topic/TopicAction.do?Id=12 Make an attempt and tell us where you get stuck. I'm not going to put the two solutions together and solve your problem. – Amir Afghani Jul 29 '17 at 17:19
  • 1
    The part about Guava's library being too big sounds disingenuous to be honest. Too big for what? – Amir Afghani Jul 29 '17 at 17:20
  • public void copyHashMap(Map> map) { Map> newMap = new HashMap>(); newMap.putAll(map); } – Yoni Jul 29 '17 at 17:20
  • when i put it on my project is required multiDex(65k) – Yoni Jul 29 '17 at 17:21
  • i think Map> is better than Map>,no? – Yoni Jul 29 '17 at 17:23
  • OK - that's a fair point. Now my suggestion is to read the Galaxy example I gave you, and write a copy constructor that loops through the entrys of the old map and deep copies them to the new map. If the contents are just Strings, this should be fairly easy to do. – Amir Afghani Jul 29 '17 at 17:24
  • I do not find in this link something about deep copy – Yoni Jul 29 '17 at 17:28
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/150487/discussion-between-yoni-and-amir-afghani). – Yoni Jul 29 '17 at 17:34
1

If you want to have this without any external dependencies then the following code is all you need.

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MultiValueMap {

    private static final Map<String, List<String>> map = new HashMap<String, List<String>>();

    public static void main(String[] args) {
        put("color", "red");
        put("color", "black");
        put("brand", "coca-cola");
        put("brand", "pepsi");

        System.out.println(get("color"));
        System.out.println(get("brand"));
    }

    public static List<String> get(String key) {
        return map.get(key);
    }

    public static void put(String key, String value) {
        if (map.get(key) == null) {
            map.put(key, new ArrayList<>());
        }
        map.get(key).add(value);
    }

}
karthikdivi
  • 3,466
  • 5
  • 27
  • 46