Skip to content

Commit b59b50f

Browse files
committed
Complete with Java8
1 parent 9c9ee1d commit b59b50f

2 files changed

Lines changed: 77 additions & 0 deletions

File tree

src/test/java/WordFrequency.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import java.util.Arrays;
2+
3+
import static java.util.stream.Collectors.groupingBy;
4+
import static java.util.stream.Collectors.joining;
5+
6+
public class WordFrequency {
7+
public static String handle(String text) {
8+
if (!text.equals("")) {
9+
return Arrays.stream(text.split(" ")).collect(groupingBy((w) -> w)).entrySet().stream()
10+
.sorted((e1, e2) -> e2.getValue().size() - e1.getValue().size()).map((e) -> e.getKey() + " " + e.getValue().size()).collect(joining("\r\n"));
11+
}
12+
return "";
13+
}
14+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,65 @@
1+
import org.junit.Test;
2+
3+
import static org.fest.assertions.api.Assertions.assertThat;
4+
15
public class WordFrequencyTest {
6+
@Test
7+
public void should_handle_blank_text() {
8+
// given
9+
String text = "";
10+
11+
// when
12+
String result = WordFrequency.handle(text);
13+
14+
// then
15+
assertThat(result).isEqualTo("");
16+
}
17+
18+
@Test
19+
public void should_handle_one_word_text() {
20+
// given
21+
String text = "one";
22+
23+
// when
24+
String result = WordFrequency.handle(text);
25+
26+
// then
27+
assertThat(result).isEqualTo("one 1");
28+
}
29+
30+
@Test
31+
public void should_handle_two_different_words_text() {
32+
// given
33+
String text = "one two";
34+
35+
// when
36+
String result = WordFrequency.handle(text);
37+
38+
// then
39+
assertThat(result).isEqualTo("one 1\r\ntwo 1");
40+
}
41+
42+
@Test
43+
public void should_handle_duplicated_words_text() {
44+
// given
45+
String text = "one two one";
46+
47+
// when
48+
String result = WordFrequency.handle(text);
49+
50+
// then
51+
assertThat(result).isEqualTo("one 2\r\ntwo 1");
52+
}
53+
54+
@Test
55+
public void should_handle_the_order() {
56+
// given
57+
String text = "one two two";
58+
59+
// when
60+
String result = WordFrequency.handle(text);
61+
62+
// then
63+
assertThat(result).isEqualTo("two 2\r\none 1");
64+
}
265
}

0 commit comments

Comments
 (0)