I have a Unit
class which has lots of fields in it as shown below:
public class Unit {
private final int id;
private final int beds;
private final String city;
private final double lat;
private final double lon;
// constructors and getters here
// toString method
}
I now have a List of Unit
which is a List
object which contains lots of Units. Now I need to find the nearest Units from List
object to Unit x
. Cap the results by limit.
private List<Unit> nearestUnits(List<Unit> lists, Unit x, int limit) {
List<Unit> output = new ArrayList<>();
// how do I sort lists object in such a way so that I can get nearest units here to "x"?
return output;
}
We have lat/long present in the Unit
class so we can use that to calculate euclidean distance and do the comparison. I am confused on how to sort the list of units by shortest distance and get the nearest units. I am working with Java 7 as of now so I can't use Java 8.