added the solution I found in Java