public class Box<T> {
private T value;
public Box(T value) { this.value = value; }
public T get() { return value; }
public void set(T value) { this.value = value; }
public <U> Box<U> map(java.util.function.Function<T, U> f) {
return new Box<>(f.apply(value));
}
}
void demo() {
Box<String> sb = new Box<>("hello");
System.out.println(sb.get()); // hello
Box<Integer> ib = sb.map(String::length);
System.out.println(ib.get()); // 5
// Diamond operator — let the compiler infer
Box<java.util.List<String>> lb = new Box<>(java.util.List.of("a", "b"));
System.out.println(lb.get());
}
Create a free account and build your private vault. Share publicly whenever you want.