What is the use of Collections.singletonList()
in Java? I understand that it returns a list with one element. Why would I want to have a separate method to do that?
singletonList can hold instance of any object. Object state can be modify.
List list = new ArrayList();
list.add('X');
list.add('Y');
System.out.println("Initial list: "+ list);
List> list2 = Collections.singletonList(list);
list.add('Z');
System.out.println(list);
System.out.println(list2);
We can not define unmodifiableList like above.