Generics in Java
When implementing a data structure such as hashmaps or arraylists, you can insert any kind of objects, but not primitive types, as the data structures are not actually storing the values but they store the memory address of references and the actual objects are not consecutive in memory.
There are some advantages of declaring generics to these datastructures.
First, we can get the error by compile time and not runtime. Second, individual type casting not needed.
Reference : https://www.geeksforgeeks.org/generics-in-java/