Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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,

share|improve this question

2 Answers

If using google-guava is an option, you can do Lists.newArrayList(mo).

The documentation is available on the google-guava API page for Lists.

EDIT Non google-guava option:

If google-guava is not an option, you can implement it yourself like this:

public static <T> List<T> newArrayList(T... a) {
    return new ArrayList<T>(Arrays.asList(a));
}     

This uses a combination of varargs and generics to create the array list. Varargs allows you to specify a variable number of parameters to this function. The generic type allows the type of the parameters to be captured and a list of corresponding type to be returned.

share|improve this answer
Thanks, But no it is not an option. – Netanel Jan 23 '12 at 12:07
I have added a non-guava option in the answer. – laixer Jan 23 '12 at 12:12

You can still use the following syntax:

List<MyObject> initList = new ArrayList<MyObject>() {{ add(mo); }}

Or using Google Guava, you can use Lists.newArrayList(mo).

You should consider using a MultiMap for what you are trying to achieve (a Map whose values are Lists). Note that Apache Common has a MultiMap implementation too, you can also take a look at the DefaultedMap class.

share|improve this answer
+1 for MultiMap – nd. Jan 23 '12 at 12:11
I think this is an inner class? I still have to parametrize "initList" and it is still another line. – Netanel Jan 23 '12 at 12:15
No this is an instance initialization. You should consider using a MultiMap, or create your own implementation of a DefaultedMap that will return you an empty List when calling get() on a non existing key. Now if you really want a one liner, make all your variables final, and use it: moMap.put(firstLetter, moMap.containsKey(firstLetter) ? new ArrayList<Object>() {{ addAll(moMap.get(firstLetter)); add(mo); }} : new ArrayList<Object>() {{ add(mo); }}); – Alex Jan 23 '12 at 12:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.