我是Java的新手,遇到以下问题时遇到困难。我有一个超过100万个天气读数的大型String数组,每个元素包含站点ID,站点名称,站点纬度,站点经度,年,月,日期(1-31),小时,风速,温度。
我的问题是:在英国任何地方,温度下降到等于或低于0.0天有多少天。 所有条目都来自同一年,所以这不是问题。但是month int变量的范围是1到12,date int变量的范围是1到31。什么是组织数据以允许我仅计算唯一天数的最佳方法?
我已经用get方法创建了一个名为WeatherRecords的对象类,该方法将字符串用逗号分割,并将每个元素解析为正确的类型,然后将该对象存储在数组中。像这样:
String[] weatherData = WeatherData.getData();
ArrayList<WeatherRecords> records = new ArrayList<>();
for (int i = 1; i < weatherData.length; i++) {
String line = weatherData[i];
String[] elements = line.split(",");
String siteIdString = elements[0];
String siteName = elements[1];
String siteLatString = elements[2];
String siteLonString = elements[3];
String recordYearString = elements[4];
String recordMonthString = elements[5];
String recordDateString = elements[6];
String recordHourString = elements[7];
String recordWindSpeedString = elements[8];
String recordTempString = elements[9];
int siteId = Integer.parseInt(siteIdString);
double siteLat = Double.parseDouble(siteLatString);
double siteLon = Double.parseDouble(siteLonString);
int recordYear = Integer.parseInt(recordYearString);
int recordMonth = Integer.parseInt(recordMonthString);
int recordDate = Integer.parseInt(recordDateString);
int recordHour = Integer.parseInt(recordHourString);
int recordWindSpeed = Integer.parseInt(recordWindSpeedString);
double recordTemp = Double.parseDouble(recordTempString);
WeatherRecords record = new WeatherRecords(siteId, siteName, siteLat, siteLon, recordYear, recordMonth,
recordDate, recordHour, recordWindSpeed, recordTemp);
records.add(record);
}
return records;
}
答案 0 :(得分:2)
请尝试下面显示的代码。使用LocalDate
代表您的年月日。然后使用流过滤并收集目标对象。
class WeatherRecord{
double recordTemp;
LocalDate localDate;
String siteName;
}
…和…
WeatherRecord w1 = new WeatherRecord();
w1.setRecordTemp(1);
w1.setSiteName("UK");
w1.setLocalDate(LocalDate.of(recordYear, recordMonth, recordDate));
records.add(w1);
List<WeatherRecord> filteredList =
list
.stream()
.filter( w -> w.getRecordTemp() <= 0 )
.collect( Collectors.toList() )
;
答案 1 :(得分:0)
假设Java 8或更高版本:
首先,将您的年,月日,小时更改为WeatherRecord的LocalDateTime属性(我想类别名称应删除“ s”),这样会更易于处理。
然后,我应该查看stream(),在其中将过滤并检索WeatherRecord的arrayList中需要的内容。
答案 2 :(得分:0)
在英国任何地方,温度下降到等于或低于0.0天有多少天
dassum的very good Answer可以帮助我们解决大部分问题,但是省略了有关获取不同日期列表的部分。让我们再推些代码。
我们的WeatherSample
类首先显示。
请注意,我们使用BigDecimal
而不是float
或double
或Float
或Double
,以避免{{3}中的the inaccuracy }技术。
要比较一对BigDecimal
对象,我们不能使用<
或<=
之类的运算符。我们必须调用floating-point接口的compareTo
,如果第一个对象小于第二个对象,则该接口返回一个负整数。因此,我们可以测试compareTo
的结果是否小于零。有关更多信息,请参见Comparable
。
package work.basil.example;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Objects;
final public class WeatherSample
{
// ----------| Member fields |---------------------------
final private LocalDate localDate;
final private int hour;
final private String siteName;
final private BigDecimal recordTemp;
// ----------| Constructors |---------------------------
public WeatherSample ( LocalDate localDate , int hour , String siteName , BigDecimal recordTemp )
{
this.localDate = Objects.requireNonNull( localDate );
this.hour = Objects.requireNonNull( hour );
this.siteName = Objects.requireNonNull( siteName );
this.recordTemp = Objects.requireNonNull( recordTemp ); // Should also check for String::isBlank & String::isEmpty.
}
// ----------| Accessors |---------------------------
public LocalDate getLocalDate ( )
{
return this.localDate;
}
public int getHour ( )
{
return this.hour;
}
public String getSiteName ( )
{
return this.siteName;
}
public BigDecimal getRecordTemp ( )
{
return this.recordTemp;
}
}
创建一些实例,并收集到不可修改的List
中。我们有23日,24日和25日的样本。仅有第一天和最后一天是热门歌曲,其时空低于零。
List < WeatherSample > samples = List.of(
new WeatherSample( LocalDate.of( 2020 , 1 , 23 ) , 11 , "Springfield" , new BigDecimal( "-23.5" ) ) ,
new WeatherSample( LocalDate.of( 2020 , 1 , 23 ) , 13 , "Springfield" , new BigDecimal( "10.1" ) ) ,
new WeatherSample( LocalDate.of( 2020 , 1 , 24 ) , 11 , "Springfield" , new BigDecimal( "4.5" ) ) ,
new WeatherSample( LocalDate.of( 2020 , 1 , 24 ) , 13 , "Springfield" , new BigDecimal( "4.7" ) ) ,
new WeatherSample( LocalDate.of( 2020 , 1 , 25 ) , 11 , "Springfield" , new BigDecimal( "-25.0" ) ) ,
new WeatherSample( LocalDate.of( 2020 , 1 , 25 ) , 13 , "Springfield" , new BigDecimal( "-25.7" ) )
);
用于处理该列表的代码。首先,我们从列表中生成一个流,每个WeatherSample
对象都被连续考虑。然后我们过滤掉
List < LocalDate > dates =
samples
.stream()
.filter( sample -> ( sample.getRecordTemp().compareTo( BigDecimal.ZERO ) < 0 ) )
.map( sample -> sample.getLocalDate() )
.distinct()
.collect( Collectors.toList() );
转储到控制台。
System.out.println( "dates.toString(): " + dates );
运行时。
dates.toString():[2020-01-23,2020-01-25]
您实际上只是问了几天。为this Question询问List
。
int countOfDates = dates.size() ;
2