List add() Method in Java with Examples
Java List add()
This method is used to add elements to the list. There are two methods to add elements to the list.
add(E e): appends the element at the end of the list. Since List supports Generics, the type of elements that can be added is determined when the list is created.
add(int index, E element): inserts the element at the given index. The elements from the given index is shifted towards right of the list. The method throws IndexOutOfBoundsException if the given index is out of range.
package com.javacodestuffs.core.list;
import java.util.ArrayList;
import java.util.List;
public class ListAddExample1 {
public static void main(String[] args) {
List < String > langList = new ArrayList < >();
langList.add("Java");
langList.add("Php");
langList.add(".Net");
langList.add("Node");
langList.add("Python");
langList.add("Angular");
langList.add("Go");
langList.add("Scala");
langList.add("Groovy");
System.out.println("List of programming languages is : ");
System.out.println(langList);
}
}
output:
List of programming languages is :
[Java, Php, .Net, Node, Python, Angular, Go, Scala, Groovy]
Java List addAll()
This method is used to add the elements from a collection to the list.
1). boolean addAll(Collection c)
Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator (optional operation). The behavior of this operation is undefined if the specified collection is modified while the operation is in progress.
It will throw the following exceptions.
- UnsupportedOperationException - if the addAll operation is not supported by this list.
- ClassCastException - if the class of an element of the specified collection prevents it from being added to this list.
- NullPointerException - if the specified collection contains one or more null elements and this list does not permit null elements, or if the specified collection is null.
- IllegalArgumentException - if some property of an element of the specified collection prevents it from being added to this list.
2). addAll(int index, Collection c)
This is used to insert elements from a collection at the given index. All the elements in the list are shifted towards the right to make space for the elements from the collection.
boolean addAll(int index,
Collection c)
It will throws the following exceptions
- UnsupportedOperationException - if the addAll operation is not supported by this list.
- ClassCastException - if the class of an element of the specified collection prevents it from being added to this list.
- NullPointerException - if the specified collection contains one or more null elements and this list does not permit null elements, or if the specified collection is null.
- IllegalArgumentException - if some property of an element of the specified collection prevents it from being added to this list.
- IndexOutOfBoundsException - if the index is out of range (index < 0 || index > size())
package com.javacodestuffs.core.list;
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
public class ListAddExample2 {
public static void main(String[] args) {
List < String > langList = new ArrayList < >();
langList.add("Java");
langList.add("Php");
langList.add(".Net");
langList.add("Node");
langList.add("Python");
langList.add("Angular");
langList.add("Go");
langList.add("Scala");
langList.add("Groovy");
System.out.println("List of programming languages is : ");
System.out.println(langList);
langList.addAll(Arrays.asList("Dart", "Perl", "Rails"));
System.out.println("\nList of programming languages after adding : ");
System.out.println(langList);
System.out.println("\nList of programming languages after adding new values : ");
langList.addAll(4, Arrays.asList("Java Script", "C++"));
System.out.println(langList);
}
}
output:
List of programming languages is :
[Java, Php, .Net, Node, Python, Angular, Go, Scala, Groovy]
List of programming languages after adding :
[Java, Php, .Net, Node, Python, Angular, Go, Scala, Groovy, Dart, Perl, Rails]
List of programming languages after adding new values :
[Java, Php, .Net, Node, Java Script, C++, Python, Angular, Go, Scala, Groovy, Dart, Perl, Rails]
UnsupportedOperationException with List add() Methods
There are the most 2 common scenarios where we get UnsupportedOperationException with List add()
1). Arrays.asList()
This method returns a fixed-size list because it’s backed by the specified array. Any operation where the list size is changed throws UnsupportedOperationException.
List
langlist = Arrays.asList("Dart", "Perl", "Rails") ["Dart", "Perl", "Rails"]
but we do like
langlist.add("Java");// will throws UnsupportedOperationException
2). Collections.unmodifiableList(List l)
This method returns a unmodifiable view of the given list. So the add() operations throw UnsupportedOperationException.
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import java.util.Collections;
public class ListAddExample3 {
public static void main(String[] args) {
List < String > langList = new ArrayList < >();
langList.add("Java");
langList.add("Php");
langList.add(".Net");
langList.add("Node");
langList.add("Python");
langList.add("Angular");
langList.add("Go");
langList.add("Scala");
langList.add("Groovy");
System.out.println("List of programming languages is : ");
System.out.println(langList);
List < String > langList1 = Collections.unmodifiableList(langList);
System.out.println("\nModified List of programming languages is : ");
langList1.add("Perl");
}
}
output:
List of programming languages is :
[Java, Php, .Net, Node, Python, Angular, Go, Scala, Groovy]
Modified List of programming languages is :
Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.Collections$UnmodifiableCollection.add(Collections.java:1055)
at ListAddExample3.main(ListAddExample3.java:26)
Post/Questions related to List add() Method in Java
How to return string array in java?
How do I add values to an existing list in Java?
How to get first element of list in java?
How do you pass a list in Java?
How to get first n elements of a list in java?
In this article, we have seen List add() Method in Java with Examples. All source code in the article can be found in the GitHub repository.
0 Comments
Post a Comment