Java-8: streaming or simpler solution?

I have two models, List<ModelA>and I want to convert it to List<ModelB>. Here are my models:

class ModelA {

    private Long id;
    private String name;
    private Integer value;

    public ModelA(Long id, String name, Integer value) {
        this.id = id;
        this.name = name;
        this.value = value;
    }

    public Long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public Integer getValue() {
        return value;
    }
}

class ModelB {
    private Long id;
    private Map<String, Integer> valuesByName;

    public ModelB(Long id, Map<String, Integer> valuesByName) {
        this.id = id;
        this.valuesByName = valuesByName;
    }

    public Long getId() {
        return id;
    }

    public Map<String, Integer> getValuesByName() {
        return valuesByName;
    }
}

Actual solution:

public static List<ModelB> convert(List<ModelA> models) {
        List<ModelB> toReturn = new ArrayList<>();
        Map<Long, Map<String, Integer>> helper = new HashMap<>();
        models.forEach(modelA -> {
            helper.computeIfAbsent(modelA.getId(), value -> new HashMap<>())
                    .computeIfAbsent(modelA.getName(), value -> modelA.getValue());
        });
        helper.forEach((id, valuesByName) -> toReturn.add(new ModelB(id,valuesByName)));
        return toReturn;
    }

But I think there is a simpler solution, do you know how I can do this in one thread or somehow simplify it?

EDIT . I want to clarify that I cannot use java9, and I need to group them by identifiers, and then by name. If in ModelB I have 4 elements with the same identifier, I do not want new instances of ModelA.

+6
source share
2 answers

, - , name, value id.

models.stream()
        .collect(Collectors.groupingBy(model -> model.getId(), //ModelA::getId - Using method reference
                Collectors.toMap(model -> model.getName(), model -> model.getValue(), (map1, map2) -> map1)))
        .entrySet()
        .stream()
        .map(entry -> new ModelB(entry.getKey(), entry.getValue()))
        .collect(Collectors.toList());

EDIT:

(map1, map2) -> map1 . value id, name ( computeIfAbsent ) ( ), IllegalStateException, .

+7

map Stream:

public static List<MobelB> convert(List<ModelA> models) {
  Map<Long, Map<String, Integer>> modelAMap = models.stream()
    .collect(Collectors.toMap(ModelA::getId, modelA -> computeMap(modelA)));

  return models.stream()
    .map(modelA -> new ModelB(modelA.getId(), modelAMap.get(modelA.getId())))
    .collect(Collectors.toList());
}

private static Map<String, Integer> computeMap(ModelA model) {
  Map<String, Integer> map = new HashMap<>();
  map.put(model.getId(), model.getName());
  return map;
}
-1

All Articles