Java Generics is one of the MOST important Java concepts.
If you understand Generics properly:
collections become safer
code becomes reusable
compile-time bugs reduce dramatically
A generic type is a generic class or interface that is parameterized over types.
What Are Generics?
Generics allow classes, interfaces, and methods to work with:
different data types safely
without rewriting code.
The Diamond
In Java SE 7 and later, you can replace the type arguments required to invoke the constructor of a generic class with an empty set of type arguments (<>) as long as the compiler can determine, or infer, the type arguments from the context. This pair of angle brackets, <>, is informally called the diamond. For example, you can create an instance of Box with the following
statement:
Box < Integer > integerBox = new Box <>();
Enter fullscreen mode
Exit fullscreen mode
Generic Class
class Box < T > { T value ; void set ( T value ) { this .…