Java 8 Java 8文件readAlllines映射和减少

Java 8 Java 8文件readAlllines映射和减少,java-8,Java 8,我有一个文本文件,如/1/所示。第一列为产品体积,第二列为产品重量。我用它来计算总体积/总重量。早期的Java版本必须读取所有行,然后进行解析,for循环……,无论如何,要读取很多行。但这是由单个线程计算的。我想用Java8的特性,比如我们的map,reduce,用很少的行来做这个计算。此外,计算效率更高sine Java8使用多线程任何示例 /1/textData.txt Volumn Weight 1010.0 3458 34334 322 3434 343 3433 5

我有一个文本文件,如/1/所示。第一列为产品体积,第二列为产品重量。我用它来计算总体积/总重量。早期的Java版本必须读取所有行,然后进行解析,for循环……,无论如何,要读取很多行。但这是由单个线程计算的。我想用Java8的特性,比如我们的map,reduce,用很少的行来做这个计算。此外,计算效率更高sine Java8使用多线程任何示例

/1/textData.txt

Volumn  Weight
1010.0  3458
34334   322
3434    343
3433    542
3134    146
3393    246
...
9787    3023

如果你的目标是在一行中做某事,那么

Files.lines(Paths.get("/path/to/file.txt")).map(line -> line.split("\t")).mapToDouble(vw -> Double.parseDouble(vw[0])/Double.parseDouble(vw[1])).sum();

您可以按以下方式进行尝试:

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());

    }

}
package com.grs.stackOverFlow.pack06;
导入java.io.IOException;
导入java.nio.file.Files;
导入java.nio.file.path;
导入java.util.List;
导入java.util.stream.collector;
导入java.util.stream.stream;
类数据{
私人双卷;
私人双倍重量;
公共数据(字符串、字符串2){
设置音量限制(字符串);
setWeightString(string2);
}
公共双卷{
返回量;
}
公共双getWeight(){
返回重量;
}
公共void setVolumeString(字符串volumeStr){
此.volume=Double.valueOf(volumeStr);
}
公共void setWeightString(stringweightstr){
此.weight=Double.valueOf(weightStr);
}
}
公共类文件摘要01{
公共静态void main(字符串[]args)引发IOException{
列表行=Files.readAllLines(path.get(“src/com/grs/stackOverFlow/pack06/data.txt”);
列表数据=行。流()
.skip(1)
.map(t->
{String[]a=t.split(\\s+);
System.out.printf(“arr:%s%s%n”,a[0],a[1]);
返回新数据(a[0],a[1]);}.collect(Collectors.toList());
System.out.println(“卷的总和:+data.stream().mapToDouble(data::getVolume.sum());
System.out.println(“权重之和:+data.stream().mapToDouble(data::getWeight.sum());
}
}

如果您有大数据,您可以尝试使用parallelStream而不是stream。

但这可能不会使您的速度比Java 7读取文件的方式有任何提高。有关单线程与多线程实现的性能比较,请参阅您的答案不正确。实际上我想要的是和(体积)/和(重量),而不是和(v1/w1+v2/w2+…vn/wn)。您提供的链接非常棒,因为它显示了读取文件的不同方式。无论如何,谢谢你。