Java Collection Utils - ashishranjandev/developer-wiki GitHub Wiki
Converting Java Iterable to Collection
List<String> result = IterableUtils.toList(iterable);
List<String> result = IteratorUtils.toList(iterator);
Sorting Collections
list.sort(Comparator.comparing(AnObject::getAttr));
// Sorting in reverse order
Comparator<Employee> employeeNameComparator
= Comparator.comparing(Employee::getName);
Comparator<Employee> employeeNameComparatorReversed
= employeeNameComparator.reversed();
Get Count
long i = response.getNodes()
.stream()
.filter(node -> node.getId() != null)
.peek(node -> node.setValue("This is the id: " + node.getId()))
.count();
integers.values().stream().mapToInt(Integer::intValue).sum();
LongAdder a = new LongAdder();
map.values().parallelStream().forEach(a::add);
sum = a.intValue();
Collectors
To Map
// Creating a map of Books with Release Year as the Key
books.stream().collect(
Collectors.toMap(Book::getReleaseYear, Function.identity()));
Get a Mapping of Item and Count
Map<String, Long> counted = list.stream()
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));