Added the same implementation in Java