用java8流映射reduce
对于类分配,我必须使用Java8流来模拟MapReduce,但要让它运行起来有很多困难。有人能帮我完成第一步的映射吗?到目前为止,我得到的代码都是她的: WeatherStationsQ2.java用java8流映射reduce,java,mapreduce,java-stream,Java,Mapreduce,Java Stream,对于类分配,我必须使用Java8流来模拟MapReduce,但要让它运行起来有很多困难。有人能帮我完成第一步的映射吗?到目前为止,我得到的代码都是她的: WeatherStationsQ2.java package assignmnent2; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Comparator; import java.util
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);
}
}
任务描述:
问题1[40分]
创建一个
具有三个属性字段的类别气象站:气象站所在的城市、气象站的测量值和类别测量对象列表,以及静态字段气象站和所有现有气象站列表。还要创建一个类度量。类度量对象的属性时间应为整数,温度应为双倍数。将maxTemperaturestartTime、endTime方法添加到此类,该类返回气象站在startTime和endTime之间测量的最高温度。
这部分完成了
问题2[60分]
将上一个问题中的CountTemperatures1、t2、r方法添加到类WeatherStation中。该方法应返回一个包含两对的列表:1个温度t1与区间[t1-r..t1+r]内任何气象站迄今为止测量的温度次数配对,2个温度t2与区间[t2-r..t2+r]内温度次数配对迄今为止,气象站中的任何一个气象站都已对其进行了测量。
为了计算结果,您需要使用“模拟的”MapReduce方法。也就是说,您的代码应该类似于MapReduce方法,但只使用Java>=8,没有机器集群,也没有任何MapReduce软件框架。此外,您需要尽可能使用Java8流,并在适当的情况下使用并行流处理。
最后,在main方法中添加代码,该方法使用几个测试站和
一些测试测量数据,并打印结果
问题1已完成,但我无意中发现了问题2您可以对所有电台使用下面的片段来获得计数
long count = this.getMeasurements()
.stream()
.filter(e -> Math.abs(e.getTime() - t1) <= r)
.count();
这是输出:
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
有太多的文字,没有人会读它,请给一个最低限度的帖子你应该让你的问题更简洁。太多的文本我现在编辑了问题你只是复制粘贴你的作业。嗯@AlexAcquier,上面的代码片段很有效。请参阅我编辑的回复以获取完整的代码。虽然没有完全满足我的需要,但方向是正确的,谢谢@mayurmadnani
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