统计各点PM2.5的最大值

来源:互联网 发布:日本爱知时计 编辑:程序博客网 时间:2024/06/05 21:14

统计各点PM2.5的最大值

代码如下:

public class PM_Control {public static void main(String[] args) {String total = "东四=423;丰台花园=378;天坛=406;海淀区万柳=322;"+ "官员=398;通州=406;昌平镇=366;怀柔镇=248;定陵=306;"+ "前门=231;永乐店=422;古城=368;昌平镇=268;怀柔镇=423;"+ "定陵=267;前门=377;永乐店=299;秀水街=285";String[] arr = total.split("(=|;)") ;Map<String,Integer> map = new LinkedHashMap<String,Integer>();//key按照链表的形式存入,有顺序for(int i=0;i<arr.length;i+=2){//arr[i]为站点名  arr[i+1]为对应的PM2.5数据String name = arr[i];//将字符穿转换为Integer类型Integer number = Integer.parseInt(arr[i+1]);if( !map.containsKey(name) || number > map.get(name) ){map.put(name, number);}}//遍历map中的name和number输出Set<Entry<String,Integer>> set = map.entrySet();for(Entry<String,Integer> en : set){String name = en.getKey();Integer i = en.getValue();System.out.println("监测站点:"+name+"\tPM2.5浓度:"+i);}//遍历各站点PM2.5的最大值(遍历value)Collection<Integer> c = map.values();for(Integer in : c){System.out.print(in+" ");}System.out.println();//使用便利Key的方式遍历集合(也可使用遍历key的方式来得到value或遍历键值对)Set<String> ss = map.keySet();for(String key : ss){System.out.println("key:\t"+key+"\tvalue:\t"+map.get(key));}}}

测试结果如下:

监测站点:东四PM2.5浓度:423监测站点:丰台花园PM2.5浓度:378监测站点:天坛PM2.5浓度:406监测站点:海淀区万柳PM2.5浓度:322监测站点:官员PM2.5浓度:398监测站点:通州PM2.5浓度:406监测站点:昌平镇PM2.5浓度:366监测站点:怀柔镇PM2.5浓度:423监测站点:定陵PM2.5浓度:306监测站点:前门PM2.5浓度:377监测站点:永乐店PM2.5浓度:422监测站点:古城PM2.5浓度:368监测站点:秀水街PM2.5浓度:285423 378 406 322 398 406 366 423 306 377 422 368 285 key:东四value:423key:丰台花园value:378key:天坛value:406key:海淀区万柳value:322key:官员value:398key:通州value:406key:昌平镇value:366key:怀柔镇value:423key:定陵value:306key:前门value:377key:永乐店value:422key:古城value:368key:秀水街value:285


0 0