public static <KeyType,ValueType> void update(Map<KeyType,List<ValueType>> m,keyType key,ValueType value)
{
List<ValueType> lst = m.get(key);
if(lst==null){
lst - new ArrayList<>();
m.put(key,value);
}
lst.add(value);
}
应用1:对不同长度的字符串进行键值映射。
public static Map<Integer,List<String>> wordsClassfied(List<String> lst)
{
Map<Integer,List<String>> wordsByLength = new TreeMap<>();
for(String s : lst)
update(wordsByLength,s.length,s);
}