HashMap with ArrayList

I have a java question about a HashMap with an ArrayList in it. The key is a string, and the value is ArrayList ( HashMap<String, ArrayList<Object>>)

I am given a list of several keys, and I need to put the corresponding values ​​in an ArrayList, and I'm not sure how to do this.

Here is an example:

(What is given to me)

ABC
ABC
ABC
123
123

Keys are actually file names, so the file is a value for ArrayList. Now I have to separate these file names in order to add files to the ArrayList. It makes sense?

This is the only code I have:

Set<String> tempModels = Utils.getSettingSet("module_names", new HashSet<String>(), getActivity());
        for(String s : tempModels) {
            Log.d("Loading from shared preferences: ", s);
            String key = s.substring(0, 17);

        }
+4
source share
2 answers

I believe that you are looking for something like this,

List<Object> al = map.get(key);
if (al == null) {
  al = new ArrayList<Object>();
  map.put(key, al);
}
al.add(value);

, raw Object Lists. ( ) .

Edit

( ) ,

if (!map.containsKey(key)) {
  map.put(key, new ArrayList<Object>());
}
map.get(key).add(value);
+4

Source: https://habr.com/ru/post/1544530/


All Articles