I need to add object to new ArrayList:
private Map<String,List<MyObject>> populateMapFromList2(List<MyObject> dataSourceList){
Map<String,List<MyObject>> moMap = new HashMap<String, List<MyObject>>();
for (MyObject mo : dataSourceList){
String firstLetter = (mo.getName().substring(0,1)).toUpperCase();
if (!moMap.containsKey(firstLetter)) {
moMap.put(firstLetter,new ArrayList<MyObject>());
}
moMap.get(firstLetter).add(mo);
}
return moMap;
}
in the line:new ArrayList<MyObject>()
I want to add mo
Like: new ArrayList<DataSource>().add(mo)
but it is a boolean value (Go figure).
is there another way to add value for the first time to not parametrized list?
or the only way is this?
List<MyObject> initList = new ArrayList<MyObject>();
initList.add(mo);
moMap.put(firstLetter,initList);
Thanks,