My question maybe related to this question - Encapsulating Internal Data Structure

I have a class LCTile.java which contains a Coordinate.java class. Coordinate.java that contains latitude and longitude(essentially a Point2D.java). Now I wish to sort the coordinates by latitude or longitude for which I have already developed a CoordinateComparator class. The Java API requires you pass in the List to be sorted and the Comparator i.e. Collections.sort(List list ,Comparator comparator). My question is the following - In order to sort the various tiles by latitude or longitude I have to obtain all the coordinate classes in a list from the original LCTile class and then pass them to the Collections.sort interface and then map them back to the original tile(s) after the sort operation is over. Is the encapsulation of the Coordinate class inside the LCTile absolutely essential or can this be done in a different way ?


You can create a comparator that compares the coordinates of the tile. In java 8 there is a helper function for that:

Comparator<LCTile> cmp = Comparator.comparing(

Then you can pass that with the list of tiles to Collections.sort.

| improve this answer | |
  • in essence you are approving my current design that I should hold onto a Coordinate object inside LCTile.java. Is that correct ? – gansub Aug 21 '15 at 11:12
  • You can replace LCTile::getCoordinate with any other function that will take a Tile and return it's matching Coordinate. This could be a function in Board. However it's more efficient to put it in Tile. – ratchet freak Aug 21 '15 at 11:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.