0
LinkedHashMap<String, List<Object>>  = ???

linkedHashMap.put("a", list1);
linkedHashMap.put("e, list2);
linkedHashMap.put("i", list3);
linkedHashMap.put("o", list4);
linkedHashMap.put("u", list5);
linkedHashMap.put("g", list6);
linkedHashMap.put("a", list7);

I want to receive exactly in same order and I can have duplicate keys.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Nain
  • 97
  • 3
  • 11
  • 1
    Maps can never have duplicate keys – OneCricketeer May 08 '18 at 00:40
  • `MultiMap` in favor of reuse. http://www.baeldung.com/guava-multimap – jmj May 08 '18 at 00:44
  • https://stackoverflow.com/questions/5501468/having-a-multimap-sorted-on-keys-only-in-java/24674964 – displayName May 08 '18 at 00:45
  • classic multimap is a map where value is a collection of value, in your case you may look for LinkedHashMap>> – Adrian May 08 '18 at 00:50
  • I'm sorry, I could have told this before but I'm not using any Guava. Is it third party Jar? – Nain May 08 '18 at 01:02
  • @cricket_007 you are right. But my scenario is different even i have duplicates I want my map in same order. – Nain May 08 '18 at 01:03
  • Again, Maps simply cannot have duplicate keys. Explain how you would get data **out** of this, not just putting data in it... For example, what are you expecting as output? – OneCricketeer May 08 '18 at 01:09
  • Just out of curiousity i wanted to know what are you expecting as output? LinkedHashMap preserves the order of insertion but doesn't allow duplicate key. Do you mean even if we allow duplicates then the list of objects held by them should be different? Will storing the objects in the list for a given key i.e as per your example if you get "a" as a key can't u update the list1? – Venkatesh Manohar May 08 '18 at 01:13
  • @cricket_007 Right now my out put is: {a=[list1, list7], e=[list2], i=[list3],o=[list4],u=[list5],g=[list6]}. – Nain May 08 '18 at 01:15
  • @cricket_007 So its printing as: a,a,e,i,o,u,g but i'm expecting as a,e,i,o,u,g,a – Nain May 08 '18 at 01:17
  • What is printing? There is no print code in the question. Please [edit] your question to include a [mcve] – OneCricketeer May 08 '18 at 01:19
  • 1
    What is the actual question here? Which class to use? If you want duplicate keys you can't use `LinkedHashMap`, unless you make it a map of lists. – user207421 May 08 '18 at 01:45
  • @cricket_007 sorry for confusing you. when i say printing, I mean output. – Nain May 08 '18 at 02:34
  • @Nain guava is 3rd library lib: https://github.com/google/guava, it does not make sense to reinvent the wheel, LinkedListMultimap give you all that you have asked – Adrian May 08 '18 at 02:37
  • I know what output and print mean... It is unclear what part of the code provided is resulting in that output. Please edit your question to show us what you actually want and the code you've tried to get to that result. – OneCricketeer May 08 '18 at 04:57

1 Answers1

0

In your case, if you were to preserve exactly the same order for entries, you can use LinkedListMultimap entries() method:, see also how it differs from get method.

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.google.common.collect.LinkedListMultimap;
import com.google.common.collect.Multimap;;

public class Main {

  public static void main(String[] args) {

    List<Object> list1 = Arrays.asList(1);
    List<Object> list2 = Arrays.asList(2);
    List<Object> list3 = Arrays.asList(3);
    List<Object> list4 = Arrays.asList(4);
    List<Object> list5 = Arrays.asList(5);
    List<Object> list6 = Arrays.asList(6);
    List<Object> list7 = Arrays.asList(7);

    Multimap<String, List<Object>> mmap = LinkedListMultimap.create();
    mmap.put("a", list1);
    mmap.put("e", list2);
    mmap.put("i", list3);
    mmap.put("o", list4);
    mmap.put("u", list5);
    mmap.put("g", list6);
    mmap.put("a", list7);


    for (Map.Entry<String, List<Object>> entry : mmap.entries()) {
      System.out.println(entry.getKey() + " " + entry.getValue());
    }

    System.out.println("Multimap key grouped  values");

    for (String key : mmap.keys()) {
      System.out.println(key+ " " + mmap.get(key));
    }

   }
}
Adrian
  • 1,973
  • 1
  • 15
  • 28
  • do I need GUAVA library to implement LinkedListMultimap ? – Nain May 08 '18 at 02:41
  • 1
    you would need GUAVA library to use com.google.common.collect.LinkedListMultimap and com.google.common.collect.Multimap, updated import for provided solution – Adrian May 08 '18 at 03:42
  • Thanku @Witas, but I don't want to use GUAVA library. Can we do without using GUAVA library? – Nain May 08 '18 at 12:19
  • in that case you would write simple multimap implementation, http://www.techiedelight.com/implement-multimap-java/ – Adrian May 08 '18 at 13:41