HashMap 移除对应 ID 的对象 - 代码示例与解析
HashMap\u003CString, MyInfoWindow\u003E infoWindowMap = new HashMap\u003C\u003E();\nMyInfoWindow infoWindow = infoWindowMap.get(bean1.getId());\n if (infoWindow == null) {\n infoWindow = new MyInfoWindow(mapView, bean1.getName(), description, bean1.getId());\n infoWindow.open(getContext(), new GeoPoint(bean1.getLatitude(), bean1.getLongitude()), 0, -30);\n infoWindowMap.put(bean1.getId(), infoWindow);\n infoWindow.open(getContext(), new GeoPoint(bean1.getLatitude(),bean1.getLongitude()), 0, -30);\n\n } else {\n infoWindow.update(new GeoPoint(bean1.getLatitude(),bean1.getLongitude()), bean1.getName(), bean1.getCaluesdata());\n }\n\n for ( int i= 0;i\u003CcustomBeans.size();i++){\n if (infoWindowMap.get(bean.getData().getTargetId())!= null){\n infoWindowMap.get(bean.getData().getTargetId()).close();\n infoWindowMap.get(bean.getData().getTargetId()).onDetach();\n infoWindowMap.remove( infoWindowMap.get(bean.getData().getTargetId()));\n \n } \n}该代码示例演示了如何使用 HashMap 存储和移除与特定 ID 关联的对象。\n\n代码使用 HashMap\u003CString, MyInfoWindow\u003E 存储 MyInfoWindow 对象,每个对象通过其 ID 作为键值进行存储。\n\n首先,代码检查 infoWindowMap 中是否存在与特定 ID 关联的对象。如果不存在,则创建一个新的 MyInfoWindow 对象并将其存储在 HashMap 中,同时设置其 ID 作为键值。\n\n如果对象已经存在,则更新该对象的属性。\n\n接下来,代码遍历一个名为 customBeans 的列表。对于列表中的每个元素,代码检查 infoWindowMap 中是否存在与该元素的目标 ID 关联的对象。\n\n如果存在,则执行以下操作:\n* 关闭与目标 ID 关联的 infoWindow 对象。\n* 从 infoWindowMap 中移除与目标 ID 关联的对象。\n\n通过这种方式,代码可以根据特定 ID 移除与之关联的对象。\n\n该示例展示了使用 HashMap 进行对象存储和移除的典型操作,它可以根据需要进行修改以适应不同的应用场景。
原文地址: https://www.cveoy.top/t/topic/pq59 著作权归作者所有。请勿转载和采集!