3. generics

22
Generics

Upload: archiseth516303960

Post on 10-Jul-2016

257 views

Category:

Documents


0 download

DESCRIPTION

A document that describes the generics feature in Java Collections

TRANSCRIPT

Page 1: 3. Generics

Generics

Page 2: 3. Generics

Generics enable types (classes and interfaces) to be parameters when defining classes, interfaces and methods

Inputs to formal parameters are values, while the inputs to type parameters are types

public class Box<T> T stands for "Type"

List<String> list = new ArrayList<String>();

Introduction

Page 3: 3. Generics

Stronger type checks at compile timeEnabling programmers to implement generic algo-rithmsElimination of casts

Benefits

1. List list = new Ar-rayList();

2. list.add("hello");3. String s = (String)

list.get(0);

1. List<String> list = new Ar-rayList<String>();

2. list.add("hello");3. String s = list.get(0);

Page 4: 3. Generics

E - Element K- KeyN - NumberT - TypeV - ValueS,U,V etc. - 2nd, 3rd, 4th types

Type Parameter Naming Conventions

Page 5: 3. Generics

Box<Integer> integerBox = new Box<Integer>();

Java SE 7 and later

Box<Integer> integerBox = new Box<>();

Diamond

Page 6: 3. Generics

public class OrderedPair<K, V>

OrderedPair<String, Integer> p1 = new Or-deredPair<String, Integer>("Even", 8);

OrderedPair<String, Integer> p1 = new Or-deredPair<>("Even", 8);

OrderedPair<String, Box<Integer>> p = new OrderedPair<>("primes", new Box<Integer>(...));

Multiple Type Parameters

Page 7: 3. Generics

Generic methods are methods that introduce their own type parameters.

This is similar to declaring a generic type, but the type parameter's scope is limited to the method where it is declared.

Static and non-static generic methods are allowed, as well as generic class constructors.

Generic Methods

Page 8: 3. Generics

1. public class Util {2. public static <K, V> boolean

compare(Pair<K, V> p1, Pair<K, V> p2) {3. return p1.getKey().equals(p2.getKey())

&&4.

p1.getValue().equals(p2.getValue());5. }6. }

7. Pair<Integer, String> p1 = new Pair<>(1, "apple");

8. Pair<Integer, String> p2 = new Pair<>(2, "pear");

9. boolean same = Util.<Integer, String>compare(p1, p2);

Generic Methods

Page 9: 3. Generics

boolean same = Util.compare(p1, p2);

This feature, known as type inference, allows you to invoke a generic method as an ordinary method, without specifying a type between angle brackets.

Generic Methods

Page 10: 3. Generics

1. Box<Number> box = new Box<Number>();

2. box.add(new Integer(10)); // OK3. box.add(new Double(10.1)); // OK

Box<Integer> is not a subtype of Box<Number> even though Integer is a subtype of Number.

Generics, Inheritance, and Subtypes

Page 11: 3. Generics

Generics, Inheritance, and Subtypes

Page 12: 3. Generics

ArrayList<E> implements List<E>, and List<E> extends Collection<E>. ArrayList<String> is a subtype of List<String>, which is a subtype of Collection<String>.

A sample Collections hierarchy

So long as you do not vary the type argument, the subtyping relationship is preserved between the types.

Generic Classes and Subtyping

Page 13: 3. Generics

The question mark (?), called the wildcard, represents an unknown typeThe wildcard can be used:

As the type of a parameterFieldLocal variableSometimes as a return type

The wildcard is never used: As a type argument for a generic method invoca-tionA generic class instance creatio

Wildcards

Page 14: 3. Generics

1. public static double sumOfList(List<? extends Num-ber> list) {

2. double s = 0.0;3. for (Number n : list)4. s += n.doubleValue();5. return s;6. }

7. List<Integer> li = Arrays.asList(1, 2, 3);8. System.out.println("sum = " + sumOfList(li));

9. List<Double> ld = Arrays.asList(1.2, 2.3, 3.5);10.System.out.println("sum = " + sumOfList(ld));

Upper Bounded Wildcards

Page 15: 3. Generics

1. public static void addNumbers(List<? super In-teger> list) {

2. for (int i = 1; i <= 10; i++) {3. list.add(i);4. }5. }

Work on List<Integer>, List<Number>, and List<Object> — anything that can hold Integer values.

Lower Bounded Wildcards

Page 16: 3. Generics

List<? > ----unknown type

An unbounded wildcard is a useful approach:If you are writing a method that can be imple-mented using functionality provided in the Ob-ject class.

When the code is using methods in the generic class that don't depend on the type parameter. For example, List.size or List.clear. In fact, Class<?> is so often used because most of the methods in Class<T> do not depend on T.

Unbounded Wildcards

Page 17: 3. Generics

1. public static void printList(List<Object> list) {2. for (Object elem : list)3. System.out.println(elem + " ");4. System.out.println();5. }

It prints only a list of Object instances; it cannot print List<Integer>, List<String>, List<Double>, and so on, because they are not subtypes of List<Object>

Unbounded Wildcards

Page 18: 3. Generics

1. public static void printList(List<?> list) {2. for (Object elem: list)3. System.out.print(elem + " ");4. System.out.println();5. }

Because for any concrete type A, List<A> is a subtype of List<?>, you can use printList to print a list of any type

Unbounded Wildcards

Page 19: 3. Generics

Cannot Instantiate Generic Types with Primitive TypesPair<int, char> p = new Pair<>(8, 'a');

Cannot Create Instances of Type Parameterspublic static <E> void append(List<E> list) { E elem = new E();

Cannot Declare Static Fields Whose Types are Type Pa-rameters

public class MobileDevice<T> {private static T os;

Restrictions on Generics

Page 20: 3. Generics

Cannot Use Casts or instanceof with Parameterized Types

public static <E> void rtti(List<E> list) { if (list instanceof ArrayList<Integer>)

Cannot Create Arrays of Parameterized TypesList<Integer>[] arrayOfLists = new

List<Integer>[2];

Cannot Create, Catch, or Throw Objects of Parameter-ized Type

// Extends Throwable indirectlyclass MathException<T> extends Exception { /* ... */ }

Restrictions on Generics

Page 21: 3. Generics

1.2.3.4.5.

Summary

Page 22: 3. Generics

Thank You