我有一个包含气象站的.csv文件(确切地说是136个),但是每个气象站都有许多不同的天气读数(年、月、日、温度、风速)。
我有两个班,一个是气象站(id,名称,纬度,经度)和气象读数(年,月,日,风速,温度)。
我创建了hashmap,其中siteid是键,weatherstation是值。
在第一个循环中,我用适当的值填充了weatherstation类并将siteid设置为键,在第二个循环中,我填充了weatherreading类并将其添加到weatherstation。
所以我现在有136行是这样的:3072=weatherstation[siteid=3072,sitename=cairnwell(3072),纬度=56.88,经度=-3.42,读数=3481]。。。。。。但我不知道怎么找到读数最低的气象站。
我在考虑将hashmap重新输入arraylist或使用collections,尽管我是编程初学者,所以对这种方法很好奇。
String[] weatherData = WeatherData.getData(); // data loaded from .csv file
HashMap<Integer, WeatherStation> weatherStations = new HashMap<>();
for (int i = 1; i < weatherData.length; i++) {
String line = weatherData[i];
String[] data = line.split(","); // spliting data
int siteId = Integer.parseInt(data[0]);
String siteName = data[1];
double latitude = Double.parseDouble(data[2]);
double longitude = Double.parseDouble(data[3]);
if(!weatherStations.containsKey(siteId)) {
WeatherStation weatherStation = new WeatherStation(siteId, siteName, latitude, longitude);
weatherStations.put(siteId, weatherStation);
}
}
for (int i = 1; i < weatherData.length; i++) {
String line = weatherData[i];
String[] data = line.split(",");
int siteId = Integer.parseInt(data[0]);
WeatherStation weatherStation = weatherStations.get(siteId); // calling siteId
int year = Integer.parseInt(data[4]);
int month = Integer.parseInt(data[5]);
int date = Integer.parseInt(data[6]);
int hour = Integer.parseInt(data[7]);
int windSpeed = Integer.parseInt(data[8]);
double temperature = Double.parseDouble(data[9]);
WeatherReading weatherReading = new WeatherReading(weatherStation,year, month, date, hour, windSpeed, temperature);
weatherStation.getReadings().add(weatherReading);
}
int count= Integer.MAX_VALUE;
Iterator<Integer> keySetIterator = weatherStations.keySet().iterator();
while(keySetIterator.hasNext()) { // going over iterations inside HashMap
Integer key = keySetIterator.next();
System.out.println("key: " + key + " value: " + weatherStations.get(key));
}
希望我对这个问题的描述足够了,如果有的话,你可以问更多的信息。谢谢你的帮助。
edit2:我找到了解决方法:] enter code here
int最低读取数=整数最大值;weatherstation\u station=空;
for(int key : weatherStations.keySet()) {
int number_of_readings = weatherStations.get(key).getReadings().size();
if (number_of_readings < lowest_number_of_reading) {
lowest_number_of_reading = number_of_readings;
lowest_station= weatherStations.get(key);
}
}
System.out.println(lowest_station);
暂无答案!
目前还没有任何答案,快来回答吧!