1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to use java 8 streams to get more readability and performance to this code

Discussion in 'Programming/Internet' started by Yan Rocha, Oct 8, 2018.

  1. Yan Rocha

    Yan Rocha Guest

    I got a forms list, and it has a tags list. I received from my front end app an array of tags (String), gotta filter these forms by these tags.

    forms.forEach(form -> {
    br.com.softplan.ungp.dynamic.form.api.model.Form formJson = new Gson()
    .fromJson(form.getSchema(), br.com.softplan.ungp.dynamic.form.api.model.Form.class);

    tags.forEach(tag -> formJson.getTags().forEach(formTag -> {
    if (tag.toUpperCase().equals(formTag.getText().toUpperCase())) {
    if (map.containsKey(tag)) {
    List<FormSummary> list = new ArrayList<>(map.get(tag));
    list.add(new FormSummary(form.getId(), form.getName()));
    map.put(tag,list);
    } else {
    map.put(tag, Collections.singletonList(new FormSummary(form.getId(), form.getName())));
    }
    }
    }));
    });


    This code is already working as expected. I just want to improve it, writing less code using java utilities.

    What I was trying to do is to map all the result inside a map into its own iteration, using stream.map() or the Collectors.toMap(), but couldn't achieve what I wanted.

    Login To add answer/comment
     

Share This Page