Rahul Rahul - 2 months ago 9
Java Question

How to compare String with HashMap

I want to compare input text with Hashmap key
I have stored Latlang values in Array list.
And i have 1 Map in that i Stored the key as city name and their coordinates.
now i want to search city in hashmap so that i can mark that points on map



private GoogleMap mMap;
LocationManager locationManager;
CoordinatorLayout mainCoordinatorLayout;
private MarkerOptions options = new MarkerOptions();
private ArrayList<LatLng> seawood = new ArrayList<>();
private ArrayList<LatLng> andhe = new ArrayList<>();
Map<String, LatLng>city = new HashMap<String, LatLng>();
EditText text;
Button search;

public static final String TAG = "gplaces";

// remember to change the browser api key
public static final String GOOGLE_BROWSER_API_KEY = "AIzaSyC79f__haC6E7ZbuO7j9HULq3VGD3pX5-E";

public static final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!isGooglePlayServicesAvailable()) {
finish();
}
setContentView(R.layout.activity_neighborhood);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);


text =(EditText) findViewById(R.id.text);
search = (Button)findViewById(R.id.search);


LatLng seawoods = new LatLng(19.0221178,73.0343958);
LatLng panvel = new LatLng(19.0221178,73.0343958);
LatLng vashi = new LatLng(19.0221178,73.0343958);
LatLng vadala = new LatLng(19.0221178,73.0343958);
LatLng andheri = new LatLng(19.097671,72.8629984);
LatLng bandra = new LatLng(19.0221178,73.0343958);
LatLng chembur = new LatLng(19.0221178,73.0343958);
LatLng sanpada = new LatLng(19.0221178,73.0343958);

city.put("Seawood", seawoods);
city.put("Panvel",panvel);
city.put("Vashi",vashi);
city.put("Vadala",vadala);
city.put("Andheri",andheri);
city.put("Bandra",bandra);
city.put("Chembur",chembur);
city.put("Sanpada",sanpada);

seawood.add(new LatLng(19.0193544, 73.0602836));
seawood.add(new LatLng(19.0193544,73.0602836));
seawood.add(new LatLng(19.0157782,73.0532021));
seawood.add(new LatLng(19.0157782,73.0532021));
seawood.add(new LatLng(19.0043141,73.028374));

andhe.add(new LatLng(19.1117702,72.8645361));
andhe.add(new LatLng(19.1117702,72.8645361));
andhe.add(new LatLng(19.1117702,72.8645361));

search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for(int i = 0; i < city.size(); i++) {

if ( text.getText().toString().equals(city.containsKey(i))) {

set(mMap, city.values(i));
}
}
}
});


}
private List<Map.Entry<String, LatLng>> getSearchResults() {
List<Map.Entry<String, LatLng>> results = new ArrayList<>();
for (Map.Entry<String, LatLng> entry : city.entrySet()) {
if (entry.getKey().toLowerCase().startsWith(text.getText().toString())) {
results.add(entry);
}
}
return results;
}

public void set(GoogleMap map, LatLng l){

mMap = map;
for (LatLng point : seawood) {
options.position(point);
options.title("someTitle");
options.snippet("someDesc");
map.addMarker(options);
}
LatLng seawoods = new LatLng(19.0221178,73.0343958);
// Add a marker in Sydney and move the camera

map.moveCamera(CameraUpdateFactory.newLatLng(seawoods));
mMap.getUiSettings().setCompassEnabled(true);
mMap.getUiSettings().setZoomControlsEnabled(true);
map.setMinZoomPreference(12.0f);
map.setMaxZoomPreference(14.0f);
}

@Override
public void onMapReady(GoogleMap googleMap) {

mMap = googleMap;


for (LatLng point : seawood) {
options.position(point);
options.title("someTitle");
options.snippet("someDesc");
googleMap.addMarker(options);
}
LatLng seawoods = new LatLng(19.0221178,73.0343958);
// Add a marker in Sydney and move the camera

googleMap.moveCamera(CameraUpdateFactory.newLatLng(seawoods));
mMap.getUiSettings().setCompassEnabled(true);
mMap.getUiSettings().setZoomControlsEnabled(true);
googleMap.setMinZoomPreference(12.0f);
googleMap.setMaxZoomPreference(14.0f);

}




Answer

Do you want strict comparisons? If so the hash map is directly build for that. Using variable names from your code:

LatLng result = city.get(text.getText().toString());

Alternatively, if you want more loose comparison, you have to iterate over your hash-map yourself. Say you want to find those entires that only begin with your search string and be case-insensitive, you'd do something like this:

    private List<Map.Entry<String, LatLng>> getSearchResults() {
    List<Map.Entry<String, LatLng>> results = new ArrayList<>();
      for (Map.Entry<String, LatLng> entry : city.entrySet()) {
        if (entry.getKey().toLowerCase().startsWith(text.getText().toString().toLowerCase())) {
            results.add(entry);
        }
      }
    return results;
    }

This will return a List of size 0 or greater, containing Map.Entry<String, LatLng>>

To access the Xth item's data you would do:

String cityName = results.get(X).getKey();
LatLng cityLatLng = results.get(X).getValue();

Edit to answer your edit:

The things you put in your on-click function seems incorrect. I would do something like:

search.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        List<Map.Entry<String, LatLng>> results = getSearchResults();
        if (results.size() > 0) {
            // in this case we have at least 1 result, so go to it
            set(mMap, results.get(0).getValue());
        } else {
            // in this case we have no results - handle it as you want
        }
    });
Comments