package test;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
//找出每个区域最大的PM2.5数据
public class testCollection {

    public static void getMaxData(String[] positions,int[] datas){
        HashMap<String,Integer> hashMap = new HashMap<String, Integer>();
        for(int i = 0;i<positions.length;i++){
            String position = positions[i];
            if(!(hashMap.containsKey(position))||datas[i]>hashMap.get(position)){
                hashMap.put(position,datas[i]);
            }
        }
        Set<Map.Entry<String,Integer>> entrySet = hashMap.entrySet();
        for(Map.Entry<String,Integer> set:entrySet){
            System.out.println("区域为"+set.getKey()+"检测到的PM2.5最大数据为"+set.getValue());
        }

    }

    public static void main(String[] args) {
        String[] positions = {"王府井","天坛","通州","昌平","农展馆","怀柔","朝阳","海淀","王府井",
                "通州","海淀","农展馆","王府井","顺义","昌平","朝阳","顺义"};
        int[] datas = {211,322,363,267,356,376,235,265,357,355,210,201,377,399,393,288,299};

        testCollection.getMaxData(positions,datas);

    }

}

  结果

区域为农展馆检测到的PM2.5最大数据为356
区域为海淀检测到的PM2.5最大数据为265
区域为通州检测到的PM2.5最大数据为363
区域为怀柔检测到的PM2.5最大数据为376
区域为王府井检测到的PM2.5最大数据为377
区域为天坛检测到的PM2.5最大数据为322
区域为昌平检测到的PM2.5最大数据为393
区域为朝阳检测到的PM2.5最大数据为288
区域为顺义检测到的PM2.5最大数据为399

  

版权声明:本文为seven717原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/seven717/p/12885256.html