Non-generic Vs Generic Collection in Java
We will be discussing differences later prior let us understand what is generic Collection and non-generic Collection, and most importantly dealing with the implementation part as during implementation one can only really get the real understanding of the concept, henceforth the differences between them.
Generics are basically the errors appearing are compile-time than at run-time. there are certain advantages of generics over non-generic are as follows:
- Code Reuse: With help of Generics, one needs to write a method/class/interface only once and use it for any type whereas, in non-generics, the code needs to be written again and again whenever needed.
- Type Safety: Generics make errors to appear compile time than at run time (It’s always better to know problems in your code at compile time rather than making your code fail at run time).
Example: To create an ArrayList that store name of students and if by mistake programmer adds an integer object instead of string, the compiler allows it. But, when this data is retrieved from ArrayList, it causes problems at runtime for Non-generic ArrayList
prog.java:19: warning: [unchecked] unchecked call to add(E) as a member of the raw type ArrayList al.add("Sachin"); ^ where E is a type-variable: E extends Object declared in class ArrayList prog.java:20: warning: [unchecked] unchecked call to add(E) as a member of the raw type ArrayList al.add("Rahul"); ^ where E is a type-variable: E extends Object declared in class ArrayList prog.java:23: warning: [unchecked] unchecked call to add(E) as a member of the raw type ArrayList al.add(10); ^ where E is a type-variable: E extends Object declared in class ArrayList 3 warnings
How generics solve this problem?
If this list was made Generic, then it would take only String objects and threw Compile Time Error in any other case.
prog.java:24: error: incompatible types: int cannot be converted to String al.add(10); ^ Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output 1 error
Now moving forward, Individual Type Casting is not needed.
If Generics is not needed, then, in the above example every time the data is to be retrieved from ArrayList, it needs to be typecasted. Typecasting at every retrieval operation is a big headache. This can be avoided if somehow it is already known that the list only holds string data.
Geek, now you should be wondering how generics solve this problem?
If this list was made Generic, then it would take only String objects and would return only String objects while retrieval. And hence individual typecasting won’t be required. The above statement is justified
With the help of generics, while one can implement algorithms Implementing generic algorithms, one can have that work on different types of objects and at the same they are type-safe too.
Do remember that there are some points, which will describe the difference between Generics and Non-Generic which are tabulated below in order to get a crisp understanding between them.
|Base||Non-generic Collection||Generic Collection|
|Syntax||ArrayList list = new ArrayList();||ArrayList<ReferenceType> list = new ArrayList<ReferenceType>();|
|Type-safety||Can hold any type of data. Hence not type-safe.||Can hold only the defined type of data. Hence type safe.|
|Typecasting||Individual type casting needs to be done at every retrieval.||No typecasting is needed.|
|Compile-Time Checking||Checked for type safety at runtime.||Checked for type safety at Compile-time.|