There are numerous advantages because Collections can be Generic. As you know Generics help is removing the duplicate code and provide compile time safety for the type of objects used for calling various methods.
A simple program for creating a list of friends with out using Generics will look like this.
List friends = new ArrayList(); ClassCastException s.
Instead if we used Generic Collections, then we can do the same program with out having to upcast the object. List<String> friends = new ArrayList<String>(); So it is suggested, we use the Generic Collections where ever possible. It is extremely rare to use any Collection like List, Set or Map without mentioning the type of the collection as Generic. Some examples of the declaration of generics are List<Integer> fibonacciNumbers = new ArrayList<Integer>();
|