Search code examples
androidforeachstreamcollectretrolambda

Warning in Android Studio: Can be replaced with collect call


I've recently started using retrolambda library to support lambdas in android development and I received the following warning from Android Studio:

Can be replaced with collect call.

This inspection reports foreach loops which can be replaced with stream api calls.

My code is as follows:

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

Question: How can I replace it with collect call?

UPDATE: when I pressed alt+enter it converted code to following:

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

And now it says that it can't resolve method stream(), Collectors. Is it fixable? Can I add some import statements? Or currently it is not supported by retrolambda?

UPDATE: SOLVED, see the answer below.


Solution

  • Thank you everyone who commented under the question. Solved the problem with a help of this library: 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.

    Final working code with this library:

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