/var/logmarcus chiu

/var/log

❯

Computer

❯

Computer/Programming Languages

❯

Computer Languages - General-Purpose Programming Languages (GPL)

❯

Java Platform

❯

Java

❯

Java - Projects & Code Examples

❯

Java - Native Libraries

❯

Java - (? extends T) vs (? super T)

Java - When to Use Foo<?>

Created on Feb 08, 2022

Heterogenous Tuple of Infinite Size

One example use case of Foo<?> is an implementation of a Tuple of infinite size storing different object types

public class Favorites {
	private Map<Class<?>, Object> favorites = new HashMap<>();

	public <T> void putFavorite(Class<T> type, T instance) {
		Assert.assertNotNull(instance);
		favorites.put(type, instance);
	}

	public <T> T getFavorite(Class<T> type) {
		return type.cast(favorites.get(type));
	}
}