Can someone please provide an example of creating a Java ArrayList
and HashMap
on the fly? So instead of doing an add()
or put()
You mean like this?
public List buildList(String first, String second)
{
List ret = new ArrayList();
ret.add(first);
ret.add(second);
return ret;
}
...
List names = buildList("Jon", "Marc");
Or are you interested in the ArrayList
constructor which takes a Collection extends E>
? For example:
String[] items = new String[] { "First", "Second", "Third" };
// Here's one way of creating a List...
Collection itemCollection = Arrays.asList(items);
// And here's another
ArrayList itemList = new ArrayList(itemCollection);