Basavaraj
Basavaraj

Reputation: 1111

Collect HashMap<String,Object> from Stream<T>

Using Map of key to iterate and based on condition returning HashMap,need to collect return map below code.

trying to convert below java code in java 8

 for (String key :  sectionsJson.keySet()) {
            Map<String, Object> section = (Map<String, Object>) sectionsJson.get(key);
            if (index == (Integer) section.get(SECTION_FIELD_KEY_INDEX)) {
                section.put(SECTION_FIELD_KEY_SECTION_KEY, key);
                return section;
            }
        }

any suggestion.

Upvotes: 0

Views: 312

Answers (3)

SubOptimal
SubOptimal

Reputation: 22973

Depending on what you want to achieve following might be also possible solutions:

simplifying the loop

for (Map.Entry<String, Map<String, Object>> entry : sectionsJson.entrySet()) {
    Map<String, Object> section = entry.getValue();
    if (index == section.get(SECTION_FIELD_KEY_INDEX)) {
        section.put(SECTION_FIELD_KEY_SECTION_KEY, entry.getKey());
        return section;
    }
}
// add the code for the case when no section was found

separate stream processing and mutating the element

// find the section
Optional<Map.Entry<String, Map<String, Object>>> first = sectionsJson.entrySet().stream()
        .filter(e -> (Integer) e.getValue().get(SECTION_FIELD_KEY_INDEX) == index)
        .findFirst();
// mutate the section
if (first.isPresent()) {
    Map.Entry<String, Map<String, Object>> sectionJson = first.get();
    sectionJson.getValue().put(SECTION_FIELD_KEY_SECTION_KEY, sectionJson.getKey());
    return sectionJson.getValue();
}
// add the code for the case when no section was found

Upvotes: 0

Nawnit Sen
Nawnit Sen

Reputation: 1038

As per your existing code. You are returning the map as soon as it finds any match. Same thing you can do using java 8 as well.

  Optional<Integer> findAny = sectionsJson.keySet().stream().filter(key -> {
        Map<String, Object> section = (Map<String, Object>)sectionsJson.get(key);
        if (index == (Integer)section.get("SECTION_FIELD_KEY_INDEX")) {
            section.put("SECTION_FIELD_KEY_SECTION_KEY", key);
            return true;
        }
        return false;

    }).findFirst();
    if (findAny.isPresent()) {
        System.out.println(sectionsJson.get(findAny.get()));
    }

Upvotes: 0

Eran
Eran

Reputation: 393811

It looks like you want to produce a Map having at most a single entry.

Map<String,Object> map = 
    sectionsJson.entrySet()
                .stream()
                .filter(e -> {
                    Map<String, Object> section  = e.getValue ();
                    return index == (Integer) section.get(SECTION_FIELD_KEY_INDEX);
                }
                .map(e -> new SimpleEntry<> (SECTION_FIELD_KEY_SECTION_KEY, e.getKey ()))
                .limit(1)
                .collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue));

It looks like your original code is simpler.

Perhaps you can simply search for the desired key:

String value =
    sectionsJson.entrySet()
                .stream()
                .filter(e -> {
                    Map<String, Object> section  = e.getValue ();
                    return index == (Integer) section.get(SECTION_FIELD_KEY_INDEX);
                }
                .map(Map.Entry::getKey)
                .findFirst()
                .orElse(null);

since you are producing a Map having (at most) a single value and a constant key, so the value is the only data the Stream pipeline should be searching for.

Upvotes: 1

Related Questions