Skip to content

Commit 79b06fb

Browse files
committed
Initial commit.
1 parent 0af0d7b commit 79b06fb

16 files changed

+350149
-2
lines changed

.idea/$CACHE_FILE$

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/$PRODUCT_WORKSPACE_FILE$

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/.gitignore

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/stream-api-exercises-part3.iml

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,17 @@
1-
# stream-api-exercises-part3
2-
Stream API Exercises : Part III
1+
### Stream API Exercises : Part III
2+
In the following exercises you will use a dictionary file: **dictionary.txt**.
3+
4+
**Q.1)** Find the words starting with letters **a** to **m**.
5+
6+
**Q.2)** Find the words starting with the letter **n** to the end of the dictionary
7+
8+
**Q.3)** Group the dictionary words by their first three letters
9+
10+
**Q.4)** Find the palindromes in the dictionary. A palindrome is a word, number, phrase, or other
11+
sequence of characters which reads the same backward as forward, such as madam or racecar
12+
13+
**Q.5)** Count the vowels used in words.
14+
15+
**Q.6)** Find the words starting with the letter **a**, and ends with the letter **z**.
16+
17+
**Q.7)** Find the longest word in the dictionary
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.example.dictionary.exercises;
2+
3+
import java.nio.file.Files;
4+
import java.nio.file.Paths;
5+
import java.util.List;
6+
7+
/**
8+
*
9+
* @author Binnur Kurt <binnur.kurt@gmail.com>
10+
*
11+
*/
12+
public class Exercise1 {
13+
14+
public static void main(String[] args) throws Exception {
15+
final List<String> words = Files.readAllLines(Paths.get("src", "dictionary.txt"));
16+
// Find the words starting with letters a to m
17+
words.stream().takeWhile(line -> line.matches("^[a-m].*$")).forEach(System.out::println);
18+
}
19+
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.example.dictionary.exercises;
2+
3+
import java.nio.file.Files;
4+
import java.nio.file.Paths;
5+
import java.util.List;
6+
7+
/**
8+
*
9+
* @author Binnur Kurt <binnur.kurt@gmail.com>
10+
*
11+
*/
12+
public class Exercise2 {
13+
14+
public static void main(String[] args) throws Exception {
15+
final List<String> words = Files.readAllLines(Paths.get("src", "dictionary.txt"));
16+
// Find the words starting with the letter n to the end of the dictionary
17+
words.stream().dropWhile(line -> line.matches("[a-m].*$")).forEach(System.out::println);
18+
}
19+
20+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.example.dictionary.exercises;
2+
3+
import java.nio.file.Files;
4+
import java.nio.file.Paths;
5+
import java.util.List;
6+
import java.util.Map;
7+
import java.util.function.BiConsumer;
8+
import java.util.function.Function;
9+
import java.util.stream.Collector;
10+
import java.util.stream.Collectors;
11+
12+
/**
13+
*
14+
* @author Binnur Kurt <binnur.kurt@gmail.com>
15+
*
16+
*/
17+
public class Exercise3 {
18+
private static final Function<String, String> first3CharsMapper = word -> word.length() >= 3 ? word.substring(0, 3)
19+
: word.substring(0, word.length());
20+
private static final Collector<String, ?, Map<String, List<String>>> clusteredIndexCollector = Collectors
21+
.groupingBy(first3CharsMapper);
22+
private static final BiConsumer<String, List<String>> clusteredIndexPrinter = (index,list) -> {
23+
System.out.println(index + ": " + list);
24+
};
25+
26+
public static void main(String[] args) throws Exception {
27+
final List<String> words = Files.readAllLines(Paths.get("src", "dictionary.txt"));
28+
// Group the dictionary words by their first three letters
29+
Map<String, List<String>> clusteredIndex = words.stream().collect(clusteredIndexCollector);
30+
clusteredIndex.forEach( clusteredIndexPrinter);
31+
}
32+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.example.dictionary.exercises;
2+
3+
import java.nio.file.Files;
4+
import java.nio.file.Paths;
5+
import java.util.List;
6+
import java.util.function.Predicate;
7+
8+
/**
9+
*
10+
* @author Binnur Kurt <binnur.kurt@gmail.com>
11+
*
12+
*/
13+
public class Exercise4 {
14+
private static final Predicate<String> isPalindrome = word -> word.contains(new StringBuilder(word).reverse());
15+
16+
public static void main(String[] args) throws Exception {
17+
final List<String> words = Files.readAllLines(Paths.get("src", "dictionary.txt"));
18+
// Find the palindromes in the dictionary
19+
words.stream().filter(isPalindrome).forEach(System.err::println);
20+
}
21+
22+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.example.dictionary.exercises;
2+
3+
import static java.util.stream.Collectors.groupingBy;
4+
import static java.util.stream.Collectors.mapping;
5+
import static java.util.stream.Collectors.toList;
6+
7+
import java.nio.file.Files;
8+
import java.nio.file.Paths;
9+
import java.util.AbstractMap;
10+
import java.util.List;
11+
import java.util.Map.Entry;
12+
import java.util.function.BiConsumer;
13+
14+
/**
15+
*
16+
* @author Binnur Kurt <binnur.kurt@gmail.com>
17+
*
18+
*/
19+
public class Exercise5 {
20+
private static final BiConsumer<Integer, List<String>> printEntry = (key, value) -> System.out.format("%c : %d\n",
21+
key, value.size());
22+
23+
public static void main(String[] args) throws Exception {
24+
final List<String> words = Files.readAllLines(Paths.get("src", "dictionary.txt"));
25+
// Count the vowels used in words
26+
words.stream().flatMap(word -> word.chars().boxed().map(c -> pair(c, word)))
27+
.collect(groupingBy(Entry::getKey, mapping(Entry::getValue, toList()))).forEach(printEntry);
28+
}
29+
30+
private static <T, U> AbstractMap.SimpleEntry<T, U> pair(T t, U u) {
31+
return new AbstractMap.SimpleEntry<T, U>(t, u);
32+
}
33+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.example.dictionary.exercises;
2+
3+
import java.nio.file.Files;
4+
import java.nio.file.Paths;
5+
import java.util.List;
6+
7+
/**
8+
*
9+
* @author Binnur Kurt <binnur.kurt@gmail.com>
10+
*
11+
*/
12+
public class Exercise6 {
13+
14+
public static void main(String[] args) throws Exception {
15+
final List<String> words = Files.readAllLines(Paths.get("src", "dictionary.txt"));
16+
// Find the first word starting with the letter a, and ends with the letter z
17+
System.out.println(words.stream().filter(word -> word.matches("^a.*z$")).findFirst().orElse("Not found!"));
18+
// Find all the words starting with the letter a, and ends with the letter z
19+
words.stream().filter(word -> word.matches("^a.*z$")).forEach(System.out::println);
20+
}
21+
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.example.dictionary.exercises;
2+
3+
import java.nio.file.Files;
4+
import java.nio.file.Paths;
5+
import java.util.Comparator;
6+
import java.util.List;
7+
8+
/**
9+
*
10+
* @author Binnur Kurt <binnur.kurt@gmail.com>
11+
*
12+
*/
13+
public class Exercise7 {
14+
15+
public static void main(String[] args) throws Exception {
16+
final List<String> words = Files.readAllLines(Paths.get("src", "dictionary.txt"));
17+
// Find the longest word in the dictionary
18+
words.stream().sorted(Comparator.comparing(String::length).reversed()).findFirst()
19+
.ifPresent(System.out::println);
20+
}
21+
22+
}

0 commit comments

Comments
 (0)