Search code examples
javalambdalambdaj

Lambdaj: Loop through Map and get the values and put in other Map


I'm new to lambdaj so trying to get more used to it. I want to update this code using lambdaj:

 Passed in parameter Map<String, Device> devices;
 final Map<String, String> resultHash = new HashMap<String, String>();
        for (Device device : devices.values()) {
            result.put(device.getAddress(), device.getName());
        }

Thanks for you help


Solution

    1. index the devices based on their address, will give you a LambdaMap.
    2. transform the Device values of the LamdbaMap to their names, giving you a LambdaMap.

    From the top of my mind:

    LambdaCollections.with(devices.values())
        .index(Lambda.on(Device.class).getAddress())
        .convertValues(Lambda.on(Device.class).getName());