// Created on savesnippets.com · https://savesnippets.com/XMefH0YLrCB6Hj public class Box { private T value; public Box(T value) { this.value = value; } public T get() { return value; } public void set(T value) { this.value = value; } public Box map(java.util.function.Function f) { return new Box<>(f.apply(value)); } } void demo() { Box sb = new Box<>("hello"); System.out.println(sb.get()); // hello Box ib = sb.map(String::length); System.out.println(ib.get()); // 5 // Diamond operator — let the compiler infer Box> lb = new Box<>(java.util.List.of("a", "b")); System.out.println(lb.get()); }