Some of these, like reified generics, would be great, and it's sad it's too late to add them to the JVM. Some of these, like more expressive imports, seem pretty pointless given that we use IDE's. But some of these are quite un-Javaish. Structural typing would really go against the grain of the language, and i have a very hard time believing it would actually be useful. What method name is currently widely used with…
FYI Guava gives you those constructors, the map is: ImmutableMap.of(Key, Value, Key, Value...); For ones with more than 4 values you will need: ImmutableMap WORD_TO_INT = new ImmutableMap.Builder () .put("one", 1) .put("two", 2) .put("three", 3) .build();
It's not hard to do slightly better (IMHO!), though:
public class MapBuilder {
public static MapBuilder with(K key, V value) {
return new MapBuilder().and(key, value);
}
private List> entries = new ArrayList();
public MapBuilder and(K key, V value) {
entries.add(new AbstractMap.SimpleEntry(key, value));
return this;
}
public Map build(Map map) {
for (Map.Entry entry : entries) {
map.put(entry.getKey(), entry.getValue());
}
return map;
}
public Map build(Supplier> mapSupplier) {
return build(mapSupplier.get());
}
}
Which lets you write: Map map = with("Java", true).and("Go", false).build(new HashMap());
Map map2 = with("Java", true).and("Go", false).build(Map::new);
I don't have Java 8 on the machine i'm on right now, so apologies if the second example doesn't compile; it might need more manifest types on it.