Can we add list inside list in Java?
1. Java List add()This method is used to add elements to the list. There are two methods to add elements to the list. Show
Lets look at some examples of List add() methods. Recommended Readings:
2. Java List addAll()This method is used to add the elements from a collection to the list. There are two overloaded addAll() methods.
Lets look at a simple example of List addAll() methods. 3. UnsupportedOperationException with List add() MethodsIf you look at the List add() and addAll() method documentation, the operation is mentioned as optional. It means that the list implementation may not support it. In this case, the list add() methods throw UnsupportedOperationException. There are two common scenarios where you will find this exception when adding elements to the list.
Lets look at a simple example of UnsupportedOperationException with add operation of both these types of lists. jshell> ListJava List add() UnsupportedOperationException References
|