Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Ganesh_JavaSE7_Programming_1z0-804_study_guide.pdf
Скачиваний:
94
Добавлен:
02.02.2015
Размер:
5.88 Mб
Скачать

Chapter 6 GeneriCs and ColleCtions

Similarly to using the extends keyword with wildcards, as in <? extends Number>, you can use the super keyword, as in <? super Integer>. The expression <? super X> means that you can use any super type (class or interface) including the type X. For instance, the following code snippet compiles well. You may observe that

<? super Integer> does not only mean super types of Integer; this expression allows Integer, too.

List<? super Integer> intList = new ArrayList<Integer>();

System.out.println("The intList is: " + intList);

in the bounded wildcard, <? extends X>, X may be a class or an interface (note that even for interfaces you use the extends keyword). the valid substitution for ? is not just any of its derived classes and interfaces; you can substitute ? for X itself! this is also applicable with <? super X> expressions.

Wildcards in the Collections Class

The collections framework uses wildcards extensively. To understand some more features of bounded wildcards and how wildcards are used in practice, here are some examples from the Collections class.

The first example is the nCopies method:

static <T> List<T> nCopies(int num, T obj)

The nCopies method returns a read-only List of num elements with value obj. Here is an example:

System.out.println("List of 5 elements filled with values 10: " + Collections.nCopies(5, 10));

It prints the following:

List of 5 elements filled with values 10: [10, 10, 10, 10, 10]

The next example is the reverse method:

static void reverse(List<?> list);

The reverse() method reverses the order of elements in the passed list. You can pass elements of any type; the Collections class just uses the wildcard <?> for the List type.

Here’s another example:

static <T> void fill(List<? super T> list, T obj)

This method fills the whole of the list with values obj. Here you use <? super T>. Why? Here is an example:

List<Object> objList = new ArrayList<Object>(); objList.add(new Object());

objList.add(new Object());

Collections.fill(objList, "hello");

System.out.println("The objList is: " + objList);

168

Chapter 6 Generics and Collections

It prints the following:

The objList is: [hello, hello]

Here, you create a List<Object> that points to an ArrayList<Object>. You create two dummy Objects and insert objList. Then you fill the objList with the String “hello” and it works. As you can see, for the fill() method you can pass a base type List as the first argument.

And for now the final (and rather tough) example: the copy() method. Its declaration:

static <T> void copy(List<? super T> dest, List<? extends T> src);

The copy() method copies all the elements from src List to dest List. Here is an example to understand why dest is <? super T> and src is <? extends T>:

List<? extends Number> intList = Collections.nCopies(5, new Integer(10)); List<Object> objList = new ArrayList<Object>();

for(int i = 0; i < 5; i++) { objList.add(new Object());

}

Collections.copy(objList, intList); System.out.println("The objList is: " + objList);

It prints the following:

The objList is: [10, 10, 10, 10, 10]

Here is a step-by-step description of what’s happening in this code:

1.You first create a list intList of type List<? extends Number>. You initialize it with a List filled with five instances of Integer object with value 10 (i.e. new Integer(10);). This is the source List type you are going to use for the Collections.copy method.

2.You create another list objList of type List<Object> and initialize it with an ArrayList<Object>. You initialize objList with five dummy Object instances. You are going to use this List as the target for Collections.copy. Why didn’t you use the nCopies method just like what you did for intList? Because the List returned by nCopies is a read-only list and if you use that List as a target for the copy() method, you’ll get an

UnsupportedOperationException.

3.You use objList as the destination (target) and intList as the source for the Collections.copy method. Now, the type T inferred is Number. As you can see, the src is of type <? extends T> (i.e., for intList); when you substitute Integer as in <Integer extends Number>, the compilation succeeds. Similarly, the dest is of type <? extends T> (i.e., for objList); when you substitute Object as in <Object super Number>, the compilation succeeds.

4.You print the copied integer values in objList.

169

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]