java version of lazy algorithms