migrate the java algorithm to a better one

Signed-off-by: fabio <fbi.gritti@gmail.com>