You can use Stream's reduce method, like:
final List<Div> divs = Arrays.asList( new Div("1", "A"), new Div("2", "B") ); final List<Map<String, String>> result = divs.stream() .reduce(new ArrayList<>(), (list, div) -> { list.add(new HashMap<String, String>() {{ put("id", div.getId()); put("name", div.getName()); }}); return list; }, (a, b) -> a);
Reduce method expects 3 parameters:
- initial value (empty list in this case)
BiFunction that provides 2 parameters: accumulator (type List<Map<String,String>> in your case) and current value taken from stream iteration (Div object) and returns a value of accumulator type - comparator function (in this case lambda
(a,b) -> a is enough.
This approach pretty similar to folding known from functional programming.