Java solution