Map reduce with java 8 流

Map reduce with java 8 stream

对于 class 作业,我必须使用 java 8 流来模拟 map reduce,但我很难让它继续运行。有人可以帮助我进行映射(第一步)吗?她是我到目前为止得到的所有代码: WeatherStationsQ2.java

package assignmnent2;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class WeatherStationQ2 {

    //Setting up class attributes
    private String city;
    private List<MeasurementQ2> measurements;
    public static List<WeatherStationQ2> stations= new ArrayList<>();


    //Setting up constructor for the WeatherStationQ1 object
    public WeatherStationQ2(String city, List<MeasurementQ2> measurements/*,List<String> stations*/){
        this.city = city;
        this.measurements = measurements;
    }

    //Setting up the setters and getters for the attributes of the object WeatherStationQ1
    public void setCity(String city){
        this.city = city;
    }

    public String getCity(){
        return city;        
    }

    public void setMeasurements(List<MeasurementQ2> measurements){
        this.measurements = measurements;
    }

    public List<MeasurementQ2> getMeasurements(){
        return measurements;
    }

    //MaxTemperature function to return highest temperature of a given time range
    public void maxTemperature(int startTime, int endTime){

        //Creates a list of the MeasurementQ2 object in the selected time range
        List<MeasurementQ2> tempList = this.getMeasurements().stream().filter(e -> e.getTime()>=startTime)
                .filter(e -> e.getTime()<=endTime).collect(Collectors.toList());

        //Finding the MeasurementQ2 with the higher temperature in the filtered list
        MeasurementQ2 maxMe = tempList.stream().max(Comparator.comparing(MeasurementQ2::getTemperature))
                  .orElseThrow(NoSuchElementException::new);

        //Display results
        System.out.println("The maximum temperature was: "+maxMe.getTemperature()+" and it happen at time: "+maxMe.getTime());
    }

    public static void countTemperature(double t1, double t2, int r){
        Stream<List<WeatherStationQ2>> st = Arrays.asList(stations).stream();
        Map<Integer, Double> map = st
        //List <MeasurementQ2> map =  (List<MeasurementQ2>) stations.parallelStream().map(WeatherStationQ2::getMeasurements);
        //Map <Integer, Double> map = stations.parallelStream().collect(Collectors.groupingBy(MeasurementQ2::getTime, MeasurementQ2::getTemperature));
        /*Stream<List<WeatherStationQ2>> st  = Arrays.asList(stations).stream();
        Stream<List<WeatherStationQ2>> map = st.map(s -> s);*/

        st.forEach(s->System.out.println(s));
    }

    public static void main(String Args[]){
        //Creates a series of MeasurementQ1 object, creates a list and populate the list
        MeasurementQ2 m = new MeasurementQ2(1, 2.0);
        MeasurementQ2 n = new MeasurementQ2(13, 8.1);
        MeasurementQ2 o = new MeasurementQ2(25, 12.5);
        List<MeasurementQ2> mesearements = new ArrayList<>();
        mesearements.add(m);
        mesearements.add(n);
        mesearements.add(o);
        MeasurementQ2 p = new MeasurementQ2(3, 23.6);
        MeasurementQ2 q = new MeasurementQ2(11, 13.8);
        MeasurementQ2 r = new MeasurementQ2(28, 14.5);
        List<MeasurementQ2> measure = new ArrayList<>();
        measure.add(p);
        measure.add(q);
        measure.add(r);

        //Creates the WeatherStationQ1 object
        WeatherStationQ2 WS = new WeatherStationQ2("Galway", mesearements);
        WeatherStationQ2 WS2 = new WeatherStationQ2("Dublin", measure);

        stations.add(WS);
        stations.add(WS2);
        WS.maxTemperature(1, 30);// Applying the maxTemperature method
        WS2.maxTemperature(1, 30);

        countTemperature(19.0,10.8,3);
    }
}

MeasurementQ2.java

   package assignmnent2;

public class MeasurementQ2 {

    //Setting up class attributes
    private int time;
    private double temperature;

    //Setting up constructor for the MeasurementQ1 object
    public MeasurementQ2(int time, double temperature){
        this.time = time;
        this.temperature=temperature;
    }

    //Setting up the setters and getters for the attributes of the object MeasurementQ1
    public void setTime(int time){
        this.time=time;
    }

    public int getTime(){
        return time;        
    }

    public void setTemperature(double temperature){
        this.temperature = temperature;
    }

    public double getTemperature(){
        return temperature;
    }
    @Override
    public String toString() {
        return this.getTime() + " " + this.getTemperature();
    }
}

作业说明: 第一题【40分】 创建一个 class具有三个属性(字段)的WeatherStation:站点所在的城市、站点的测量值(class测量值的对象列表)和静态站点站点(所有现有天气的列表)站)。同时创建一个 class 测量。 class 测量的对象应具有属性时间(整数)和温度(双精度数)。添加一个方法 maxTemperature(startTime, endTime) 到这个 class 其中 return 是气象站在 startTime 和 endTime 之间测量的最高温度。 这部分已完成

第2题[60分] 将方法 countTemperatures(t1,t2,r) 添加到上一个问题的 class WeatherStation。该方法应该 return 一个包含两对的列表:1) 温度 t1 与迄今为止任何天气测量的间隔 [t1-r..t1+r] 中温度的次数配对站中的站,以及 2) 温度 t2 与迄今为止站中任何气象站测量的间隔 [t2-r..t2+r] 中温度的次数配对。 为了计算结果,您需要使用“模拟”MapReduce 方法。也就是说,您的代码应该类似于 MapReduce 方法,但仅使用 Java >=8(没有机器集群,也没有任何 MapReduce 软件框架)。此外,您需要使用 Java 8 Streams(尽可能)和并行流处理(在适当的情况下)。 最后,将代码添加到您的主要方法,该方法使用几个测试站调用您的 countTemperatures 方法,并且 一些测试测量数据,并打印结果。

问题 1 完成了,但我偶然发现了问题 2

您可以对所有站点使用以下代码段来获取计数

    long count = this.getMeasurements()
        .stream()
        .filter(e -> Math.abs(e.getTime() - t1) <= r)
        .count();

编辑

public static void countTemperature(double t1, double t2, int r) {
    stations.stream()
            .map(station ->
                    station.getMeasurements()
                            .stream()
                            .filter(e -> Math.abs(e.getTime() - t1) <= r)
                            .count()
            )
            .forEach(System.out::println);
}

通过以下调用:

countTemperature(19.0, 10.8, 8);

这是输出:

The maximum temperature was: 12.5 and it happen at time: 25
The maximum temperature was: 23.6 and it happen at time: 3
2
1