hashmap的使用
package tcc.test.collection;
import java.util.HashMap;
import java.util.Map;
/**
* @author tcc:
* @version 创建时间:2020年12月8日 下午7:39:28
* 类说明
*/
public class HashMapTest {
public static void main(String[] args) {
// 声明HashMap对象
Map<String,Integer> map= new HashMap<>();
//添加数据
map.put("ZhangYi",98);
map.put("WangEr",99);
map.put("ZhangShan",89);
map.put("Lisi",92);
//根据键值对键值获取数据‘’
int value=map.get("Lisi");
System.out.println("kay:Lisi And value:"+value);
//获取Map中键值对的个数
int size=map.size();
System.out.println("map 中的键值对个数为:"+size);
//判断Map集合中是否包含键为key的键值对
boolean b1=map.containsKey("LiSI");
boolean b2=map.containsKey("Lisi");
System.out.println("是否包含键值为LiSI的键值对数据:"+b1);
System.out.println("是否包含键值为Lisi的键值对数据:"+b2);
//判断Map集合中是否包含值为value的键值对
boolean b3=map.containsValue(99);
boolean b4=map.containsValue(100);
System.out.println("是否包含值为99的键值对数据:"+b3);
System.out.println("是否包含值为100的键值对数据:"+b4);
//判断Map集合中是否没有任何键值对
boolean b5=map.isEmpty();
System.out.println("map中键值对数据是否为空:"+b5);
//根据键值删除Map中键值对
int value2=map.remove("Lisi");
System.out.println("删除了键为Lisi的键值对数据,其值为:"+value2);
boolean b6=map.containsKey("Lisi");
System.out.println("是否包含键值为Lisi的键值对数据:"+b6);
//清空Map集合中所有的键值对
map.clear();
boolean b7=map.isEmpty();
System.out.println("map中键值对数据是否为空:"+b7);
遍历hashmap
JSONArray data_person_jsonArray = JSONArray.parseArray(data_person_string);
List<JSONObject> jsonObjects = new ArrayList<JSONObject>();
Map map = new HashMap();
if (data_person_jsonArray.size()>0) {
for(int i = 0 ; i<data_person_jsonArray.size();i++){
JSONObject json_family_hc = JSONObject.parseObject(data_person_jsonArray.get(i).toString());
String cyxm = json_family_hc.getString("xm");//姓名
String cysfz = json_family_hc.getString("sfzh");//姓名
log.info("身份证=="+cysfz+";姓名=="+cyxm);
JSONObject jsonObject = new JSONObject();
jsonObject.put("xm", cyxm);
jsonObject.put("sfz",cysfz);
jsonObjects.add(jsonObject);
map.put(i, cysfz);
msg = "{\"success\" : true,\"errorCode\" : \"200\", \"errorMsg\" : \"公安信息查询完成\", \"data\" :" +jsonObjects + "}";
}
}
//婚姻得接口
HyWsManager hyWsManager = new HyWsManager();
for (Object key : map.keySet()) {
String dsfz = map.get(key).toString().replace("\"", "");
JsonObject json_hy = null;
try{
json_hy = hyWsManager.getHyhjxx(dsfz,"1");//通过婚姻接口完善户籍信息
}catch(Exception e){
log.info(e);
continue;
}
if("{}".equals(json_hy.toString()) || map.containsValue(json_hy.get("dsfz").toString().replace("\"", "")) ){
continue;
}else{
JSONObject jsonObject = new JSONObject();
jsonObject.put("xm", json_hy.get("dxm").toString().replace("\"", ""));
jsonObject.put("sfz",json_hy.get("dsfz").toString().replace("\"", ""));
jsonObjects.add(jsonObject);
msg = "{\"success\" : true,\"errorCode\" : \"200\", \"errorMsg\" : \"公安信息查询完成\", \"data\" :" +jsonObjects + "}";
log.info("msg======"+msg);
}
}