Collection algorithms in Java