Creating List Views – Collections, Part I: ArrayList

12.7 Creating List Views

The asList() method in the Arrays class and the toArray() methods in the Collection<E> interface provide the bidirectional bridge between arrays and collections. The asList() method of the Arrays class creates List<E> views of arrays.

Click here to view code image

@SafeVarargs <E> List<E> asList(E… elements)                    
From
 Arrays
class
.

Returns a fixed-size list view that is backed by the array corresponding to the variable arity parameter elements. The method is annotated with @SafeVarargs because of the variable arity parameter. The annotation suppresses the heap pollution warning in its declaration and also unchecked generic array creation warning at the call sites (§25.5, p. 1585).

Changes to the elements of the list view are reflected in the array, and vice versa. The list view is said to be backed by the array. The size of the list view is equal to the array length and cannot be changed. The iterator for a list view does not support the remove() method.

The code below illustrates use of the asList() method. The list1 at (1) is backed by the array1. The list2 is backed by an implicit array of Integer at (2). An array of a primitive type cannot be passed as an argument to this method, as evident by the compile-time error at (3). However, the Collections.addAll() method provides better performance when adding a few elements to an existing collection.

Click here to view code image

Integer[] array1 = new Integer[] {9, 1, 1};
List<Integer> list1 = Arrays.asList(array1);          // (1) A list of Integer
List<Integer> list2 = Arrays.asList(9, 1, 1);         // (2) Varargs
int[] array2 = new int[] {9, 1, 1};                   // An array of int
// List<Integer> intList3 = Arrays.asList(array2);    // (3) Compile-time error!

Various operations on the list1 show how changes are reflected in the backing array1. Elements cannot be added to the list view (shown at (4)), and elements cannot be removed from the list view (shown at (9)). An UnsupportedOperationException is thrown in both cases. An element at a given position can be changed, as shown at (5). The change is reflected in the list1 and the array1, as shown at (6) and (7), respectively. A sublist view is created from the list1 at (8), and sorted at (10). The changes in the sublist1 are reflected in the list1 and the backing array1.

Click here to view code image

System.out.println(list1);                   // [9, 1, 1]
// list1.add(10);                            // (4) UnsupportedOperationException
list1.set(0, 10);                            // (5)
System.out.println(list1);                   // (6) [10, 1, 1]
System.out.println(Arrays.toString(array1)); // (7) [10, 1, 1]
List<Integer> sublist1 = list1.subList(0, 2);// (8)
System.out.println(sublist1);                // [10, 1]
// sublist1.clear();                         // (9) UnsupportedOperationException
Collections.sort(sublist1);                  // (10)
System.out.println(sublist1);                // [1, 10]
System.out.println(list1);                   // [1, 10, 1]
System.out.println(Arrays.toString(array1)); // [1, 10, 1]

The code below shows how duplicates can be eliminated from an array:

Click here to view code image

String[] jiveArray   = new String[] {“java”, “jive”, “java”, “jive”};
Set<String> jiveSet  = new HashSet<>(Arrays.asList(jiveArray));// (1)
String[] uniqueJiveArray = jiveSet.toArray(new String[0]);     // (2)
System.out.println(Arrays.toString(uniqueJiveArray));           // (3) [java, jive]

At (1), the jiveArray is used to create a List, which in turn is used to create a Set. At (2), the argument to the toArray() method specifies the type of the array to be created from the set. The final array uniqueJiveArray does not contain duplicates, as can be seen at (3).

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *