Java Map Add addToList(Map> map, X key, Y value)

Here you can find the source of addToList(Map> map, X key, Y value)

Description

add To List

License

Apache License

Return

The resulting list.

Declaration

public static <X, Y> List<Y> addToList(Map<X, List<Y>> map, X key, Y value) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.ArrayList;

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

public class Main {
    /**/*from  w  w w  .  j  ava 2  s.  co  m*/
     * @return The resulting list.
     */
    public static <X, Y> List<Y> addToList(Map<X, List<Y>> map, X key, Y value) {
        List<Y> values;
        if (map.containsKey(key)) {
            values = map.get(key);
            values.add(value);
        } else {
            values = new ArrayList<Y>();
            values.add(value);
            map.put(key, values);
        }
        return values;
    }
}

Related

  1. addOne(Map map, K key)
  2. addOne(Map map, T key)
  3. addTo(Map> map, K key, T value)
  4. addTo(Map map, N key, E element, Class collectionClass)
  5. addToAnd(T target, Map... items)
  6. addToListMap(Map map, Object key, Object value)
  7. addToListMap(Map> map, K key, V value)
  8. addToListMap(Map> map, String key, String value)
  9. addToListMap(Map> map, T key, T value)