1
\$\begingroup\$

In List<Hotel>, I'd like to extract Hotels which has OTAProperty with a specific otaName, as Map<Long, Hotel>. The map's key is the id for the OTAProperty named given otaName, and the value for the map is the Hotel instance.

I made a method createHotelMapByOtaName working as intended. But I think it's possible to remove nested forEach block by using Java 8's stream and related operations such as map, flatMap, and Collectors.toMap. Can anyone get some advice or code snippets?

package com.example;

import com.google.common.collect.Lists;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HotelService {

    public static Map<Long, Hotel> createHotelMapByOtaName(List<Hotel> hotels, String otaName) {
        Map<Long, Hotel> map = new HashMap<>();
        hotels.forEach(h -> {
            h.getOtaProperties().forEach(op -> {
                if (otaName.equals(op.getOtaName())) map.put(op.getId(), h);
            });
        });
        return map;
    }

    public static void main(String[] args) {
        List<Hotel> hotels = Lists.newArrayList(
                new Hotel("Hilton",
                        Lists.newArrayList(
                                new OTAProperty(1L, "expedia"),
                                new OTAProperty(2L, "agoda"))),
                new Hotel("Ramada",
                        Lists.newArrayList(
                                new OTAProperty(4L, "agoda"))),
                new Hotel("Hyatt",
                        Lists.newArrayList(
                                new OTAProperty(5L, "expedia")))
        );
        // expected: {1=Hotel{name=Hilton, otaProperties=[OTAProperty{id=1, otaName=expedia}, OTAProperty{id=2, otaName=agoda}]}, 5=Hotel{name=Hyatt, otaProperties=[OTAProperty{id=5, otaName=expedia}]}}
        System.out.println(HotelService.createHotelMapByOtaName(hotels, "expedia"));
    }
}

Followings are codes for 2 models:

Hotel.java:

package com.example;

import com.google.common.base.MoreObjects;

import java.util.List;

public class Hotel {
    private String name;
    private List<OTAProperty> otaProperties;

    public Hotel(String name, List<OTAProperty> otaProperties) {
        this.name = name;
        this.otaProperties = otaProperties;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<OTAProperty> getOtaProperties() {
        return otaProperties;
    }

    public void setOtaProperties(List<OTAProperty> otaProperties) {
        this.otaProperties = otaProperties;
    }

    @Override
    public String toString() {
        return MoreObjects.toStringHelper(this)
                .add("name", name)
                .add("otaProperties", otaProperties)
                .toString();
    }
}

OTAProperty.java:

package com.example;

import com.google.common.base.MoreObjects;

public class OTAProperty {
    private Long id;
    private String otaName;

    public OTAProperty(Long id, String otaName) {
        this.id = id;
        this.otaName = otaName;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getOtaName() {
        return otaName;
    }

    public void setOtaName(String otaName) {
        this.otaName = otaName;
    }

    @Override
    public String toString() {
        return MoreObjects.toStringHelper(this)
                .add("id", id)
                .add("otaName", otaName)
                .toString();
    }
}
\$\endgroup\$
4
\$\begingroup\$

This transformation is not so obvious, but still feasible without instantiating a dedicated Map directly and without using forEach.

First of all, I suggest to implement hashCode and equals in Hotel and OTAProperty classes: this is often useful, especially when storing objects in collections.

Here is how the functional sequence can look like:

public static Map<Long, Hotel> createHotelMapByOtaName2(List<Hotel> hotels, String otaName) {
  return hotels.stream()
             .map(hotel ->
                hotel.getOtaProperties().stream()
                     .filter(op -> otaName.equals(op.getOtaName())) // 1
                     .collect(Collectors.toMap(OTAProperty::getId,  
                                               (h) -> hotel))) // 2
             .map(Map::entrySet) // 3
             .flatMap(Collection::stream) // 4
             .collect(Collectors.toMap(Map.Entry::getKey,
                                       Map.Entry::getValue)); // 5
}
  1. Equivalent of the original if condition: we take only OTAProperties that match the required name.

  2. Each hotel item is wrapped to a Map<Long, Hotel>, which is a subset of the resulting map.

  3. Transformation of the stream of Maps into a stream of Set<Entry<Long, Hotel>>.

  4. Concatenation of streams of Sets into a single stream of Entry<Long, Hotel>. Unfortunately, we cannot join Maps so easily, that is why there is this intermediate transformation.

  5. Finally, the stream of entries is collected into a Map representing the result of the sequence.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.