我最近开始使用retrolambda库来支持Android开发中的lambda,我从Android Studio收到以下警告:
可以用收集电话代替。
此检查报告foreach循环,可以用流api调用替换。
我的代码如下:
// mGeofenceList is a List<Geofence>
mGeofenceList = new ArrayList<>();
// GeofenceUtils.GeofenceObjects.entrySet() is a HashMap<String, LatLng>
for (Map.Entry<String, LatLng> entry : GeofenceUtils.GeofenceObjects.entrySet()) {
mGeofenceList.add(new Geofence.Builder()
.setRequestId(entry.getKey())
.setCircularRegion(
entry.getValue().latitude,
entry.getValue().longitude,
GeofenceUtils.GEOFENCE_RADIUS_IN_METERS)
.setExpirationDuration(GeofenceUtils.GEOFENCE_EXPIRATION_IN_MILLISECONDS)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER |
Geofence.GEOFENCE_TRANSITION_EXIT)
.build());
}
问题:如何将其替换为收集电话?
更新:当我按下alt +时,将其转换为以下代码:
// method stream() cannot be found
mGeofenceList.addAll(GeofenceUtils.GeofenceObjects.entrySet().stream()
.map(entry -> new Geofence.Builder()
.setRequestId(entry.getKey())
.setCircularRegion(
entry.getValue().latitude,
entry.getValue().longitude,
GeofenceUtils.GEOFENCE_RADIUS_IN_METERS)
.setExpirationDuration(GeofenceUtils.GEOFENCE_EXPIRATION_IN_MILLISECONDS)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER |
Geofence.GEOFENCE_TRANSITION_EXIT)
// Collectors cannot be found
.build()).collect(java.util.stream.Collectors.toList()));
现在它说它无法解析方法流(),收集器。 它可以修复吗?我可以添加一些import语句吗?或者目前它不受retrolambda的支持?
更新:已解决,请参阅下面的答案。
答案 0 :(得分:2)
感谢所有评论此问题的人。在此库的帮助下解决了问题:https://github.com/aNNiMON/Lightweight-Stream-API
Stream.of(YourCollection) 在Java 8实现中,您将看到YourCollection.stream(...)。 无论哪种方式,都会创建一个Stream实例。
此库的最终工作代码:
df <- data.frame(datetime=c('1/1/2015 0:00','1/1/2015 0:01','1/1/2015 0:02','1/1/2015 0:03','1/1/2015 0:04','1/1/2015 0:05','1/1/2015 0:06','1/1/2015 0:07','1/1/2015 0:08','1/1/2015 0:09','1/1/2015 0:10','1/1/2015 0:11','1/1/2015 0:12','1/1/2015 0:13','1/1/2015 0:14','1/1/2015 0:15','1/1/2015 0:16','1/1/2015 0:17'),alarm=c('NO','NO','NO','NO','NO','NO','NO','NO','NO','NO','YES','NO','NO','NO','YES','NO','NO','NO'),stringsAsFactors=F);
df$datetime <- as.POSIXct(df$datetime,'%d/%m/%Y %H:%M',tz='UTC');
w <- which(df$alarm=='YES');
df$diff <- difftime(df$datetime,df$datetime[c(1L,w)[findInterval(seq_along(df$alarm),w)+1L]],units='hours');
df;
## datetime alarm diff
## 1 2015-01-01 00:00:00 NO 0.00000000 hours
## 2 2015-01-01 00:01:00 NO 0.01666667 hours
## 3 2015-01-01 00:02:00 NO 0.03333333 hours
## 4 2015-01-01 00:03:00 NO 0.05000000 hours
## 5 2015-01-01 00:04:00 NO 0.06666667 hours
## 6 2015-01-01 00:05:00 NO 0.08333333 hours
## 7 2015-01-01 00:06:00 NO 0.10000000 hours
## 8 2015-01-01 00:07:00 NO 0.11666667 hours
## 9 2015-01-01 00:08:00 NO 0.13333333 hours
## 10 2015-01-01 00:09:00 NO 0.15000000 hours
## 11 2015-01-01 00:10:00 YES 0.00000000 hours
## 12 2015-01-01 00:11:00 NO 0.01666667 hours
## 13 2015-01-01 00:12:00 NO 0.03333333 hours
## 14 2015-01-01 00:13:00 NO 0.05000000 hours
## 15 2015-01-01 00:14:00 YES 0.00000000 hours
## 16 2015-01-01 00:15:00 NO 0.01666667 hours
## 17 2015-01-01 00:16:00 NO 0.03333333 hours
## 18 2015-01-01 00:17:00 NO 0.05000000 hours