Wednesday, December 9, 2020

Streams Cookbook

1.  Stream to Map

Map<Integer, String> result3 = list.stream().collect(
Collectors.toMap(x -> x.getId(), x -> x.getName()));

Map<String, Long> result2 = list.stream().collect(
Collectors.toMap(Hosting::getName, Hosting::getWebsites));

List<DataType> newList stream() .map(item -> { if (something) {
return item.get(bar).toString();
} else {
return item.somOtherObject;}}).collect(Collectors.toList());

Collector : 
    -Alternative to removal  through iterator.

No comments:

Post a Comment