Java List copyOf method

Java List copyOf method Example

copyOf method added in jdk10.

Returns an unmodifiable List containing the elements of the given Collection, in its iteration order. The given Collection must not be null, and it must not contain any null elements. If the given Collection is subsequently modified, the returned List will not reflect such modifications.
Implementation Note:
If the given Collection is an unmodifiable List, calling copyOf will generally not create a copy.

Method:

static <E> List<E> copyOf​(Collection<? extends E> coll)

Implementation Note:
If the given Collection is an unmodifiable List, calling copyOf will generally not create a copy.
Type Parameters:
E - the List's element type
Parameters:
coll - a Collection from which elements are drawn, must be non-null
Returns:
a List containing the elements of the given Collection
Throws:
NullPointerException - if coll is null, or if it contains any nulls

Example:


class copyOfExample{
public static void main(String args[]){

List list = new ArrayList<>(obj);  //obj-> 1 2 3


list.copyOf(list2)  //list2 -> 1 2 3 4

//return 1 2 3

}
}

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation