3 ms·
This is a really nice write up! Java 8 is such a huge improvement over previous versions. One thing it still lacks is a better literal format for defining maps
by mark_l_watson 11y ago
This is a really nice write up!
Java 8 is such a huge improvement over previous versions. One thing it still lacks is a better literal format for defining maps, etc.
I just self published a book that uses Java 8 (https://leanpub.com/powerjava https://leanpub.com/powerjava if you will pardon a plug) and to be honest I got some pushback from a few people who like my books as to why I didn't use Clojure, Haskell, etc. To be honest I do prefer Clojure and Haskell, but the Java ecosystem is so huge, with so many good libraries that sometimes it is the more practical language to use.
- mavelikara 11y agoMark, I am sure you know this, but I am posting this for the benefit of others who might not. For literal maps, I tend to use double brace initialization style. It is more verbose than languages with direct literal support for maps and creates an extra anonymous class, but it keeps the map in a single syntactic construct. For lists, I use Arrays.asList(a,b,c) style; if you static import the method it reads nicely as-list-a-b-c.
- pjmlp 11y agoWhich is a no-go in Android as it can prevent GC in some cases, because as anonymous inner class it holds a reference to its parent and you end up leaking Activities. It is usually presented as anti-pattern in talks about Android performance.
- mavelikara 11y agoAh, I have never programed for Android devices. Thanks for pointing this out.
- stickfigure 11y agoGuava has some tools that make map literals a little prettier: Map<Foo, Bar> map = ImmutableMap.of(foo1, bar1, foo2, bar2, foo3, bar3, foo4, bar4); There are overloads for up to 5 entries, and they're even key/value typesafe.