Generics in Java

Generics Many algorithms work in a similar way irrespective of the data types on which they are applied on All Stacks work in a similar way irrespective of the type of object they hold Generics help to write algorithms independent of a data type These algorithms can be applied to a variety of data types The data type is parameterized A class, interface or a method that works on a parameterized type is called a generic public class List<T>{ protected T [] list; public List(T [] list){ this.list = list; } public void put(T element, int position){ list[position] = element; } public T get(int position){ return list[position]; } } In above code, T represents a data type. While declaring an object of List, the programmer can decide the actual type for T. List<String> list = new List<String>(new String[5]); list.put(“Hello”, 0); list.put(“Welcome”, 1); list.put(“Ok”, 2); System.out.println(list.get(0)); System.out.println(list.get(1)); System.out.println(list.get(2));   Just like… Read more“Generics in Java”