Skip to content

Instantly share code, notes, and snippets.

@jpetitto
Created October 29, 2015 15:41
Show Gist options
  • Save jpetitto/9db54c105fed2acd628b to your computer and use it in GitHub Desktop.
Save jpetitto/9db54c105fed2acd628b to your computer and use it in GitHub Desktop.
Least Hacky Loop
List<Item> allBucket = ...
List<Item> genderBucket = ...
List<Item> sizeBucket = ...
List<Item> colorBucket = ...
List<Item> recommendations = new ArrayList<>();
picker(recommendations, limit, colorBucket, sizeBucket, genderBucket, allBucket);
private <T> int picker(List<T> src, List<T> dst, int limit) {
Iterator<T> iterator = src.iterator();
while (iterator.hasNext() && limit > 0) {
dst.add(iterator.next());
limit--;
}
return limit;
}
private <T> void picker(List<T> dst, int limit, List<T>... sources) {
for (List<T> src : sources) {
while (picker(src, dst, limit) > 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment