1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.launcher3.util;
18 
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 
22 /**
23  * A utility map from keys to an ArrayList of values.
24  */
25 public class MultiHashMap<K, V> extends HashMap<K, ArrayList<V>> {
26 
MultiHashMap()27     public MultiHashMap() { }
28 
MultiHashMap(int size)29     public MultiHashMap(int size) {
30         super(size);
31     }
32 
addToList(K key, V value)33     public void addToList(K key, V value) {
34         ArrayList<V> list = get(key);
35         if (list == null) {
36             list = new ArrayList<>();
37             list.add(value);
38             put(key, list);
39         } else {
40             list.add(value);
41         }
42     }
43 
44     @Override
clone()45     public MultiHashMap<K, V> clone() {
46         MultiHashMap<K, V> map = new MultiHashMap<>(size());
47         for (Entry<K, ArrayList<V>> entry : entrySet()) {
48             map.put(entry.getKey(), new ArrayList<V>(entry.getValue()));
49         }
50         return map;
51     }
52 }
53