I often find myself in a situation where I need to create Map objects from a set or List . The key is usually a String or Enum string or the like, and the value is some new object with merged data. The usual way to do this, for my part, is to first create a Map<String, SomeKeyValueObject> , and then iterate over the set or list that I enter and modify my newly created map.
As in the following example:
class Example { Map<String, GroupedDataObject> groupData(final List<SomeData> list){ final Map<String, GroupedDataObject> map = new HashMap<>(); for(final SomeData data : list){ final String key = data.valueToGroupBy(); map.put(key, GroupedDataObject.of(map.get(key), data.displayName(), data.data())); } return map; } } class SomeData { private final String valueToGroupBy; private final Object data; private final String displayName; public SomeData(final String valueToGroupBy, final String displayName, final Object data) { this.valueToGroupBy = valueToGroupBy; this.data = data; this.displayName = displayName; } public String valueToGroupBy() { return valueToGroupBy; } public Object data() { return data; } public String displayName() { return displayName; } } class GroupedDataObject{ private final String key; private final List<Object> datas; private GroupedDataObject(final String key, final List<Object> list) { this.key = key; this.datas = list; } public static GroupedDataObject of(final GroupedDataObject groupedDataObject, final String key, final Object data) { final List<Object> list = new ArrayList<>(); if(groupedDataObject != null){ list.addAll(groupedDataObject.datas()); } list.add(data); return new GroupedDataObject(key, list); } public String key() { return key; } public List<Object> datas() { return datas; } }
It is very unclean. We create a card, and then mutate it again and again.
I liked the use of java 8s Stream and the creation of non-mutating data structures (more precisely, you don't see the mutation). So, is there a way to turn this data grouping into something that uses a declarative approach rather than an imperative way?
I tried to implement a sentence in https://stackoverflow.com/a/167129/129 , but I seem to stumble. Using the approach in the answer (suggestion to use Collectors.groupingBy and Collectors.mapping ), I can get data sorted by map. But I canβt group the βdataβ into the same object.
Is there a way to do this in a declarative way, or am I imperative?