Flat Preloader Icon

Java Generics

Overview

  • Java Generic Features
  • Generic List in Java
  • Generic Map in Java
  • Java Generic Classes & Methods
  • Java Generic For Loop
  • Generic WildCard

Java Generics

  • Java Generics was introduced in Java 5 to deal with type-safe objects. Using generics a programmer can force to store a specific type of objects in collection
  • E.g. List list = new ArrayList<>();
  • Advantages are :-
    • Type Safety:- Only one specific type of Objects are allowed to be stored in collection.
    • Type Casting not required.
    • Compile time verification.

Generic List in Java

  • Compile time verification.
  • E.g
    • List l = new ArrayList<>();
    • List l2= new ArrayList<>();
  • Demo to see a generic List and how to iterate over it.

Generic Classes

  • You can define generic classes by using angle brackets and a type parameter.
  • Example of a generic class:
				
					public class Box<T> {
    private T value;

    public Box(T value) {
        this.value = value;
    }

    public T getValue() {
        return value;
    }
}

				
			

Generic Map in Java

  • Following is a way to create a generic map.
  • Map myMap = new HashMap<>();
  • Generic Iterators could be created for keys and values.
  • E.g. Iterator keyIt= myMap.keySet().iterator();

Java Generic Classes & Methods

  • Even the classes & methods we define can be generified.
  • Demo for creating and using generic classes & methods

Generic WildCard

  • Unknown wildcard a.k.a. unbounded e.g. List<?> elements.
  • Extends wildcard a.k.a. Upper Bounded e.g. List <? extends superClass>
  • Super wildcard a.k.a. Lower Bounded e.g. List <? super superClass>

Type Inference

  • Java’s type inference allows you to omit type parameters in certain cases, and the compiler will infer them based on context.
  • Example:
				
					List<String> names = new ArrayList<>();
// Type inference for ArrayList<String>

				
			
Generics are a powerful feature in Java that help improve type safety and code reusability. They are widely used in Java libraries and frameworks to provide flexible and generic solutions for various data structures and algorithms.

Set Interface

  • Set is a collection of unique objects and it does not allow duplicates
  • HashSet implementation uses hashtable for storing these objects
  • LinkedHashSet implementation uses linked list for storing these objects. Insertion/Delete thus, are fast here.
  • TreeSet implementation uses a tree for storing these objects in sorted order. Hence searching is fastest here.

Map Interface

  • Map contains the objects as key-value pair.
  • The keys in the Map are unique, but the values could be duplicate
  • Some of the methods are put(Object,Object), get(Object), remove(Object), keySet(), entrySet() etc.
  • Entry is a inner class of map and is accessed via Map.Entry and has methods, getKey() and getValue().

Map Interface(Continued…)

  • HashMap implementation can have one null key and it maintains no order of the elements stored.
  • HashTable also an implementation of Map, does not allow null key values and is synchronised.
  • LinkedHashMap also acts like a HashMap, but it maintains the natural insertion order of elements.
  • TreeMap cannot have null key and it maintains the elements in ascending order

Queue Interface

  • Queue interface typically, but not necessarily keeps the elements in the FIFO order.
  • Some of the methods of this interface are add(Object), remove(), poll(), peek() etc.
  • A typical implementation of the Queue is a LinkedList and PriorityQueue(this maintains the elements in natural order).