Generics in Java KT Presentation
Generics in Java KT Presentation
Java
PRESENTER: ARPAN PANIGRAHI
DATE: [TODAY'S DATE]
Introduction
Type Safety
Elimination of Type Casting
Code Reusability
Basic Concepts
Generic Interface:
public interface Pair<K, V> {
public K getKey();
public V getValue();
}
Generic Methods
Generic Method:
public <T> void printArray(T[] array) {
for (T element : array) {
System.out.println(element);
}
}
Bounded Type
Parameters
Upper Bound:
public <T extends Number> void printNumber(T
number) {
System.out.println(number);
}
Lower Bound:
public void addNumbers(List<? super Integer> list)
{
list.add(new Integer(10));
}
Wildcards in Generics
Unbounded Wildcard:
List<?> list = new ArrayList<String>();
Collections Framework:
List<String> list = new ArrayList<>();
list.add("Hello");
Questions?