public List<String> getCountries() { if (/* the condition that returns a list having some elements */) { List<String> listCountries = Arrays.asList("USA", "UK"); return listCountries; } else { // the condition that returns an empty list return Collections.emptyList(); } }And here’s the calling code:
List<String> listCountries = getCountries(); if (listCountries == Collections.EMPTY_LIST) { System.out.println("Empty list: no country"); }The above reference comparison (using == operator) proves that the factory method returns a singleton instance of an empty list. You can also write like this for more readability:
if (listCountries.isEmpty()) { System.out.println("Empty list: no country"); }
public Map<Integer, String> getErrors() { if (false) { // the condition that returns a map having some mappings Map<Integer, String> mapErrors = new HashMap<>(); mapErrors.put(500, "Internal Server Error"); return mapErrors; } else { // the condition that returns an empty map return Collections.emptyMap(); } }And the client code:
Map<Integer, String> mapErrors = getErrors(); if (mapErrors == Collections.EMPTY_MAP) { System.out.println("Empty map: No error found"); }
public Set<Integer> getNumbers() { if (false) { // the condition that returns a set having some mappings Set<Integer> setNumbers = new HashSet<>(); setNumbers.add(123); setNumbers.add(987); return setNumbers; } else { // the condition that returns an empty set return Collections.emptySet(); } }and the calling code:
Set<Integer> setNumbers = getNumbers(); if (setNumbers == Collections.EMPTY_SET) { System.out.println("Emtpy set: no numbers"); }And the following example illustrates passing an empty collection to a method:
public void processList(List<String> list) { // processes the input list... }with the calling code:
processList(Collections.emptyList());
public void processSet(Set<Integer> set) { // processes the input set... }then here how the client code looks like:
Set<Integer> setInput = Collections.singleton(new Integer(200)); processSet(setInput);
public void processList(List<String> list) { // processes the input list... }so the calling code:
List<String> listInput = Collections.singletonList("OK"); processList(listInput);
public void processMap(Map<Integer, String> map) { // processes the input map... }Then the following code passes a singleton set to the method:
Map<Integer, String> mapInput = Collections.singletonMap(200, "OK"); processMap(mapInput);The singleton collection can be also useful when we need to remove all occurrences of a specified element from a collection, as shown in the example below:
List<String> listNames = new ArrayList<>( Arrays.asList("Joe", "Dan", "Carl", "Jack", "Tom", "Dan")); System.out.println("Before remove: " + listNames); listNames.removeAll(Collections.singletonList("Dan")); System.out.println("After remove: " + listNames);Output:
Before remove: [Joe, Dan, Carl, Jack, Tom, Dan] After remove: [Joe, Carl, Jack, Tom]
List<T> nCopies(int n, T o)
This method has two main uses, as explained in the following examples:List<String> list = new ArrayList<>(Collections.nCopies(1000, (String)null));
list.addAll(Collections.nCopies(100, (String)null));