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

116 lines
4.4 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()
.map(String::chars)
.reduce(IntStream.of(), IntStream::concat)
.boxed()
.collect(Collectors.groupingBy(i -> (char) (i & 0xFFFF), Collectors.counting()));
}
public Map<Character, Long> 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()))
.entrySet()
.stream()
.filter(e -> e.getValue() > n)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
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));
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"));
assert expected.equals(processor.groupByLineLength()); // 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);
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}
final Map<Character, Long> 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}
System.out.println("OK");
}
}