Download Java Magazine - March/April 2015
Transcript
LISTING 12 Function<String, IntStream> first3 = word -> word.chars().limit(3); Function<String, IntStream> last3 = word -> word.chars().skip(Integer.max(0, word.length() – 4); Download all listings in this issue as text LETTER SCORE” square, this function should return zero, meaning that no bonus will be awarded. See Listing 11. We can then write our last method, which computes the full score of the best first move, as shown in Listing 12. We should also add another filter step to remove words longer than seven letters, because seven is the maximum number of letters allowed in a Scrabble word. With this new scoring function, we can see that there are two best words, jezebel and quickly, for a score of 120 points. Not bad for an opening move! Conclusion The problems we solved in this article might look like trivial game problems, but they solve many of the problems we face in general business applications: building histograms, comparing them, extracting maximum values, and handling corner cases raised by empty ORACLE.COM/JAVAMAGAZINE /////////////////////////////////////////// MARCH/APRIL 2015 streams. Using the Stream API, we can solve these problems without much code—and the solution is still efficient from a pure performance point of view. We used some of the new patterns introduced in Java SE 8, but the Iterator and the if-then-else patterns are absent from the solution. As we can see here, Java SE 8 requires us to think about the solutions for our problems differently. As a bonus, we get cleaner and more-efficient code. The Official Scrabble Players Dictionary and the words in the works of Shakespeare, as well as many other interesting data sets, can be found on Robert Sedgewick’s page “Real-World Data Sets.” </article> LEARN MORE COMMUNITY LISTING 11 JAVA IN ACTION ously computed, multiply by two (for the first-move bonus), and add 50 points if the word is seven letters long (the bonus for using all our letters in one word). Of course, the hard part is to find the special letter. The first and last three letters of a given word can be extracted with the code shown in Listing 9. These two streams hold the letter that will be double-scored. Note that if the word we consider is shorter than four letters, the streams will be empty. But that’s not a problem, because the stream.max() method returns an instance of Optional, which can be empty. How can we merge those two streams into one? We have two solutions for that: Use either the concat() method or the of() method. Because the concat() call is stateful, we might think it won’t be as efficient in parallel as of(), so let’s use of() (see Listing 10). This merged stream of letters still needs to be mapped to a stream of scores for the letters, and then the maximum score needs to be determined. Because there are words of fewer than four letters, the two streams can be empty and merging them will also return an empty stream. So we should ensure that we cope with an empty Optional correctly. If we have no letter that can be placed on the “DOUBLE LISTING 10 JAVA TECH LISTING 9 ABOUT US //java architect / blog •Stream API •Collector interface •Java 8 50