Non-generic Vs Generic Collection in Java
Last Updated :
21 Feb, 2023
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
Implementation:
Example 1
Java
import java.util.*;
class GFG {
public static void main(String[] args)
{
ArrayList al = new ArrayList();
al.add( "Sachin" );
al.add( "Rahul" );
al.add( 10 );
String s1 = (String)al.get( 0 );
String s2 = (String)al.get( 1 );
try {
String s3 = (String)al.get( 2 );
}
catch (Exception e) {
System.out.println( "Exception: " + e);
}
}
}
|
Output:
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.
Example 2
Java
import java.util.*;
class GFG {
public static void main(String[] args)
{
ArrayList<String> al = new ArrayList<String>();
al.add( "Sachin" );
al.add( "Rahul" );
al.add( 10 );
String s1 = al.get( 0 );
String s2 = al.get( 1 );
String s3 = al.get( 2 );
}
}
|
Output:
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.
Example 3
Java
import java.util.*;
class GFG {
public static void main(String[] args) {
ArrayList al = new ArrayList();
al.add( "Sachin" );
al.add( "Rahul" );
String s1 = (String)al.get( 0 );
String s2 = (String)al.get( 1 );
}
}
|
Output:

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
Example 4
Java
import java.util.*;
class Test {
public static void main(String[] args)
{
ArrayList<String> al = new ArrayList<String>();
al.add( "Sachin" );
al.add( "Rahul" );
String s1 = al.get( 0 );
String s2 = al.get( 1 );
System.out.print(al);
}
}
|
Note:
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. |
Similar Reads
Need of Concurrent Collections in java
As we already know Collections which is nothing but collections of Objects where we deals with the Objects using some pre-defined methods. But There are several problems which occurs when we use Collections concept in multi-threading. The problems which occurs while using Collections in Multi-thread
2 min read
Iterator vs Collection in Java
Iterator and Collection, both has helped and comforted the programmers at many a times. But their usage and application has a very wide difference. 1. Iterator Declaration public interface Iterator Type Parameters: E - the type of elements returned by this iteratorIterators are used in Collection fr
3 min read
Collection vs Collections in Java with Example
Collection: Collection is a interface present in java.util package. It is used to represent a group of individual objects as a single unit. It is similar to the container in the C++ language. The collection is considered as the root interface of the collection framework. It provides several classes
3 min read
Why We Need Collection Framework in Java?
A framework is a set of classes and interfaces which provide a ready-made architecture. In order to implement a new feature or a class, there is no need to define a framework. However, an optimal object-oriented design always includes a framework with a collection of classes such that all the classe
3 min read
Generic For Loop in Java
When we know that we have to iterate over a whole set or list, then we can use Generic For Loop. Java's Generic has a new loop called for-each loop. It is also called enhanced for loop. This for-each loop makes it easier to iterate over array or generic Collection classes. In normal for loop, we wri
4 min read
Templates in C++ vs Generics in Java
While building large-scale projects, we need the code to be compatible with any kind of data which is provided to it. That is the place where your written code stands above that of others. Here what we meant is to make the code you write be generic to any kind of data provided to the program regardl
4 min read
Convert an Iterable to Collection in Java
Iterable and Collection have served to be of great use in Java. Iterators are used in Collection framework in Java to retrieve elements one by one and a Collection is a group of individual objects represented as a single unit. Java provides Collection Framework which defines several classes and inte
4 min read
Collection contains() method in Java with Examples
The contains(Object element) of java.util.Collection interface is used to check whether the element 'element' exists in this collection. This method returns a boolean value depicting the presence of the element. If the element is present, it returns true, else it returns false. Syntax: Collection.co
3 min read
C++ STL vs Java Collections Framework
Java and C++ provide their different functionalities and their use cases. When it comes to Data structures and Algorithms Java has a special framework named Java collection framework and C++ provides a library called STL.So let's have a look at what are the different functions, complexities, and wha
3 min read
Collections.sort() in Java with Examples
java.util.Collections.sort() method is present in java.util.Collections class. It is used to sort the elements present in the specified list of Collection in ascending order. It works similar to java.util.Arrays.sort() method but it is better than as it can sort the elements of Array as well as link
5 min read