Here you can find the source of addMappedUnmodifiableListEntry(Map
Parameter | Description |
---|---|
K | key of entry |
V | value to be put in the list |
map | contains key, List<values> |
key | a parameter |
value | a parameter |
public static <K, V> List<V> addMappedUnmodifiableListEntry(Map<K, List<V>> map, K key, V value)
//package com.java2s; //License from project: Apache License import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Map; public class Main { /**//from ww w. j av a 2 s . c om * When you have a map containing values that are array lists of a specific * type... use this method to optionally create the list if it's not there * and add the item. * * @param <K> * key of entry * @param <V> * value to be put in the list * @param map * contains key, List<values> * @param key * @param value */ public static <K, V> List<V> addMappedUnmodifiableListEntry(Map<K, List<V>> map, K key, V value) { List<V> list = map.get(key); if (list == null) list = new ArrayList<V>(); else list = new ArrayList<V>(list); list.add(value); list = Collections.unmodifiableList(list); map.put(key, list); return list; } /** * This method provides a way to obtain an object from a collection, given * an equal object (logical equals). In the collection and set interfaces * (even list) the assumption is that if you have an equal object why would * you need the one out of the collection. Because logically equal objects * can be physically different objects we provide this method to return out * of a collection the actual object in the collection ( for partial object * checking and untrusted object arguements that reference an existing item * in a collection ). * * @param p_oCol * @param p_oObj * @return */ public static <T> T get(Collection<T> p_oCol, T p_oObj) { if (p_oObj == null) return null; if (isEmpty(p_oCol)) return null; if (p_oCol instanceof List) return get(p_oCol, ((List<T>) p_oCol).indexOf(p_oObj)); T oObj = null; T oTempObj = null; for (Iterator<T> oIter = p_oCol.iterator(); oIter.hasNext();) { oTempObj = oIter.next(); if (p_oObj.equals(oTempObj)) { oObj = oTempObj; break; } } return oObj; } /** * Mostly provided for sets, Please keep in mind sets don't guarantee any * specific order. And it could be that for any one instance of a set this * method may return 2 different objects for the same index (dependent on * the set implementation) This method is useful for getting a single item * out of a collection when you don't know what type of collection you are * dealing with. * * @param p_oCol * @param p_iIndex * @return */ public static <T> T get(Collection<T> p_oCol, int p_iIndex) { if (isEmpty(p_oCol)) return null; if (p_iIndex < 0 || p_iIndex >= p_oCol.size()) return null; if (p_oCol instanceof List) return ((List<T>) p_oCol).get(p_iIndex); T oObj = null; T oTempObj = null; int i = 0; for (Iterator<T> oIter = p_oCol.iterator(); oIter.hasNext();) { oTempObj = oIter.next(); if (i++ == p_iIndex) { oObj = oTempObj; break; } } return oObj; } public static boolean isEmpty(Iterable<?> i) { if (i instanceof Collection) return ((Collection<?>) i).isEmpty(); return i == null || !i.iterator().hasNext(); } public static boolean isEmpty(Map<?, ?> p_oCol) { return p_oCol == null || p_oCol.isEmpty(); } public static int size(Collection<?> col) { return col == null ? 0 : col.size(); } }