We have a framework for handling server notifications, where listeners are responsible for producing one or several Events that will be forwarded to the UI part. Although it happens that one single notification can trigger several Events, 95% of the listeners follow this pattern:
public Collection<Event> onNotification(...) { ArrayList<Event> events = new ArrayList<Event>(); if (...) { ... events.add(new Event(...)); } return events; }
The usual case is to have one or no event. By sizing the ArrayList to 1, you would have already a nice optimization, sicne the default size of the ArrayList is 10. But you can go one step further, and use the trivial Collection factory methods of the java.util.Collections class:
public Collection<Event> onNotification(...) { if (...) { ... return Collections.singletonList(new Event()); } return Collections.emptyList(); }
The emptyList() method does not create any object, since it returns a reference to an immutable constant list. The singletonList() will return an immutable lightweight wrapper around the one element.