4 ms·
yes, Java 8 adds a bunch of methods to ArrayList and HashMap that makes the code a little more high level. There is also another variation, using map.getOrDefa
by _old_dude_ 8y ago
yes, Java 8 adds a bunch of methods to ArrayList and HashMap that makes the code a little more high level.
There is also another variation, using map.getOrDefault()
words.forEach(word -> {
map.put(word, map.getOrDefault(word, 0) + 1);
});
and then at some points you discover the Stream API
var map = words.stream().collect(Collectors.groupingBy(w -> w, Collectors.counting()));
- rienbdj 8y agoAnyone else disappointed by the Java implementation of these ideas? It feels less ergonomic than previous solutions and came considerably later. That said, it is better than nothing.
- based2 8y agoJava is changing: https://www.youtube.com/watch?v=gIrKP76iuPs https://www.youtube.com/watch?v=gIrKP76iuPs "Venkat Subramaniam — Design Patterns in the Light of Lambda Expressions" And you can use Scala or Clojure with Java APIs/Libraries/Frameworks https://www.scala-lang.org/ https://www.scala-lang.org/ https://typelevel.org/cats/ https://typelevel.org/cats/ http://dotty.epfl.ch/ http://dotty.epfl.ch/ https://clojure.org/ https://clojure.org/ https://clojure.org/guides/spec https://clojure.org/guides/spec https://github.com/clojure/spec-alpha2/wiki/Schema-and-select https://github.com/clojure/spec-alpha2/wiki/Schema-and-selec...
- vbsteven 8y agoThe streams api is a little verbose but this is mainly because of Java’s backwards compatibility policy. They want older code to still compile and run on new JDKs. Default methods on interfaces help to add features to existing collections but they are limited in what they can do, hence streams. If you really want more ergonomic collections while staying close to Java then use Kotlin which can be easily added to most Java code bases and has all the modern functional methods like map/reduce/filter/group on standard collections.
- jnordwick 8y ago> modern functional methods like map/reduce/filter/group "Modern". Lol.
- vbsteven 8y agoI know these functional concepts are far from new. I use "modern" in this context because other contemporary languages like Python/Ruby/Scala/Rust etc all have versions of those and FP-style collection usage using these names has become popular in the last 10 years.
- _old_dude_ 8y agoIt's also to separate the methods doing computation lazily (Stream) from the one doing computation eagerly (Collection). Otherwise a simple call like isEmpty() can trigger the whole computation like in Haskell. As a user of the API, you have to wrap your code into a Stream to be lazy evaluated, the Stream being a computation monad, so you always know if a code is evaluated eagerly or lazily.
- wcarss 8y agoin python: >>> from collections import defaultdict >>> map = defaultdict(int) >>> for word in ['hi', 'there', 'hi']: ... map[word] += 1 I am glad Java has this stuff now, but it's a little surprising that its tools are still so obtuse relative to competitors. edit: I should have foreseen the derailment caused by and nitpicking to follow a comment like this. Sorry folks!
- kbsletten 8y agoTo be fair, getValueOrDefault is strictly more powerful than defaultdict because you can vary the initial value by the key.
- detaro 8y agodict.get() does the same in Python (optional second parameter for a default value). But I've wondered too why there isn't a form of the default dict that can take parameters like the key for the factory function.
- nayuki 8y agoFunny you say that, because I find the key-sorted TreeMap to be useful in Java, but Python seems to have no equivalent.