1

I have a text file as in /1/. The first column is product Volumn, and second column is product weight. My pupose it to calcuate total_Volumn/total_Weight. Earlier Java version has to read all lines, then parse, and for loop ...., anyway many lines. But this is calcuated by single thread. I would like to Java 8 feature, such us map, reduce, to make this calculation with few lines. Besides, the calculations is more efficient sine Java8 uses multi-threads Any example?

/1/ textData.txt

Volumn  Weight
1010.0  3458
34334   322
3434    343
3433    542
3134    146
3393    246
...
9787    3023
user84592
  • 4,092
  • 6
  • 45
  • 72

2 Answers2

1

You can try this as below :

package com.grs.stackOverFlow.pack06;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

class Data{
    private Double volume;
    private Double weight;

    public Data(String string, String string2) {
        setVolumeString(string);
        setWeightString(string2);
    }
    public Double getVolume() {
        return volume;
    }
    public Double getWeight() {
        return weight;
    }

    public void setVolumeString(String volumeStr){
        this.volume=Double.valueOf(volumeStr);
    }

    public void setWeightString(String weightStr){
        this.weight=Double.valueOf(weightStr);
    }

}
public class FileSummary01 {

    public static void main(String[] args) throws IOException {

        List<String> lines = Files.readAllLines(Paths.get("src/com/grs/stackOverFlow/pack06/data.txt"));

        List<Data> data=lines.stream()
                                     .skip(1)
                                     .map(t->
                                             { String [] a =t.split("\\s+");
                                                System.out.printf("arr : %s %s %n",a[0],a[1]);
                                                return new Data(a[0],a[1]);}).collect(Collectors.toList());

        System.out.println("sum of volume : " + data.stream().mapToDouble(Data::getVolume).sum());
        System.out.println("sum of weight : " + data.stream().mapToDouble(Data::getWeight).sum());

    }

}

If you have large data you can try parallelStream instead of stream.

Goro
  • 431
  • 3
  • 14
0

If your goal is to do something in one line then

Files.lines(Paths.get("/path/to/file.txt")).map(line -> line.split("\t")).mapToDouble(vw -> Double.parseDouble(vw[0])/Double.parseDouble(vw[1])).sum();
Serg M Ten
  • 5,278
  • 4
  • 19
  • 37
  • 1
    But that might not give you any speed improvement over the Java 7 way of reading a file. For performance comparison of single thread vs. multithread implementations see http://stackoverflow.com/a/40597140/3867574 – Serg M Ten Nov 17 '16 at 10:11
  • 1
    your answer is not correct. Actually I want sum(Volumn)/sum(Weight), not sum(v1/w1+v2/w2+...vn/wn). The link you provide is really excellent since it shows different way of reading a file. Thanks anyway. – user84592 Nov 21 '16 at 03:32