【发布时间】:2016-04-05 04:44:03
【问题描述】:
我最近开始使用 retrolambda 库来支持 android 开发中的 lambda,我收到了来自 Android Studio 的以下警告:
可以替换为对方付费电话。
此检查报告可替换为流 api 调用的 foreach 循环。
我的代码如下:
// 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+enter 时,它会将代码转换为以下内容:
// 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()));
现在它说它无法解析方法流(),收集器。 它可以修复吗?我可以添加一些导入语句吗?还是目前retrolambda不支持?
更新:已解决,请参阅下面的答案。
【问题讨论】:
-
@harshad,我看到了这个答案,这与我的问题无关。
-
点击Alt+Enter然后替换
-
@NonthonbamTonthoi,谢谢。现在它说它无法解析流,收集器方法。
-
Android 不运行 Java 8。不知道为什么 IDE 认为你可以。您应该在项目设置中更改 Java 编译版本
标签: android foreach stream collect retrolambda