You can simply pass a list of objects β you donβt even need if / else, since you can always call "toString ()" or "String.valueOf" in the message object:
public void addMessages(List<Object> messages) { if (!CollectionUtils.isEmpty(messages)) { for (Object message : messages) { this.messages.add(String.valueOf(message)); } } }
On the other hand: potential problems can arise due to the presence of zero elements in the message list, so you can check this in your loop. Other potential problems:
- this.messages is not initialized and adding messages raises a
NullPointerException
- if it is a single point method (e.g. spring service) that should be avoided
source share