public
LinkedList(Collection<? extends E> c)
Constructs
a list containing the elements of the specified collection, in the
order they are returned by the collection's iterator.
import java.util.*; class LinkedListConstructor{ public static void main(String args[]){ LinkedList<Integer> myList; Collection<Integer> myColl; myColl = new HashSet<> (); /* Add Elements to myList */ myColl.add(10); myColl.add(20); myColl.add(null); myList = new LinkedList<> (myColl); System.out.println("Elements in myList are"); System.out.println(myList); } }
Output
Elements in myList are [null, 20, 10]
1. Throws
NullPointerException if the specified collection is null
import java.util.*; class LinkedListConstructorNull{ public static void main(String args[]){ LinkedList<Integer> myList; Collection<Integer> myColl; myColl = null; myList = new LinkedList<> (myColl); } }
Output
Exception in thread "main" java.lang.NullPointerException at java.util.LinkedList.addAll(LinkedList.java:406) at java.util.LinkedList.addAll(LinkedList.java:385) at java.util.LinkedList.<init>(LinkedList.java:117) at LinkedListConstructorNull.main(LinkedListConstructorNull.java:10)
No comments:
Post a Comment