Generics in Java with Examples
Generics allows us to pass the generic types as parameters to a class or methods.
Example: class A<T>
Generics Advantages:
- Stronger type checking
- Casting elimination
Type Parameter Naming Conventions:
- E – Element
- K – Key
- V – Value
- N – Number
- T – Type
There are various ways of using Generics, In this post let us see, generics with
- Upper Bounded Wildcards
- Unbounded Wildcards
- Lower Bounded Wildcards
First let us see an example of using generics along with the class
import java.util.List; /** * * @author vikram */ public class JavaGenerics<T> { private T t; public T getT() { return t; } public void setT(T t) { this.t = t; } public static void main(String args[]){ JavaGenerics<Integer> jg1 = new JavaGenerics<Integer>(); jg1.setT(1); JavaGenerics<String> jg2 = new JavaGenerics<String>(); jg2.setT("This is String"); System.out.println("Generics Printing Integer: "+jg1.getT()); System.out.println("Generics Printing String: "+jg2.getT()); } }
Output:
Now let us see another example of using Generics with Key and Value along with Upper Bound Wildcards, Unbounded Wildcard and Lower Bound Wildcards.
Wildcards usually starts with “?” – Example: List<? extends Number>
Upper Bound Wildcards
Used to relax restrictions on a variable. In the below example we have used upperBound wildcard to pass integers, calculate sum of it and change it double and print it.
Unbounded Wildcards:
Unbounded wildcards is used for unknown types. It can be of any type, Integer or String or object etc..
Lower Bound Wildcards:
Lower Bound Wildcards restricts the unknown type to be specific. (Upper Bound relaxes the restrictions)
import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * * @author vikram */ public class JavaGenericsPair<K,V> { private K key; private V value; public K getKey() { return key; } public void setKey(K key) { this.key = key; } public V getValue() { return value; } public void setValue(V value) { this.value = value; } public static void upperBoundWildCard(List<? extends Number> list){ double s = 0.0; for(Number n:list){ s = s+n.doubleValue(); } System.out.println("--------UpperBound-------"); System.out.println(s); } public static void Unbounded(List<?> list){ System.out.println("-------Unbounded-------"); list.forEach(System.out::println); } public static void LowerBounds(List<? super Integer> list){ System.out.println("--------Lower Bounds----------"); list.forEach(System.out::println); } public static void main(String args[]){ Student student = new Student(); student.setName("Student Name"); List upperBound = Arrays.asList(1,2,3,4); upperBoundWildCard(upperBound); List unbounded = Arrays.asList("one",2,student); Unbounded(unbounded); List list3 = new ArrayList(); list3.add(2.0); list3.add(1); list3.add(1.4f); list3.add(student); LowerBounds(list3); JavaGenericsPair<String, Integer> JGP1 = new JavaGenericsPair<String, Integer>(); JavaGenericsPair<String, String> JGP2 = new JavaGenericsPair<String, String>(); JGP1.setKey("One"); JGP1.setValue(1); JGP2.setKey("Two"); JGP2.setValue("twoTwo"); System.out.println("--------------Key Value Pairs------------"); System.out.println("Key Value Pair with String and Integer: "+JGP1.getKey()+" "+JGP1.getValue()); System.out.println("Key Value Pair with String and String: "+JGP2.getKey()+" "+JGP2.getValue()); } }