I need to split one list to multiple list based on some property.
List<Custom1> nums = new ArrayList<>();
List<Custom2> cats = new ArrayList<>();
List<Custom3> forms = new ArrayList<>();
list.stream().forEach(custom -> {
if (custom.getType().equalsIgnoreCase("N")) {
Custom1 attr = new Custom1();
attr.setAttrCd(custom.getCode());
attr.setValue(Float.parseFloat(custom.getValue()));
nums.add(attr);
} else if (custom.getType().equalsIgnoreCase("C")) {
Custom2 attr = new Custom2();
attr.setAttrCd(custom.getCode());
attr.setValue(Integer.parseInt(custom.getValue()));
cats.add(attr);
} else if (custom.getType().equalsIgnoreCase("F")) {
Custom3 attr = new Custom3();
attr.setAttrCd(custom.getCode());
attr.setValue(custom.getValue());
forms.add(attr);
}
});
The same code can be made better with stream filter.
list.stream().filter(...).collect(toList())
but i need to iterate 3 times in my case. Is there any other better way?