简体   繁体   English

遍历hasmap并使用键值

[英]Iterate through hasmap and use key value

I'm trying to iterate through a hashmap and and use the key during my iteration. 我正在尝试遍历hashmap并在迭代过程中使用密钥。 I decided to use it inside an Async Task because the iteration was blocking my UI thread. 我决定在Async Task中使用它,因为迭代阻塞了我的UI线程。

My hashmap has LatLng as the Key and a Marker as the Object, when I get the key during Iteration and pass it to a new LatLng that works fine but when I try to use this new LatLng I get a java.util.ConcurrentModificationException exception. 当在迭代期间获取密钥并将其传递给可以正常工作的新LatLng时,我的哈希图将LatLng作为键,将Marker作为对象,但是当我尝试使用此新LatLng时,我得到了java.util.ConcurrentModificationException异常。 Strangely this doesn't happen when I use the code without the AsyncTask 奇怪的是,当我使用没有AsyncTask的代码时,这不会发生

Here is my code 这是我的代码

    class AddCityMarker extends AsyncTask<Integer, Integer, String> {

    protected String doInBackground(Integer... counter) {           

        //CityMarker cMarker;
        LatLng marker_loc;

        List<String> city_markers = new ArrayList<String>();
        if(displayed.size() > 0){

            Iterator<HashMap.Entry<LatLng, Marker>> myIterator = displayed.entrySet().iterator();
            while(myIterator.hasNext()) {
                //cMarker = new CityMarker();
                HashMap.Entry<LatLng, Marker> entry = myIterator.next();                    
                marker_loc = entry.getKey();

                Log.i("ZOOM", "Key = " + marker_loc + ", Value = " + entry.getValue());

                List<Address> addresses = null;                     
                try {
                  //This is where I get the error
                    addresses = gcode.getFromLocation(marker_loc.latitude, marker_loc.longitude, 1);
                } catch (IOException e) {
                    e.printStackTrace();
                }

                if(addresses.size() > 0) {  
                    if(!city_markers.contains(addresses.get(0).getLocality())){
                        city_markers.add(addresses.get(0).getLocality());
                        //map.addMarker(new MarkerOptions().position(marker_loc).snippet("CITY"));
                    }                           
                }
            }                   
        }


        return null;
    }

    protected void onPostExecute(String jsonResult) {

        try {   

            if(isClubMarkers){
                map.clear();
                isClubMarkers = false;
            }
            displayed.clear();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From Android 1.6 to Android HoneyComb, the async tasks are running in parallel mode. 从Android 1.6到Android HoneyComb,异步任务以并行模式运行。 That means like different threads. 这意味着像不同的线程。 Some times in your case, you might be calling the async task more than one time and they may try to access the same HashMap for the itteration. 在某些情况下,您可能多次调用异步任务,并且他们可能会尝试访问同一HashMap来进行发送。 You can avoid this exception by using "ConcurrentHashMap" instead of HashMap. 您可以通过使用“ ConcurrentHashMap”代替HashMap来避免此异常。 But the proper way is to identify whether the async task is calling more than once and try to avoid it if its there. 但是正确的方法是识别异步任务是否正在多次调用,并尝试避免异步任务在那里。 This can also happen if you tries to operate with the Hashmap when the Async task itterates though the same. 如果您在异步任务触发时仍尝试使用Hashmap,则也会发生这种情况。 If thats the case use "ConcurrentHashMap" which is thread safe. 如果是这种情况,请使用线程安全的“ ConcurrentHashMap”。

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

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