我正在访问这个API,它给我提供了全球天气:
https://callforcode.weather.com/doc/v3-global-weather-notification-headlines/
然而,它以lat/lng作为输入参数,我需要整个世界的数据。
我想我可以遍历每一个纬度,每两个纬度和两个经度,给我一个世界上的点,每120英里,南北大约100度,这应该给我16,200个API调用((360/2) * (180/2))中的所有数据。
如何在Java中有效地做到这一点?
我想出了这样的主意,但有什么更好的办法吗?
for(int i = 0; i < 360; i+2){
var la = i;
for(int x = 0 x < 180; x+2) {
var ln = x;
//call api with lat = i, lng = x;
}
}发布于 2018-09-10 18:23:48
这在某种程度上是一种范式转变,但我不会使用嵌套的for -循环来解决这个问题。在许多情况下,您正在考虑对整个结果集进行迭代,通常可以在不损失很多或任何效果的情况下大幅减少覆盖范围。缓存,裁剪,排序.这些都是你需要的东西:而不是换循环。
因此,您最终得到的是某种加权缓存系统,它考虑了人口密度、使用模式和其他优先级,以确定要检查的纬度/经度坐标以及检查频率。高级代码可能如下所示:
void executeUpdateSweep(List<CoordinateCacheItem> cacheItems)
{
for(CoordinateCacheItem item : cacheItems)
{
if(shouldRefreshCache(item))
{
//call api with lat = item.y , lng = item.x
}
}
}
boolean shouldRefreshCache(item)
{
long ageWeight = calculateAgeWeight(item);//how long since last update?
long basePopulationWeight = item.getBasePopulationWeight();//how many people (users and non-users) live here?
long usageWeight = calculateUsageWeight(item);//how much is this item requested?
return ageWeight + basePopulationWeight + usageWeight > someArbitraryThreshold;
}https://stackoverflow.com/questions/52260948
复制相似问题