首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用java 8流映射reduce

使用java 8流映射reduce
EN

Stack Overflow用户
提问于 2018-10-07 23:28:45
回答 1查看 2.1K关注 0票数 -3

对于类赋值,我必须使用java 8 stream来模拟map reduce,但要让它运行起来有很多困难。有人能帮我做一下映射(第一步)吗?这就是我到目前为止得到的所有代码: WeatherStationsQ2.java

代码语言:javascript
复制
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

代码语言:javascript
复制
   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();
    }
}

作业描述:问题140分创建一个具有三个属性(字段)的WeatherStation类:站点所在的城市、站点的测量值(测量类对象的列表)和静态外业站点(所有现有气象站的列表)。还要创建一个度量类。测量类的对象应具有时间(整数)和温度(双精度)属性。向这个类添加一个方法maxTemperature( startTime,endTime),该方法返回气象站在startTime和endTime之间测量的最高温度。这部分已经完成了

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

问题1完成了,但我在问题2上遇到了问题

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-08 00:46:09

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

代码语言:javascript
复制
    long count = this.getMeasurements()
        .stream()
        .filter(e -> Math.abs(e.getTime() - t1) <= r)
        .count();

编辑

代码语言:javascript
复制
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);
}

使用下面的调用:

代码语言:javascript
复制
countTemperature(19.0, 10.8, 8);

这是输出:

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52690030

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档