【问题标题】:Warning in Android Studio: Can be replaced with collect callAndroid Studio 中的警告:可以替换为对方付费电话
【发布时间】: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


【解决方案1】:

感谢所有在问题下发表评论的人。在这个库的帮助下解决了这个问题:https://github.com/aNNiMON/Lightweight-Stream-API

Stream.of(YourCollection) 在 Java 8 实现中,您将看到 YourCollection.stream(...) 。 无论哪种方式,都会创建 Stream 的实例。

这个库的最终工作代码:

// 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()));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-15
    • 2015-01-10
    • 1970-01-01
    • 1970-01-01
    • 2014-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多