private HeightInfo findNearest( List<HeightInfo> positions, HeightInfo actual, HeightInfo nearest) { double minLength = Double.MAX_VALUE; Collections.shuffle(positions); for (HeightInfo i : positions) { if (i.equals(actual)) { return i; } double tempLenght = getLength(i.getX(), nearest.getX(), i.getZ(), nearest.getZ()); if (minLength < 0.5) { break; } if (tempLenght < minLength) { minLength = tempLenght; nearest = i; } } return nearest; }