Java Generics Multiple Type Parameters Example

Introduction

Java Generics allow you to define classes, interfaces, and methods with multiple type parameters. This provides greater flexibility and reusability, enabling you to work with multiple types while maintaining type safety. In this guide, we'll explore how to define and use generics with multiple type parameters, accompanied by examples.

Table of Contents

  1. What are Multiple Type Parameters?
  2. Defining a Generic Class with Multiple Type Parameters
  3. Example: Generic Class with Multiple Type Parameters
  4. Defining a Generic Method with Multiple Type Parameters
  5. Example: Generic Method with Multiple Type Parameters
  6. Conclusion

1. What are Multiple Type Parameters?

Multiple type parameters allow you to define a class, interface, or method with more than one type parameter. Each type parameter is specified within the angle brackets (<>), separated by commas.

2. Defining a Generic Class with Multiple Type Parameters

A generic class with multiple type parameters is defined by placing the type parameters inside angle brackets (<>) after the class name.

Syntax:

class ClassName<T, U> {
    // Class body
}

3. Example: Generic Class with Multiple Type Parameters

Example:

// Defining a generic class with multiple type parameters
class Pair<K, V> {
    private K key;
    private V value;

    public Pair(K key, V value) {
        this.key = key;
        this.value = value;
    }

    public K getKey() {
        return key;
    }

    public V getValue() {
        return value;
    }

    public void setKey(K key) {
        this.key = key;
    }

    public void setValue(V value) {
        this.value = value;
    }
}

public class MultipleTypeParametersExample {
    public static void main(String[] args) {
        // Creating an instance of a generic class with Integer and String types
        Pair<Integer, String> intStringPair = new Pair<>(1, "One");
        System.out.println("Key: " + intStringPair.getKey() + ", Value: " + intStringPair.getValue());

        // Creating an instance of a generic class with String and Double types
        Pair<String, Double> stringDoublePair = new Pair<>("PI", 3.14);
        System.out.println("Key: " + stringDoublePair.getKey() + ", Value: " + stringDoublePair.getValue());
    }
}

Output:

Key: 1, Value: One
Key: PI, Value: 3.14

Explanation:

  • The Pair class is defined with two type parameters K and V.
  • The key and value fields, along with their getters and setters, use these type parameters.
  • Instances of Pair are created with different types, demonstrating the flexibility of using multiple type parameters.

4. Defining a Generic Method with Multiple Type Parameters

A generic method with multiple type parameters is defined by placing the type parameters inside angle brackets (<>) before the method's return type.

Syntax:

public <T, U> void methodName(T param1, U param2) {
    // method body
}

5. Example: Generic Method with Multiple Type Parameters

Example:

public class GenericMethodExample {
    // Generic method with multiple type parameters
    public static <T, U> void printPair(T key, U value) {
        System.out.println("Key: " + key + ", Value: " + value);
    }

    public static void main(String[] args) {
        printPair(1, "One");                // Output: Key: 1, Value: One
        printPair("PI", 3.14);              // Output: Key: PI, Value: 3.14
        printPair(2.718, "Euler's Number"); // Output: Key: 2.718, Value: Euler's Number
    }
}

Output:

Key: 1, Value: One
Key: PI, Value: 3.14
Key: 2.718, Value: Euler's Number

Explanation:

  • The printPair method is defined with two type parameters T and U.
  • The method prints the key-value pair, demonstrating the use of multiple type parameters in a method.

6. Conclusion

Using multiple type parameters in Java Generics allows you to create more flexible and reusable classes and methods. By defining multiple type parameters, you can work with different types in a type-safe manner, reducing the need for type casting and preventing runtime errors.

In this guide, we've covered how to define and use generic classes and methods with multiple type parameters, along with examples to illustrate their usage. Understanding and utilizing multiple type parameters can significantly enhance the robustness and maintainability of your Java applications.

Happy coding!

Comments