Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package AV5;
- import javax.sound.sampled.Line;
- public class LineCounter {
- private int lines;
- private int words;
- private int chars;
- public LineCounter(int lines, int words, int chars) {
- this.lines = lines;
- this.words = words;
- this.chars = chars;
- }
- public LineCounter(String line) {
- ++lines;
- words = line.split("\\s+").length;
- chars = line.length();
- }
- public LineCounter sum(LineCounter other) {
- return new LineCounter(this.lines + other.lines,
- this.words + other.words,
- this.chars + other.chars);
- }
- @Override
- public String toString() {
- return String.format("Lines : %d , Words: %d , Chars: %d", lines, words, chars);
- }
- }
- public static void readDataWithBufferedReaderAndMapReduce(InputStream inputStream)
- {
- BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
- LineCounter result = bufferedReader.lines()
- //mapiraj od linija vo objekt
- //za sekoja linija ke se napravi nov line counter
- .map(l -> new LineCounter(l))
- //reduce prima 2 raboti->od 10 line counteri napravi 1->od poveke nesta sklopi gi vo edno nesto
- .reduce(
- new LineCounter(0, 0, 0),
- //left e momentot do rezultatot a right posle toa
- (left, right) -> left.sum(right)
- );
- //reduce e terminalna akcija
- System.out.println(result);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement