Generics in java

Generics In Java

What is Generics

Parameterized types are referred to as generics. The aim is to let methods, classes, and interfaces accept types as parameters (Integer, String,… and user-defined types). Generics can be used to design classes that handle various data types. A generic entity is an entity (class, interface, function, etc.) that works with a parameterized type.

Need for Generics

Let us understand with an example. We have an ArrayList of string type and add an integer type element. Before Java 5 this code will compile and throw exceptions at the worst runtime. Generics provides the facility of compile-time type safety i.e. The compiler checks that the correct types should be used at the correct places and no ClassCastException.

Generics using Class

The implementation of a generic class is identical to that of a non-generic class. It differs only in that it has a type parameter section. A comma should be used to separate each type of parameter that is present. The classes that take in one or more parameters are referred to as parameterized types or classes.

MyCustomList class

package com.siteinvokers;

import java.util.ArrayList;

// this is generic class
public class MyCustomList<T> {
	ArrayList<T> list = new ArrayList<>();

	public void addElement(T element) {
		list.add(element);
	}

	public void removeElement(T element) {
		list.remove(element);
	}

	@Override
	public String toString() {
		return "MyCustomList [list=" + list + "]";
	}

	public T get(int index) {
		// TODO Auto-generated method stub
		return list.get(index);
	}

}

GenericsRunn class

This class is to run generics 

package com.siteinvokers;

import java.util.ArrayList;
import java.util.List;

public class GenericsRunn {
 

	// using lower bounded wild cards
	static void addCoupleOfValues(List<? super Number> numbers) {
		numbers.add(2);
		numbers.add(2.0);
		numbers.add(2.22f);
		numbers.add(2l);

	}

	public static void main(String[] args) {
		MyCustomList<String> list = new MyCustomList<>();

		list.addElement("Element 1");
		list.addElement("Element 2");
		System.out.println(list);
		// O/P - MyCustomList [list=[Element 1, Element 2]]

		// getting indexed element
		System.out.println(list.get(0));
		// O/P - Element 1

		MyCustomList<Integer> list2 = new MyCustomList<>();
		list2.addElement(Integer.valueOf(5));
		list2.addElement(Integer.valueOf(6));

		System.out.println(list2);
		// O/P : MyCustomList [list=[5, 6]]

		// getting indexed element
		System.out.println(list2.get(0));
		// O/P - 5

    }
}

Generics using static method

The implementation of a generic class is identical to that of a non-generic class. It differs only in that it has a type parameter section. A comma should be used to separate each type of parameter that is present. The classes that take in one or more parameters are referred to as parameterized types or classes.

GenericsRunn class

package com.siteinvokers;

import java.util.ArrayList;
import java.util.List;

public class GenericsRunn {

	// creating generic method
	static <X> X doubleValue(X value) {
		return value;
	}

	static <X extends List> void duplicate(X list) {
		list.addAll(list);
		System.out.println("Duplicate list : " + list);
	}
	
	public static void main(String[] args) {

		// using generic method

		ArrayList<Integer> al = new ArrayList<>(List.of(1, 2, 3, 4));
		duplicate(al);
		// O/P : Duplicate list : [1, 2, 3, 4, 1, 2, 3, 4]

		// using wild cards
		System.out.println("Sum for int is : " + sumOfNumber(List.of(1, 2, 3, 4, 5)));
		// O/P : Sum is : 15.0

		System.out.println("Sum for double is : " + sumOfNumber(List.of(1.2, 2.1, 3.2, 4.56, 5.2)));
		// O/P : Sum for double is : 16.25
		System.out.println("Sum for long is : " + sumOfNumber(List.of(1l, 2l, 3l, 4l, 6l)));
		// O/P : Sum is : 16.0


    }
    
}

Leave A Comment

Your email address will not be published. Required fields are marked *