June 2018
Beginner
722 pages
18h 47m
English
Each of the collection classes has a constructor that accepts a collection of elements of the same type. For example, here is how an object of class ArrayList can be created using the ArrayList(Collection collection) constructor and how an object of class HashSet can be created using the HashSet<Collection collection) constructor:
List<String> list1 = new ArrayList<>();list1.add("s1");list1.add("s1");List<String> list2 = new ArrayList<>(list1);System.out.println(list2); //prints: [s1, s1]Set<String> set = new HashSet<>(list1);System.out.println(set); //prints: [s1]List<String> list3 = new ArrayList<>(set);System.out.println(list3); //prints: [s1]
We will show more examples of using such constructors in the Using other ...
Read now
Unlock full access