繁体   English   中英

如何比较String和HashMap

[英]How to compare String with HashMap

我想将输入文本与将Latlang值存储在Array列表中的Hashmap键进行比较。 我有1个地图,其中我将密钥存储为城市名称及其坐标。 现在我想在哈希图中搜索城市,以便我可以在地图上标记这些点

  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); } 

您要严格比较吗? 如果是这样,则直接为此构建哈希映射。 使用代码中的变量名:

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

另外,如果您想进行更宽松的比较,则必须自己遍历哈希映射。 假设您要查找仅以搜索字符串开头且不区分大小写的那些整数,则可以执行以下操作:

    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;
    }

这将返回一个大小为0或更大的List ,其中包含Map.Entry<String, LatLng>>

要访问第X个项目的数据,请执行以下操作:

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

编辑以回答您的编辑:

您在单击功能中放入的内容似乎不正确。 我会做类似的事情:

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
        }
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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