Live data from Hacker News

Java: Missing Features

infoq.com

51–54 of 54 posts

Re: Java: Missing Features

#51
post #3

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();

Yes, that's the most orthodox Java solution to this.

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.

Re: Java: Missing Features

#52

Earlier quoted context omitted.

If there's no logic in the getter and setters, just make the properties public.

Huh? Then they're just fields and not properties, maybe I'm misunderstanding what you're trying to say. And there are numerous reasons that Properties are preferable to Fields. Unless someone else can help me, I think this is really bad advice for a C# developer.

>Huh? Then they're just fields and not properties

Nomenclature schmomenclature.

They're variables with object scope.

Re: Java: Missing Features

#53

Earlier quoted context omitted.

If there's no logic in the getter and setters, just make the properties public.

Huh? Then they're just fields and not properties, maybe I'm misunderstanding what you're trying to say. And there are numerous reasons that Properties are preferable to Fields. Unless someone else can help me, I think this is really bad advice for a C# developer.

>Huh? Then they're just fields and not properties

Nomenclature schmomenclature.

They're variables with object scope.

Re: Java: Missing Features

#54

Earlier quoted context omitted.

If there's no logic in the getter and setters, just make the properties public.

Huh? Then they're just fields and not properties, maybe I'm misunderstanding what you're trying to say. And there are numerous reasons that Properties are preferable to Fields. Unless someone else can help me, I think this is really bad advice for a C# developer.

>Huh? Then they're just fields and not properties

Nomenclature schmomenclature.

They're variables with object scope.

Post reply on HN