This repository has been archived on 2021-10-31. You can view files and clone it, but cannot push or open issues or pull requests.
PF3/hw3/src/streams/FileLinesProcessor.java

117 lines
4.2 KiB
Java

package streams;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class FileLinesProcessor {
private final String filename;
FileLinesProcessor(String filename) {
this.filename = filename;
}
private Stream<String> getLines() throws IOException {
return Files.lines(Paths.get(filename));
}
public byte[][] fileLinesAsBytes() throws IOException {
return getLines().map(String::getBytes).toArray(byte[][]::new);
}
public long countByNumberOfSpaces(int n) throws IOException {
return getLines()
.map(s -> s.chars()
.filter(c -> c == ' ')
.count())
.filter(c -> c == n).count();
}
public String concatNth(int n) throws IOException {
return getLines()
.map(s -> n >= s.length() ? "" : s.substring(n, n + 1))
.collect(Collectors.joining());
}
public Map<Integer, List<String>> groupByLineLength() throws IOException {
return getLines()
.collect(Collectors.groupingBy(String::length, Collectors.toList()));
}
public Map<Character, Long> countCharsOccurrences() throws IOException {
return getLines()
.flatMapToInt(String::chars)
.boxed()
.collect(Collectors.groupingBy(i -> (char) i.intValue(), Collectors.counting()));
}
public Map<Character, Long> countCharsOccurrences(int n) throws IOException {
return countCharsOccurrences()
.entrySet()
.stream()
.filter(e -> e.getValue() > n)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
private static <T> 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");
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<Integer, List<String>> 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"));
assertEquals(processor.groupByLineLength(), expected); // Expected result: {3=[abc, xyz], 11=[hello world],
// 17=[play with streams]}
final Map<Character, Long> expected2 = new HashMap<>();
expected2.put(' ', 3L);
expected2.put('a', 3L);
expected2.put('b', 1L);
expected2.put('c', 1L);
expected2.put('d', 1L);
expected2.put('e', 2L);
expected2.put('h', 2L);
expected2.put('i', 1L);
expected2.put('l', 4L);
expected2.put('m', 1L);
expected2.put('o', 2L);
expected2.put('p', 1L);
expected2.put('r', 2L);
expected2.put('s', 2L);
expected2.put('t', 2L);
expected2.put('w', 2L);
expected2.put('x', 1L);
expected2.put('y', 2L);
expected2.put('z', 1L);
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<Character, Long> expected3 = new HashMap<>();
expected3.put(' ', 3L);
expected3.put('a', 3L);
expected3.put('l', 4L);
assertEquals(processor.countCharsOccurrences(2), expected3); // Expected result: { =3, a=3, l=4}
System.out.println("OK");
}
}