对于类分配,我必须使用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();
}
}
作业说明: 问题1 [40分] 创建一个 具有三个属性(字段)的WeatherStation类:站点所在的城市,站点的度量(Measurement类的对象列表)和静态场站(所有现有气象站的列表)。还创建一个类Measurement。 Measurement类的对象应具有时间(整数)和温度(双精度数)属性。向此类添加方法maxTemperature(startTime,endTime),该方法返回气象站在startTime和endTime之间测得的最高温度。 此部分已完成
问题2 [60分] 从上一个问题向类WeatherStation添加方法countTemperatures(t1,t2,r)。该方法应返回一个包含两对的列表:1)温度t1与迄今站点内任何气象站测量到的时间间隔[t1-r..t1 + r]中的温度的次数成对, 2)温度t2与迄今已通过气象站中的任何气象站测量的间隔[t2-r..t2 + r]中温度的次数成对。 为了计算结果,您需要使用“模拟” MapReduce方法。也就是说,您的代码应类似于MapReduce方法,但仅使用Java> = 8(没有计算机集群,也没有任何MapReduce软件框架)。另外,您需要使用Java 8 Streams(尽可能)和并行流处理(如果适用)。 最后,将代码添加到您的主方法中,该方法使用一些测试站来调用countTemperatures方法。 一些测试测量数据,并打印结果。
问题1已经完成,但我偶然发现了问题2
答案 0 :(得分:0)
您可以对所有电台使用下面的代码片段来获取计数
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