Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the filter function in kotlin

Tags:

kotlin

filter

so the past couple of hours, i have been trying to understand how the filter function works in kotlin and if it has any correlation with that of Java. basically, i have a code that's written in java and i would love to have it transcribed to kotlin

private List<Order> getFilteredOrders(Courier courier) {
        String[] glovoBoxKeywords = glovoBoxWords.toLowerCase().split(",");
        List<Vehicle> allowedVehicles = Arrays.asList(MOTORCYCLE, ELECTRIC_SCOOTER);

        return orders.stream()
                .filter(order -> {
                    String description = order.getDescription().toLowerCase();
                    if (!courier.getBox()) {
                        return Arrays.stream(glovoBoxKeywords).noneMatch(description::contains);
                    }

                    return true;
                })
                .filter(order -> {
                    Location pickupLocation = order.getPickup();
                    Location deliveryLocation = order.getDelivery();
                    Double distance = calculateDistance(pickupLocation, deliveryLocation);

                    if (distance > longDeliveryDistance) {
                        return allowedVehicles.contains(courier.getVehicle());
                    }

                    return true;
                })
                .collect(Collectors.toList());
    }

i tried this but i got at this, and was literally stuck :(

 private fun findFilteredOrder(courier: Courier) : List<Order> {
        val glovoBoxKeyWords = glovoBoxWords.toLowerCase().split(",")
        val allowedVehicles = listOf(Vehicle.ELECTRIC_SCOOTER, Vehicle.MOTORCYCLE)

        orderList.filter { order ->
            val description = order.getDescription().toLowerCase()
            if(!courier.getBox()) {

            }
            true
        }.filter {
            val pickupLocation = it.getPickup()
            val deliveryLocation = it.getDelivery()
            val distance = calculateDistance(deliveryLocation, pickupLocation)

            if(distance > longDeliveryDistance) {
                courier.getVehicle() in allowedVehicles
            }
            true
        }
    }

Please this is my first attempt and doing something with kotlin, so please go easy guys. thanks, also i'd be appreciative if anyone could help me with informative stuff as to how to understand these kotlin functions better. let, apply, associateBy... etc.. THANKS

like image 650
olatunji oniyide Avatar asked Jun 06 '26 19:06

olatunji oniyide


1 Answers

The filter function in Kotlin Collections has the same principle as other frameworks/libraries, including Java Streams. Given a predicate (a function from the type of the collection to Boolean) it will return a new collection with the elements matching the predicate. You can find more information and examples of other functions and operators in the official documentation and here.

Your code was almost there, I translate the Java Stream operation to Kotlin List and rewrite the return statements to remove the redundant if

private fun findFilteredOrder(courier: Courier) : List<Order> {
    val glovoBoxKeyWords = glovoBoxWords.toLowerCase().split(",")
    val allowedVehicles = listOf(Vehicle.ELECTRIC_SCOOTER, Vehicle.MOTORCYCLE)

    orderList.filter { order ->
        val description = order.getDescription().toLowerCase()
        courier.getBox() || glovoBoxKeywords.none { it in description }
    }.filter { order ->
        val pickupLocation = order.getPickup()
        val deliveryLocation = order.getDelivery()
        val distance = calculateDistance(deliveryLocation, pickupLocation)

        distance <= longDeliveryDistance || courier.getVehicle() in allowedVehicles
    }
}
like image 194
Omar Mainegra Avatar answered Jun 10 '26 19:06

Omar Mainegra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!