diff --git a/hw3/.idea/workspace.xml b/hw3/.idea/workspace.xml index 986b4a9..3c08476 100644 --- a/hw3/.idea/workspace.xml +++ b/hw3/.idea/workspace.xml @@ -2,8 +2,9 @@ - + + - + - + - - - @@ -31,8 +29,8 @@ - - + + @@ -50,11 +48,11 @@ - + - - + + @@ -95,8 +93,8 @@ @@ -154,6 +152,7 @@ + @@ -175,7 +174,7 @@ - + @@ -266,9 +265,9 @@ + - @@ -285,18 +284,22 @@ 1575886303360 - + + + + + - - + - + @@ -306,7 +309,7 @@ - + @@ -332,20 +335,6 @@ - - - - - - - - - - - - - - @@ -364,18 +353,18 @@ - + + - - - - - - - - - - + + + + + + + + + @@ -390,33 +379,30 @@ + + + + + + + + + + + + + + - - + + - - - - - - - - - - - - - - - - - @@ -473,7 +459,6 @@ diff --git a/hw3/src/streams/FileLinesProcessor.java b/hw3/src/streams/FileLinesProcessor.java index 98bd31e..f07e3d6 100644 --- a/hw3/src/streams/FileLinesProcessor.java +++ b/hw3/src/streams/FileLinesProcessor.java @@ -46,40 +46,41 @@ public class FileLinesProcessor { public Map countCharsOccurrences() throws IOException { return getLines() - .map(String::chars) - .reduce(IntStream.of(), IntStream::concat) + .flatMapToInt(String::chars) .boxed() - .collect(Collectors.groupingBy(i -> (char) (i & 0xFFFF), Collectors.counting())); + .collect(Collectors.groupingBy(i -> (char) i.intValue(), Collectors.counting())); } public Map countCharsOccurrences(int n) throws IOException { - return getLines() - .map(String::chars) - .reduce(IntStream.of(), IntStream::concat) - .boxed() - .collect(Collectors.groupingBy(i -> (char) (i & 0xFFFF), Collectors.counting())) + return countCharsOccurrences() .entrySet() .stream() .filter(e -> e.getValue() > n) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); } + private static void assertEquals(T o1, T o2) { + if (o1 != o2 && o2 == null || !o2.equals(o1)) { + throw new AssertionError(o1 + " != " + o2); + } + } + public static void main(final String... args) throws IOException { final FileLinesProcessor processor = new FileLinesProcessor("ex3_test.txt"); - assert processor.fileLinesAsBytes()[0][1] == 'b'; - assert processor.fileLinesAsBytes()[1][2] == 'z'; - assert processor.countByNumberOfSpaces(0) == 2; // (i.e., "abc" and "xyz") - assert processor.countByNumberOfSpaces(2) == 1; // (i.e., "play with streams") - assert "axhp".equals(processor.concatNth(0)); - assert "byel".equals(processor.concatNth(1)); - assert "ly".equals(processor.concatNth(3)); + assertEquals(processor.fileLinesAsBytes()[0][1], (byte) 'b'); + assertEquals(processor.fileLinesAsBytes()[1][2], (byte) 'z'); + assertEquals(processor.countByNumberOfSpaces(0), 2L); // (i.e., "abc" and "xyz") + assertEquals(processor.countByNumberOfSpaces(2), 1L); // (i.e., "play with streams") + assertEquals(processor.concatNth(0), "axhp"); + assertEquals(processor.concatNth(1), "byel"); + assertEquals(processor.concatNth(3), "ly"); final Map> expected = new HashMap<>(); expected.put(3, Arrays.asList("abc", "xyz")); expected.put(11, Collections.singletonList("hello world")); expected.put(17, Collections.singletonList("play with streams")); - assert expected.equals(processor.groupByLineLength()); // Expected result: {3=[abc, xyz], 11=[hello world], - // 17=[play with streams]} + assertEquals(processor.groupByLineLength(), expected); // Expected result: {3=[abc, xyz], 11=[hello world], + // 17=[play with streams]} final Map expected2 = new HashMap<>(); expected2.put(' ', 3L); @@ -101,14 +102,14 @@ public class FileLinesProcessor { expected2.put('x', 1L); expected2.put('y', 2L); expected2.put('z', 1L); - assert expected2.equals(processor.countCharsOccurrences()); // Expected result { =3, a=3, b=1, c=1, d=1, e=2, - // h=2, i=1, l=4, m=1, o=2, p=1, r=2, s=2, t=2, w=2, - // x=1, y=2, z=1} + assertEquals(processor.countCharsOccurrences(), expected2); // Expected result { =3, a=3, b=1, c=1, d=1, e=2, + // h=2, i=1, l=4, m=1, o=2, p=1, r=2, s=2, t=2, w=2, + // x=1, y=2, z=1} final Map expected3 = new HashMap<>(); expected3.put(' ', 3L); expected3.put('a', 3L); expected3.put('l', 4L); - assert expected3.equals(processor.countCharsOccurrences(2)); // Expected result: { =3, a=3, l=4} + assertEquals(processor.countCharsOccurrences(2), expected3); // Expected result: { =3, a=3, l=4} System.out.println("OK"); }