
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Non-Generic vs Generic Collection in Java
In this article, we will learn about the collections in Java. The collections are used to store and manipulate groups of objects. Java collections can be broadly classified into two types ?
Non-generic collections allow storing objects of different types leading to runtime errors. On the other hand generic collections enforce type safety at compile-time, reducing the risk of type-related errors.
Non - Generic Collection
When the data structure is non-generic, it causes issues when the data is tried to be retrieved from the collection/data structure. Every time an element from the collection is retrieved, it needs to be explicitly type-casted to its required type, which is a problem when there are too many elements.
Example
Non-generic collections store objects without defining a specific type requiring explicit type casting when retrieving values ?
import java.util.*; public class Demo { public static void main(String[] args) { ArrayList my_list = new ArrayList(); my_list.add("Joe"); my_list.add("Rob"); my_list.add("Nate"); my_list.add("Bill"); String s1 = (String)my_list.get(0); String s2 = (String)my_list.get(1); String s3 = (String)my_list.get(3); System.out.println(s1); System.out.println(s2); System.out.println(s3); } }
Output
: Joe Rob Bill
Generic Collection
Generic collections were introduced in Java 5 as part of the Generics feature. The type-safe generic Java collection allows developers to declare the type of objects that a collection can contain. This removes the need for explicit typecasting and minimizes the chance of runtime errors.
- The errors occur at compile time instead of run time.
- Code reusability: Generics help in reusing the code already written, thereby making it usable for other types (for a method, class, or interface).
- If a data structure is generic, say a list, it would only take specific types of objects and return the same specific type of object as output. This eliminates the need to typecast individually.
- Algorithms can be implemented with ease since they can be used to work with different types of objects and maintain type safety as well as code reusability.
Example
Generic collections define a specific type eliminating the need for type casting and reducing runtime errors ?
import java.util.*; public class Demo { public static void main(String[] args) { ArrayList my_list = new ArrayList(); my_list.add("Joe"); my_list.add("Rob"); my_list.add("Nate"); my_list.add("Bill"); String s1 = (String)my_list.get(0); String s2 = (String)my_list.get(1); String s3 = (String)my_list.get(3); System.out.println(s1); System.out.println(s2); System.out.println(s3); } }
Output
Joe Rob Billy
When to Use Non-Generic vs Generic Collections
Using Generic Collections:
- When you need type safety and compile-time error checking.
- When working with modern Java applications (Java 5 and later).
- When you want to avoid explicit typecasting and improve code readability.
Use Non-Generic Collections:
- When working with legacy code that predates Java 5.
- In rare cases where you need to store objects of different types in the same collection.