Generic Types in Java (for ArtClub @ArtBrains Software)

64
GENERIC TYPES IN JAVA

Transcript of Generic Types in Java (for ArtClub @ArtBrains Software)

Page 1: Generic Types in Java (for ArtClub @ArtBrains Software)

GENERIC TYPES IN JAVA

Page 2: Generic Types in Java (for ArtClub @ArtBrains Software)

AGENDA

General informationBounds and RestrictionsErasureHeap pollution

Page 3: Generic Types in Java (for ArtClub @ArtBrains Software)

Java8Lambda

SpringGenerics Stream API

Page 4: Generic Types in Java (for ArtClub @ArtBrains Software)

HOW GOOD YOU ARE AT GENERICS?

<R> Strudel<R> loop(Function<? super T,? extends R> mapper)<R> Stream<R> map(Function<? super T,? extends R> mapper)

Page 5: Generic Types in Java (for ArtClub @ArtBrains Software)

A generic type is a generic class or interface that is parameterized over types.

Code that uses generics has many benefits over non-generic code:

Stronger type checks at compile time.Elimination of casts.Enabling programmers to implement generic algorithms.

Page 6: Generic Types in Java (for ArtClub @ArtBrains Software)

public class Box {

private Object object;

public void set(Object o){ this.object = o; } public Object get() { return object; }}

public class Box<T> {

private T t;

public void set(T t) { this.t = t; } public T get() { return t; }}

As you can see, all occurrences of Object are replaced by T. A type variable can be any non-primitive type you specify: any

class type, any interface type, any array type, or even another type variable.

Simple Example

Page 7: Generic Types in Java (for ArtClub @ArtBrains Software)

public class Test {

public static void main(String[] args) { BoxPrinter value1 = new BoxPrinter(new Integer(10)); System.out.println(value1); Integer intValue1 = (Integer) value1.getValue(); BoxPrinter value2 = new BoxPrinter("Hello world"); System.out.println(value2);

// Ooops ... Integer intValue2 = (Integer) value2.getValue(); }}

class BoxPrinter { private Object val;

public BoxPrinter(Object arg) { val = arg; }

public String toString() { return "{" + val + "}"; }

public Object getValue() { return val; }}

Page 8: Generic Types in Java (for ArtClub @ArtBrains Software)

public class Test {

public static void main(String[] args) { BoxPrinter<Integer> value1 = new BoxPrinter<Integer>(new Integer(10)); System.out.println(value1); Integer intValue1 = value1.getValue(); BoxPrinter<String> value2 = new BoxPrinter<String>("Hello world"); System.out.println(value2);

// Compile error Integer intValue2 = value2.getValue(); }}

class BoxPrinter<T> { private T val;

public BoxPrinter(T arg) { val = arg; }

public String toString() { return "{" + val + "}"; }

public T getValue() { return val; }}

Page 9: Generic Types in Java (for ArtClub @ArtBrains Software)

Naming ConventionsThe most commonly used type parameter names

are:

E - Element (used extensively by the Java Collections Framework)K - KeyN - NumberT - TypeV - ValueS,U,V etc. - 2nd, 3rd, 4th types

Page 10: Generic Types in Java (for ArtClub @ArtBrains Software)

Integer i = 127; Class<Integer> c = i.getClass();//compile error

Page 11: Generic Types in Java (for ArtClub @ArtBrains Software)

Generic MethodsGeneric 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.

public static <K, V> boolean compare(Pair<K, V> p1, Pair<K, V> p2) { return p1.getKey().equals(p2.getKey()) && p1.getValue().equals(p2.getValue());}

Page 12: Generic Types in Java (for ArtClub @ArtBrains Software)

GENERIC GENERAL RESTRICTIONS• Cannot Instantiate Generic Types with Primitive

Types• Cannot Create Instances of Type Parameters• Cannot Declare Static Fields Whose Types are Type

Parameters• Cannot Use Casts or instanceof With

Parameterized Types• Cannot Create Arrays of Parameterized Types• Cannot Create, Catch, or Throw Objects of

Parameterized Types• Cannot Overload a Method Where the Formal

Parameter Types of Each Overload Erase to the Same Raw Type

Page 13: Generic Types in Java (for ArtClub @ArtBrains Software)

Cannot Create Instances of Type Parameters

You cannot create an instance of a type parameter. For example, the following code causes a compile-time error:

But, You can always use Reflection API

public static <E> void append(List<E> list) { E elem = new E(); // compile-time error list.add(elem);}

public static <E> void append(List<E> list, Class<E> cls) throws Exception { E elem = cls.newInstance(); // OK list.add(elem);}

Page 14: Generic Types in Java (for ArtClub @ArtBrains Software)

Cannot Use Casts or instanceof with Parameterized Types

Because the Java compiler erases all type parameters in generic code, you cannot verify which parameterized type for a generic type is being

used at runtime:

Cannot Create Arrays of Parameterized Types

You cannot create arrays of parameterized types. For example, the following code does not compile:

public static <E> void doSmth(List<E> list) { if (list instanceof ArrayList<Integer>) { // compile-time error // some logic }}

List<Integer>[] arrayOfLists = new List<Integer>[2]; // compile- time error

Page 15: Generic Types in Java (for ArtClub @ArtBrains Software)

Cannot Create, Catch, or Throw Objects of Parameterized Types

A generic class cannot extend the Throwable class directly or indirectly.// Extends Throwable indirectly

// Extends Throwable directly

A method cannot catch an instance of a type parameter:

You can, however, use a type parameter in a throws clause:

class MathException<T> extends Exception // compile-time error

class QueueFullException<T> extends Throwable // compile-time error

public static <T extends Exception, J> void execute(List<J> jobs) { try { for (J job : jobs); } catch (T e) { // compile-time error }}

class Parser<T extends Exception> { public void parse(File file) throws T { // OK}

Page 16: Generic Types in Java (for ArtClub @ArtBrains Software)

public class Quiz5<T> {

public List<Integer> numbers() { return Arrays.asList(1,2); }

public static void main(String[] args) { Quiz5 quiz = new Quiz5();

for (Integer i: quiz.numbers()) { System.out.println(i); } }

}// wtf?! this code is ok

// ClassCastException// Compile Error// NPE

//error hereincompatible types

Page 17: Generic Types in Java (for ArtClub @ArtBrains Software)

TYPE

Generics were introduced to the Java language to provide tighter type checks at compile time and to

support generic programming. To implement generics, the Java compiler applies

type erasure.

Page 18: Generic Types in Java (for ArtClub @ArtBrains Software)

public class Person implements Comparable<Person> {

private String name;

@Override public int compareTo(Person o) { return name.compareTo(o.name); }

public int compareTo(Object o) { return toString().compareTo(o.toString()); }}

//compile error// both methods have same erasure

ERASURE

Page 19: Generic Types in Java (for ArtClub @ArtBrains Software)

class Box<T> { private T value; public Box (T t) { this.value = t; } public T get() { return value; }}

Box<Integer> box = new Box<>(10);Integer i = box.get();

Box box = new Box (10);Integer i = (Integer) box.get();

class Box { private Object value; public Box (Object t) { this.value = t; } public Object get() { return value; }}

//java compiles //we write

Page 20: Generic Types in Java (for ArtClub @ArtBrains Software)

Bridge Methodspublic class Person implements Comparable<Person> { private String name;

@Override public int compareTo(Person o) { return name.compareTo(o.name); }

public static void main(String[] args) { Stream.of(Person.class.getDeclaredMethods()). forEach(System.out::println); }}

public int Quiz4.Person.compareTo(java.lang.Object)public int Quiz4.Person.compareTo(Quiz4.Person)

//Sout//wtf? o.O

Page 21: Generic Types in Java (for ArtClub @ArtBrains Software)

public interface Comparable<T> {int CompareTo (T t)

}

public interface Comparable {int CompareTo (Object o)

}public class Person implements Comparable<Person> {

@Override public int compareTo(Person o) { return 0; }

}

//isSynthetic()public int compareTo(Object o) { return compareTo((Person) o);}

Page 22: Generic Types in Java (for ArtClub @ArtBrains Software)

ArrayList - sometimes things are different from what you see

public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable{. . . private E[] elementData; private int DEFAULT_CAPACITY = 10;. . . public ArrayList() { elementData = new E[DEFAULT_CAPACITY]; }. . .}

Aww, you fool! It’s not going to work!

Type parameter «E» cannot be instantiated

directly

Page 23: Generic Types in Java (for ArtClub @ArtBrains Software)

public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable {

private E[] elementData; private int DEFAULT_CAPACITY = 10;

public ArrayList() { elementData = (E[]) new Object[DEFAULT_CAPACITY]; }

. . .}

As I said in «Effective Java» - casts are good.

Well done, my boy!

Page 24: Generic Types in Java (for ArtClub @ArtBrains Software)

Bounds || Restrictionspublic class Box <T> { private T t; public <U extends Number> void inspect(U u){ System.out.println("T: " + t.getClass().getName()); System.out.println("U: " + u.getClass().getName()); }

public static void main(String[] args) { Box<Integer> integerBox = new Box<Integer>(); integerBox.set(new Integer(10)); integerBox.inspect("some text"); // error: this is still String! }}

class NaturalNumber<T extends Integer> { private T n;

public NaturalNumber(T n) { this.n = n; }

public boolean isEven() { return n.intValue() % 2 == 0; } //The isEven method invokes //the intValue method defined in //the Integer class through n.}

class D <T extends A & B & C> // we can do multiple bounds, BUT …

Page 25: Generic Types in Java (for ArtClub @ArtBrains Software)

Type Inference

Type inference is a Java compiler's ability to look at each method invocation and corresponding

declaration to determine the type argument (or arguments) that make the invocation applicable.

static <T> T pick(T a1, T a2) { return a2; }Serializable s = pick("d", new ArrayList<String>());

Page 26: Generic Types in Java (for ArtClub @ArtBrains Software)

Target TypesThe Java compiler takes advantage of target typing to

infer the type parameters of a generic method invocation. The target type of an expression is the data type that the Java compiler expects depending on where

the expression appears.

static <T> List<T> emptyList();

processStringList(Collections.emptyList());

void processStringList(List<String> stringList) {/* some logic */ }

List<Object> cannot be converted to List<String>

ClassCastException

Incompatible types

No errors or exceptions//JDK7

Page 27: Generic Types in Java (for ArtClub @ArtBrains Software)
Page 28: Generic Types in Java (for ArtClub @ArtBrains Software)

WILDCARDS

Page 29: Generic Types in Java (for ArtClub @ArtBrains Software)

Upper Bounded WildCardsTo write the method that works on lists of Number and the subtypes of Number, such as Integer, Double, and Float, you would specify List<?

extends Number>.

public static void process(List<? extends Foo> list) { }

public static void process(List<? extends Foo> list) { for (Foo elem : list) { // ... }}

public static double sumOfList(List<? extends Number> list) { double s = 0.0; for (Number n : list) s += n.doubleValue(); return s;}

List<Integer> li = Arrays.asList(1, 2, 3);System.out.println("sum = " + sumOfList(li)); //sum = 6.0List<Double> ld = Arrays.asList(1.2, 2.3, 3.5);System.out.println("sum = " + sumOfList(ld)); //sum = 7.0

Page 30: Generic Types in Java (for ArtClub @ArtBrains Software)

Story about no one, experimentalclass Foo<T extends Cloneable> { public void doSomething(T param) { T copy = (T) param.clone(); }}

3. this code is perfect,

don’t talk shit

2. incompatible types1. ClassCastException

4. none above

Page 31: Generic Types in Java (for ArtClub @ArtBrains Software)

Unbounded Wildcard

The goal of printList is to print a list of any type, but it fails to achieve that goal — 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>. To write a generic printList method, use List<?>:

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

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

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

List<Integer> li = Arrays.asList(1, 2, 3);List<String> ls = Arrays.asList("one", "two", "three");printList(li);printList(ls);

Page 32: Generic Types in Java (for ArtClub @ArtBrains Software)

Lower Bounded Wildcards

You can specify an upper bound for a wildcard, or you can specify a lower bound, but you cannot specify both.

<? super X>As for key word <? extends> You can use <?

super> to specify any base class or interface of X, and X itself

List<? super Integer> list

Page 33: Generic Types in Java (for ArtClub @ArtBrains Software)

List<? extends Number> numbers = new ArrayList<>();

List<? super Number> numbers = new ArrayList<>();

What we can add to the following collections via numbers.add() ?

Integer, Double, Number, Long

Object, Number

List<?> list = new ArrayList<>();anything?

null

null

all that ? extend Number

Page 34: Generic Types in Java (for ArtClub @ArtBrains Software)

List <? extends Number> list = new ArrayList<>();

list.add(); <- null

List <? extends Number> list = …list = new ArrayList<Number>;list = new ArrayList<Integer>;list = new ArrayList<Long>;

//what compiler sees

//what possibly can come

Page 35: Generic Types in Java (for ArtClub @ArtBrains Software)

List <? super Number> list = new ArrayList<>();

list.add(); <- all that ? extends Number

List <? super Number> list = …//what compiler sees

list = ArrayList<Object>();list = ArrayList<Number>();//what possibly can come

Page 36: Generic Types in Java (for ArtClub @ArtBrains Software)

public class MindBlown <T extends MindBlown<T>>

WTF is it? An infinite recursion? How this gonna work?

public class Enum <E extends Enum<E>>

WHY?

1. The type parameter E is used in various methods of Enum, such as compareTo() or getDeclaringClass()

2. Classes that are type arguments to Enum must themselves be subtypes of Enum, so you can't declare a class X to extend Enum<Integer>

3. Any class that extends Enum must pass itself as the type parameter. You cannot declare X to extend Enum<Y>, even if Y extends Enum.

Page 37: Generic Types in Java (for ArtClub @ArtBrains Software)

public void run(List<String>...list) {}

//WARNING: possible heap pollution

HEAP

List<String> = List<Integer>

Page 38: Generic Types in Java (for ArtClub @ArtBrains Software)

Number [] numbers = new Integer [10];List<Number> numberList = new ArrayList<Integer>();

// compile error

numbers[0] = 9.7d; //ArrayStoreException

!Arrays are co-variant, Generics - invariant!

Page 39: Generic Types in Java (for ArtClub @ArtBrains Software)

IT’S PUZZLER TIME!!!

Page 40: Generic Types in Java (for ArtClub @ArtBrains Software)

Story about one girl, who loved Interfaces

Page 41: Generic Types in Java (for ArtClub @ArtBrains Software)

public static void main(String[] args) {

String stringIsFinalClass = newlist();

}

private static <A extends List> A newList() { return (A) new ArrayList<>(); }

Cannot infer arguments

Incompatible types

Runtime error

I once wright code like this

Page 42: Generic Types in Java (for ArtClub @ArtBrains Software)

Story about one dev, who loved checked Exceptions

Page 43: Generic Types in Java (for ArtClub @ArtBrains Software)

public class B { public static <T> T foo() { try { return (T) new Integer(42); } catch (ClassCastException e) { return (T) "ArtClub"; } } public static void main(String[] args) { System.out.println(B.<String>foo()); }}

Compile errorRuntime error

print 42print ArtClub

ClassCastException

Page 44: Generic Types in Java (for ArtClub @ArtBrains Software)
Page 45: Generic Types in Java (for ArtClub @ArtBrains Software)

Story about one boy, who trust compilers

Page 46: Generic Types in Java (for ArtClub @ArtBrains Software)

List<Integer> integers = new ArrayList<Integer>();List list = integers;List<Number> numbers = list;numbers.add(9.78d);

9.78

ClassCastException

Compile Error

9

System.out.println(numbers.get(0));System.out.println(integers.get(0));

Page 47: Generic Types in Java (for ArtClub @ArtBrains Software)
Page 48: Generic Types in Java (for ArtClub @ArtBrains Software)

Story about one philosopher , who loved to focus

Page 49: Generic Types in Java (for ArtClub @ArtBrains Software)

Set<Number> set = new TreeSet<>();set.add(1);set.add(1L);set.add(1.00);

System.out.println(set.size());

A. 3 B. Compile error

C. 2 D. Runtime error

Page 50: Generic Types in Java (for ArtClub @ArtBrains Software)

Story about one Andriy, who loved arrays

Page 51: Generic Types in Java (for ArtClub @ArtBrains Software)

public static void main(String[] args) { setFirst(new String[10],}

1.incompatible types

2.ClassCastException3.cannot infer arguments

4.i don’t believe generics anymore

//JDK 7

1);new Integer(1));

static <T> void setFirst(T[] ar, T s) { ar[0] = s;}

// ну ок

Page 52: Generic Types in Java (for ArtClub @ArtBrains Software)

How we fix it?

static <T, S extends T> void setFirst(T[] ar, S s) { ar[0] = s;}

public static void main(String[] args) { setFirst(new String[10], new Integer(1));}

// compile error

//JDK8

// ArrayStoreException

Page 53: Generic Types in Java (for ArtClub @ArtBrains Software)

Story about one Troll, who lives under bridge

Page 54: Generic Types in Java (for ArtClub @ArtBrains Software)

abstract class DefaultList<T> {

abstract void add(T ... elements);

void addNotNull(T...elements) { for (T element : elements) { if (element != null); add(element); } }}

public class ConcreateList extends DefaultList<String> {

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

@Override void add(String... elements) { list.addAll(Arrays.asList(elements)); }

@Override public String toString() { return list.toString(); }

public static void main(String[] args) { DefaultList<String> stringList = new ConcreateList(); stringList.addNotNull("null", null); System.out.println(stringList); }}

1. «null»

2. «null», «null»

3. Compile error

4. Runtime error

// error here

// error here

// error here

Page 55: Generic Types in Java (for ArtClub @ArtBrains Software)
Page 56: Generic Types in Java (for ArtClub @ArtBrains Software)

Story about one gayse, who wants ofigenno

Page 57: Generic Types in Java (for ArtClub @ArtBrains Software)

public class Compressor<T> {

String compress(Collection<?> obj) { String s = ""; for (Object o : obj) s += obj.toString(); return s; }

int compress(List<Integer> list) { int result = 0; for (int i: list) result+=i; return result; }

public static void main(String[] args) {

List<String> list = Arrays.asList("1","2","3"); System.out.println(new Compressor().compress(list)); }

}

1. Compile error

2. Runtime error

3. 6

4. 123

// classCastException

Page 58: Generic Types in Java (for ArtClub @ArtBrains Software)

PECS (Producer - extends, Consumer - super)

If a parametrized type represents a T producer -

use <? extends T>;

If a parametrized type represents a T consumer -

use <? super T>; Joshua Bloch

public static <T> T max(Collection<? extends T> coll, Comparator<? super T> comp);

Page 59: Generic Types in Java (for ArtClub @ArtBrains Software)

Integer i = 127; Class<Integer> c = i.getClass();//compile error

Why this isn’t working?class Object {Class<?> getClass();

}

class Number {Class<Number>

getClass();}

class Integer {Class<Integer>

getClass();}

Page 60: Generic Types in Java (for ArtClub @ArtBrains Software)
Page 61: Generic Types in Java (for ArtClub @ArtBrains Software)

1, 2, 3, 14409881081, 2, 3, 23 nov 2016 19:56:00:00:00

ClassCastExceptionCompile error

List<Long> list = new ArrayList<>();list.add(1L);list.add(2L);list.add(3L);Date now = new Date();list.add(now);

for (def node: list) { System.out.println(node);}

Page 62: Generic Types in Java (for ArtClub @ArtBrains Software)
Page 63: Generic Types in Java (for ArtClub @ArtBrains Software)
Page 64: Generic Types in Java (for ArtClub @ArtBrains Software)

Q/A?

https://www.facebook.com/andrew.petryk@ipreferespresso

www.linkedin.com/in/andrew-petryk