import java.util.*;
// Generic class
public class Box<T> {
private T value;
public void setValue(T value) {
this.value = value;
}
public T getValue() {
return value;
}
// Generic method
public <U> void printValue(U value) {
System.out.println("Value: " + value);
}
}
// Bounded type parameter
class NumberBox<T extends Number> {
private T number;
public double getDoubleValue() {
return number.doubleValue();
}
}
// 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; }
}
// Generic method with wildcard
class Utils {
public static void printList(List<?> list) {
for (Object item : list) {
System.out.println(item);
}
}
// Upper bounded wildcard
public static double sum(List<? extends Number> numbers) {
double sum = 0.0;
for (Number n : numbers) {
sum += n.doubleValue();
}
return sum;
}
// Lower bounded wildcard
public static void addNumbers(List<? super Integer> list) {
list.add(1);
list.add(2);
}
}