4 ms·
Syntactic Sugar: Collection literals List<String> people = {"Frank", "Mary", "Satan"}; I find it breathtaking this isn't core yet. I was writing test fixture s
by mmahemoff 15y ago
Syntactic Sugar: Collection literals
List<String> people = {"Frank", "Mary", "Satan"};
I find it breathtaking this isn't core yet. I was writing test fixture support code for 1.4 to easily and DRYly generate lists of up to 10 items, which required 10 copy-paste functions each with a different number of arguments. 1.5 came along and made it possible to easily do it with one function thanks to varargs, but it's 6 years later and still no collection literals!
- rkalla 15y agoI don't think anyone would disagree with you; we definitely went through a stagnation of Java between 6 and 7 where it seemed Sun was trying to find it's footing with Java, trying to not sink the company, trying to be bought up, trying to figure out where Netbeans fit in the scheme of everything, etc. etc. It is like the Java world hit the 'pause' button on really ambitious stuff for a few years and the only things that got into releases were the laser-sharp-focused features that the teams needed no variation on. They knew exactly what the feature was, where to take it and when to ship it. I think it was for the better, because now there is all this pent up energy getting reinjected into the platform and we'll see 8 next year with the rest of all these goodies and hopefully 9/10 2 years after that. Pretty good recovery IMO. At least it didn't languish and die a horrible death or something.
- deleted 15y ago[deleted]
- rtg 15y agoIf you use the Google library [1], this becomes a bit more convenient that the built-in Java way: List<String> people = ImmutableList.of("Frank", "Mary", "Bob"); There's also ImmutableSet and many other goodies, to say the least. For instantiation of these generic types, you can do: List<String> people = Lists.newArrayList(); Map<String,Integer> scores = Maps.newHashMap(); and so on. [1] http://code.google.com/p/guava-libraries/ http://code.google.com/p/guava-libraries/
- markelliot 15y agoSince 1.5 you can use the `Arrays` built-in [1]: List<String> people = Arrays.asList("Frank", "Mary", "Bob"); [1] http://download.oracle.com/javase/1.5.0/docs/api/java/util/Arrays.html http://download.oracle.com/javase/1.5.0/docs/api/java/util/A...
- smcj 15y agoWhat's wrong with List.of(...)? I think adding special syntax rules is a pretty bad idea.