Skip to content

Instantly share code, notes, and snippets.

@lukaseder
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukaseder/9895576 to your computer and use it in GitHub Desktop.
Save lukaseder/9895576 to your computer and use it in GitHub Desktop.
void test() {
// Filling collections with values, similar to
// Collections.fill();
List<Optional<Number>> optionalList1 = new ArrayList<>();
List<Number> list1 = new ArrayList<>();
fillOptional(optionalList1, 1);
fillOptional(optionalList1, 1.0);
fill(list1, 1);
fill(list1, 1.0);
}
<T> void fill(List<? super T> list, T value) {
ListIterator<? super T> it = list.listIterator();
while (it.hasNext()) {
it.next();
it.set(value);
}
}
<T> void fillOptional(List<Optional<T>> list, T value) {
ListIterator<Optional<T>> it = list.listIterator();
while (it.hasNext()) {
it.next();
it.set(Optional.of(value));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment