2

Currently I am using LinkedHashMap which maintain the insertion order.

Syntax for LinkedHashMap which I used :

private LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap<String, String>();
linkedHashMap.put("R1", "Data1");   
linkedHashMap.put("R2", "Data2");
linkedHashMap.put("R3", "Data3");
linkedHashMap.put("R4", "Data4");

Which is working fine. But I have where I have to used duplicate key and also I have maintain the insertion data.

For example :

private LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap<String, String>();
linkedHashMap.put("R1", "Data1");   
linkedHashMap.put("R2", "Data2");
linkedHashMap.put("R1", "Data3");
linkedHashMap.put("R2", "Data4");

When I am trying to keep duplicate data in linkedHaphMap then its removed duplicate data and keep only one data.

So How can I insert duplicate data and maintain the insertion order in java? What is the way to achieve my case?

Shiladittya Chakraborty
  • 4,270
  • 8
  • 45
  • 94
  • You should have a look at: http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/ListMultimap.html – ParkerHalo Feb 01 '16 at 11:15
  • What would `linkedHashMap.get("R2")` give you? When there are two entries with that key, and also when there's only one? – T.J. Crowder Feb 01 '16 at 11:16
  • 1
    A Map isn't designed for multiple copies of the same key, you should design your own class or use a `List>` – Ferrybig Feb 01 '16 at 11:16
  • 1
    Please check below post. http://stackoverflow.com/questions/18922165/how-to-include-duplicate-keys-in-hashmap – Prabu Subra Feb 01 '16 at 11:19
  • What is meant by `insertion order`? Does the modified value inserted become the most recent? – Marichyasana Feb 01 '16 at 11:32

3 Answers3

5

Try this:

public class Test {

    public static void main(String[] args) {
        Map<String, List<String>> map = new LinkedHashMap<>();
        put(map, "R1", "Data1");
        put(map, "R2", "Data2");
        put(map, "R1", "Data3");
        put(map, "R2", "Data4");

        System.out.println(map); // prints {R1=[Data1, Data3], R2=[Data2, Data4]}
    }

    public static void put(Map<String, List<String>> map, String key, String value) {
        if (map.get(key) == null) {
            List<String> list = new ArrayList<>();
            list.add(value);
            map.put(key, list);
        } else {
            map.get(key).add(value);
        }
    }
}
SeniorJD
  • 6,946
  • 4
  • 36
  • 53
0

Use composite pattern for your data. Simple example could look like this.

public class Container{
    String value;
    ArrayList<String> list = null;

    public Container(String value){
        this.value = value;
    }

    public void add(String newvalue){
         if (list == null){
             list = new ArrayList();
             list.add(value);
             list.add(newvalue);
         }else{
             list.add(newvalue);
         }
    }
    public boolean isSingleValue(){
         return list==null;
    }
    public Iterator<String> getOrderedList(){
          return list.iterator();
    }
    public String getValue(){
          return value;
    }

This way your map can hold single elements as well as a list of elements. You could use is like this:

if (map.containsKey(key)){
   map.get(key).add(value);
}else{
   map.put(key,new Container(value))
}
Radu Ionescu
  • 3,462
  • 5
  • 24
  • 43
0
private LinkedHashMap<String, List<String>> linkedHashMap = new LinkedHashMap<String, List<String>>();

put("R1", "Data1");   
put("R2", "Data2");
put("R1", "Data3");
put("R2", "Data4");

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

private boolean isDuplicate(String key){
    return (linkedHashMap.get(key).size()>1)
}