简体   繁体   中英

compare two hashtable values and put results into an arraylist

I'm a little stuck with the way I want to compare and collect data from hash tables that I am creating. The code is a little messy as I am now getting a little confused on where to go next, I apologise.

I have two Hashtables. Each table holds keys which are co-ordinates. One table holds the longitudes, the other the latitudes. The values of each side hold a location. What I want to do is compare the two tables values, so if the Strings are the same, then the Key and Value can be put into a separate ArrayLists. A coordinates list, maybe one for lat and one for lon to make the gps feature easier, and then a location list.

Here is the code :

    public static void trails(String[] args) {

    Hashtable<Double, String> trailLat = new Hashtable<Double, String>();

    trailLat.put(51.7181283, "Bike Park Wales");
...


    Hashtable<Double, String> trailLon = new Hashtable<Double, String>();

    trailLon.put(-3.3633637, "Bike Park Wales");
...


    if ( trailLat.keys() >= userLatL && trailLat.keys() <= userLatH ) {
        trailLat.values().retainAll(trailLon);
        ArrayList<Item> items = new ArrayList<Item>(trailLat.values());
    }
        ....

I've only included the latitude part of the code as I would think it's just repeated.

The 'userLatL' and 'userLatH' are the users location coordinates 20mile radius boundaries. The idea is to return the keys and values that fall within that number difference/20 mile radius.

Cheers in advance! Any help would be really appreciated!

I would use this approach

class Trail{
    String name;
    double lat,lon;
}

double LAT_MIN, LAT_MAX, LON_MIN, LON_MAX;
List<Trail> trails = new ArrayList<Trail>();
//populate trails from db or whatever

List<Trail> goodTrails = new ArrayList<Trail>();
for(Trail trail : trails){
    if(trail.lat > LAT_MIN && trail.lat < LAT_MAX && trail.lon > LON_MIN && trail.lon < LON_MAX ){
        goodTrails.add(trail);
    }
}

Even though you are looking at 20mile radius, you would have to take a spherical shape of the earth in the account if that radius ever grows bigger.

Also you would need some better structure instead of 2 hashmaps where the keys are lon/lat values. You can use a simple table to store all the points (in the database or as a list of objects).

I don't want to use other people's work, everything is explained here:

http://janmatuschek.de/LatitudeLongitudeBoundingCoordinates

Good Luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM