Skip to content

Instantly share code, notes, and snippets.

@dehidehidehi
Created December 5, 2022 18:51
Show Gist options
  • Save dehidehidehi/267205fd151349e0bf25b408f389af4a to your computer and use it in GitHub Desktop.
Save dehidehidehi/267205fd151349e0bf25b408f389af4a to your computer and use it in GitHub Desktop.
Custom collector for a LinkedHashMap with values all set to Optional empty.
/**
* Custom collector for a {@link LinkedHashMap} with values all set to {@link Optional#empty()}
*/
private <T, R> Collector<T, ?, LinkedHashMap<T, Optional<R>>> toEmptyLinkedHashMap() {
return Collectors.toMap(Function.identity(), f -> Optional.empty(), (x, y) -> y, LinkedHashMap::new);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment