LinkedHashMap<Integer, ArrayList> LHM = new LinkedHashMap<Integer, ArrayList>(); 

How to add elements to ArrayList ?

 LHM.put(1, new ArrayList().add("1")); 
  • add your solution to the question - Mikhail Vaysman
  • @mikhailvaysman Added a line which I am trying to add an element. - aaa
  • you have only a formal description of the problem in question - such questions are usually closed. add your solution or reformulate the question. - Mikhail Vaysman

2 answers 2

You can add an item as follows.

 Map<Integer, List<String>> lhm = new LinkedHashMap<>(); List<String> list = lhm.getOrDefault(1, new ArrayList<>()); list.add("1"); lhm.put(1, list); 
  • You scoffed)) - aaa
  • This site is a site for answering questions. I just wanted you to ask a clear question. then the answer will be useful not only to you, but also to other people. - Mikhail Vaysman
  • So I think I asked. There are lhm with certain types and the question is to add elements to this lhm. You can suggest an edit. - aaa
  • Initially your question sounded different, but you fixed it - thanks for that. - Mikhail Vaysman
  • I did not understand what I wrote) - aaa

You can do this:

 Map<String, List<String>> map = new LinkedHashMap<>(); map .computeIfAbsent(key, k -> new ArrayList<>()) .add("value"); 

This option allows you to create an object only if it is really required.