Usage of generic collection in java

Usage of generic collection in java

The usage of generic collections in Java is primarily aimed at achieving type safety, code reusability, and improved readability and maintainability. Generics allow you to define classes, interfaces, and methods with type parameters, which makes it possible to create data structures and algorithms that can work with any object type while ensuring that type-related errors are caught at compile time.

Usage of generic collection in java

Key Usages of Generic Collections

1. Type Safety

   By using generic collections, you can specify the type of elements that the collection can hold, which prevents accidental addition of incompatible types and catches type-related errors at compile time.

2. Code Reusability

   Generic collections enable the creation of reusable components. For example, you can write a single class or method that can handle different types of data without the need to duplicate code for each type.

3. Improved Code Readability

   Generics make the code more readable by clearly specifying the type of elements that a collection can hold. This helps other developers understand the expected type of elements in the collection.

4. Elimination of Type Casting

   Generics eliminate the need for explicit type casting when retrieving elements from a collection, reducing the risk of ClassCastException at runtime.

5. Enhanced Maintainability

   Generic code is easier to maintain because type-related errors are caught early, and changes in the type of elements do not require changes in the collection code.

Example
java
import java.util.ArrayList;
import java.util.List;

// A generic class that can hold any type of data
class DataHolder<T> {
    private T data;

    public DataHolder(T data) {
        this.data = data;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}

public class GenericCollectionUsage {
    // A generic method to print elements of a list
    public static <T> void printList(List<T> list) {
        for (T item : list) {
            System.out.println(item);
        }
    }

    public static void main(String[] args) {
        // Using a generic collection to store strings
        List<String> stringList = new ArrayList<>();
        stringList.add("Hello");
        stringList.add("World");

        // Using a generic collection to store integers
        List<Integer> integerList = new ArrayList<>();
        integerList.add(10);
        integerList.add(20);

        // Using the generic DataHolder class to store different types of data
        DataHolder<String> stringHolder = new DataHolder<>("Sample String");
        DataHolder<Integer> integerHolder = new DataHolder<>(100);

        // Printing the data held by DataHolder instances
        System.out.println("String DataHolder: " + stringHolder.getData());
        System.out.println("Integer DataHolder: " + integerHolder.getData());

        // Printing elements of the lists using a generic method
        System.out.println("\nElements of stringList:");
        printList(stringList);

        System.out.println("\nElements of integerList:");
        printList(integerList);
    }
}

Usage of generic collection in java example

  • We create a generic class DataHolder that can hold any type of data specified by the type parameter.
  • We define a generic method printList() that can print elements of a list of any type.
  • We create two generic collections: one to store strings (stringList) and another to store integers (integerList).
  • We create instances of the DataHolder class to hold a string and an integer.
  • We use the printList() method to print elements of both the string list and the integer list.

This example Usage of generic collection demonstrates how generics can be used to achieve type safety, code reusability, and improved readability and maintainability in Java.

Homepage

Readmore