|
| 1 | +package org.ecabrerar.examples.java8.mooc.lesson2; |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
| 5 | + * |
| 6 | + * JDK 8 MOOC Lesson 2 homework |
| 7 | + */ |
| 8 | +//package lesson2; |
| 9 | + |
| 10 | +import java.io.BufferedReader; |
| 11 | +import java.io.IOException; |
| 12 | +import java.nio.charset.StandardCharsets; |
| 13 | +import java.nio.file.Files; |
| 14 | +import java.nio.file.Paths; |
| 15 | +import java.util.Arrays; |
| 16 | +import java.util.List; |
| 17 | +import java.util.stream.Collectors; |
| 18 | +import java.util.stream.Stream; |
| 19 | + |
| 20 | +/** |
| 21 | + * @author Speakjava ([email protected]) |
| 22 | + */ |
| 23 | +public class SolutionsLesson2 { |
| 24 | + private static final String WORD_REGEXP = "[- .:,]+"; |
| 25 | + |
| 26 | + /** |
| 27 | + * Run the exercises to ensure we got the right answers |
| 28 | + * |
| 29 | + * @throws java.io.IOException |
| 30 | + */ |
| 31 | + public void runExercises() throws IOException { |
| 32 | + System.out.println("JDK 8 Lambdas and Streams MOOC Lesson 2"); |
| 33 | + System.out.println("Running exercise 1 solution..."); |
| 34 | + exercise1(); |
| 35 | + System.out.println("Running exercise 2 solution..."); |
| 36 | + exercise2(); |
| 37 | + System.out.println("Running exercise 3 solution..."); |
| 38 | + exercise3(); |
| 39 | + System.out.println("Running exercise 4 solution..."); |
| 40 | + exercise4(); |
| 41 | + System.out.println("Running exercise 5 solution..."); |
| 42 | + exercise5(); |
| 43 | + System.out.println("Running exercise 6 solution..."); |
| 44 | + exercise6(); |
| 45 | + System.out.println("Running exercise 7 solution..."); |
| 46 | + exercise7(); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Exercise 1 |
| 51 | + * |
| 52 | + * Create a new list with all the strings from original list converted to |
| 53 | + * lower case and print them out. |
| 54 | + */ |
| 55 | + private void exercise1() { |
| 56 | + List<String> list = Arrays.asList( |
| 57 | + "The", "Quick", "BROWN", "Fox", "Jumped", "Over", "The", "LAZY", "DOG"); |
| 58 | + |
| 59 | + /* YOUR CODE HERE */ |
| 60 | + |
| 61 | + List<String> newList = list |
| 62 | + .stream() |
| 63 | + .map(String::toLowerCase) |
| 64 | + .collect(Collectors.toList()); |
| 65 | + |
| 66 | + |
| 67 | + newList.forEach(System.out::println); |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Exercise 2 |
| 75 | + * |
| 76 | + * Modify exercise 1 so that the new list only contains strings that have an |
| 77 | + * odd length |
| 78 | + */ |
| 79 | + private void exercise2() { |
| 80 | + List<String> list = Arrays.asList( |
| 81 | + "The", "Quick", "BROWN", "Fox", "Jumped", "Over", "The", "LAZY", "DOG"); |
| 82 | + |
| 83 | + |
| 84 | + List<String> newList = list |
| 85 | + .stream() |
| 86 | + .filter(s -> (s.length() % 2)==1) |
| 87 | + .map(String::toLowerCase) |
| 88 | + .collect(Collectors.toList()); |
| 89 | + |
| 90 | + |
| 91 | + newList.forEach(System.out::println); |
| 92 | + |
| 93 | + /* YOUR CODE HERE */ |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Exercise 3 |
| 98 | + * |
| 99 | + * Join the second, third and forth strings of the list into a single string, |
| 100 | + * where each word is separated by a hyphen (-). Print the resulting string. |
| 101 | + */ |
| 102 | + private void exercise3() { |
| 103 | + List<String> list = Arrays.asList( |
| 104 | + "The", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog"); |
| 105 | + |
| 106 | + /* YOUR CODE HERE */ |
| 107 | + String result = list |
| 108 | + .stream() |
| 109 | + .skip(1) |
| 110 | + .limit(3) |
| 111 | + .collect(Collectors.joining("-")); |
| 112 | + |
| 113 | + System.out.println(result); |
| 114 | + |
| 115 | + |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Count the number of lines in the file using the BufferedReader provided |
| 120 | + */ |
| 121 | + private void exercise4() throws IOException { |
| 122 | + try (BufferedReader reader = Files.newBufferedReader( |
| 123 | + Paths.get("SonnetI.txt"), StandardCharsets.UTF_8)) { |
| 124 | + /* YOUR CODE HERE */ |
| 125 | + |
| 126 | + long totalLines = reader.lines().count(); |
| 127 | + |
| 128 | + System.out.println("Number of lines = " + totalLines); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Using the BufferedReader to access the file, create a list of words with |
| 134 | + * no duplicates contained in the file. Print the words. |
| 135 | + * |
| 136 | + * HINT: A regular expression, WORD_REGEXP, is already defined for your use. |
| 137 | + */ |
| 138 | + private void exercise5() throws IOException { |
| 139 | + try (BufferedReader reader = Files.newBufferedReader( |
| 140 | + Paths.get("SonnetI.txt"), StandardCharsets.UTF_8)) { |
| 141 | + /* YOUR CODE HERE */ |
| 142 | + |
| 143 | + List<String> uniqueWords = reader |
| 144 | + .lines() |
| 145 | + .flatMap(line -> Stream.of(line.split(WORD_REGEXP))) |
| 146 | + .distinct() |
| 147 | + .collect(Collectors.toList()); |
| 148 | + |
| 149 | + uniqueWords.stream().forEach(System.out::println); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Using the BufferedReader to access the file create a list of words from |
| 155 | + * the file, converted to lower-case and with duplicates removed, which is |
| 156 | + * sorted by natural order. Print the contents of the list. |
| 157 | + */ |
| 158 | + private void exercise6() throws IOException { |
| 159 | + try (BufferedReader reader = Files.newBufferedReader( |
| 160 | + Paths.get("SonnetI.txt"), StandardCharsets.UTF_8)) { |
| 161 | + /* YOUR CODE HERE */ |
| 162 | + |
| 163 | + List<String> words = reader.lines() |
| 164 | + .flatMap(line -> Stream.of(line.split(WORD_REGEXP))) |
| 165 | + .map(String::toLowerCase) |
| 166 | + .sorted() |
| 167 | + .distinct() |
| 168 | + .collect(Collectors.toList()); |
| 169 | + |
| 170 | + words.stream().forEach(System.out::println); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Modify exercise6 so that the words are sorted by length |
| 176 | + */ |
| 177 | + private void exercise7() throws IOException { |
| 178 | + try (BufferedReader reader = Files.newBufferedReader( |
| 179 | + Paths.get("SonnetI.txt"), StandardCharsets.UTF_8)) { |
| 180 | + /* YOUR CODE HERE */ |
| 181 | + |
| 182 | + |
| 183 | + List<String> words = reader.lines() |
| 184 | + .flatMap(line -> Stream.of(line.split(WORD_REGEXP))) |
| 185 | + .map(String::toLowerCase) |
| 186 | + .sorted() |
| 187 | + .distinct() |
| 188 | + .collect(Collectors.toList()); |
| 189 | + |
| 190 | + words.stream().forEach(System.out::println); |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + /** |
| 195 | + * Main entry point for application |
| 196 | + * |
| 197 | + * @param args the command line arguments |
| 198 | + * @throws IOException If file access does not work |
| 199 | + */ |
| 200 | + public static void main(String[] args) throws IOException { |
| 201 | + SolutionsLesson2 lesson = new SolutionsLesson2(); |
| 202 | + lesson.runExercises(); |
| 203 | + } |
| 204 | +} |
| 205 | + |
0 commit comments