Flatten a nested list. Java version.