Android Studio 中的警告:可以用对方付费电话代替
Warning in Android Studio: Can be replaced with collect call
我最近开始使用 retrolambda 库来支持 android 开发中的 lambda,我收到了来自 Android Studio 的以下警告:
Can be replaced with collect call.
This inspection reports foreach loops which can be replaced with stream api calls.
我的代码如下:
// 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()));
现在它说它无法解析方法 stream(), Collectors。
它可以修复吗?我可以添加一些导入语句吗?还是 retrolambda 目前不支持?
更新:已解决,请参阅下面的答案。
谢谢大家在问题下发表评论。在这个库的帮助下解决了问题:https://github.com/aNNiMON/Lightweight-Stream-API
Stream.of(YourCollection)
In the Java 8 implementation you’ll see YourCollection.stream(…) instead.
Either way an instance of Stream is created.
此库的最终工作代码:
// stream() changed to Stream.of( ... ) as per library specs
mGeofenceList.addAll(Stream.of(GeofenceUtils.GeofenceObjects.entrySet())
.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 works without prefix
.build()).collect(Collectors.toList()));
我最近开始使用 retrolambda 库来支持 android 开发中的 lambda,我收到了来自 Android Studio 的以下警告:
Can be replaced with collect call.
This inspection reports foreach loops which can be replaced with stream api calls.
我的代码如下:
// 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()));
现在它说它无法解析方法 stream(), Collectors。 它可以修复吗?我可以添加一些导入语句吗?还是 retrolambda 目前不支持?
更新:已解决,请参阅下面的答案。
谢谢大家在问题下发表评论。在这个库的帮助下解决了问题:https://github.com/aNNiMON/Lightweight-Stream-API
Stream.of(YourCollection) In the Java 8 implementation you’ll see YourCollection.stream(…) instead. Either way an instance of Stream is created.
此库的最终工作代码:
// stream() changed to Stream.of( ... ) as per library specs
mGeofenceList.addAll(Stream.of(GeofenceUtils.GeofenceObjects.entrySet())
.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 works without prefix
.build()).collect(Collectors.toList()));