Multiple Parameters In Generic
A generic class or method can have more than one type parameter. This allows you to store, relate, or operate on multiple different types without losing type safety.
Syntax
class ClassName<T, U> {
// T and U are type parameters
}

Example Generic Pair Class
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 class Main {
public static void main(String[] args) {
Pair<String, Integer> student=new Pair<>("John", 25);
System.out.println("Name: "+student.getKey());
System.out.println("Age: "+student.getValue());
Pair<Integer, String> product = new Pair<>(101, "Laptop");
System.out.println("Product ID: "+product.getKey());
System.out.println("Product Name: "+product.getValue());
}
}
Output:
Name: John
Age: 25
Product ID: 101
Product Name: Laptop
🔹 Real-World Uses
Map.Entry<K, V> → Stores key-value pairs in a HashMap.
Generic Response Classes in APIs → Example: Response<Status, Data>.
Tuples or Pairs → Returning multiple values from a method.
