Map_iterator & min()

2161 ワード

package com.test;

import java.util.HashMap;
import java.util.Map;

public class Test3
{
    public String test(Map<String, Integer> temp)
    {
        Object[] tempArray = temp.values().toArray();
        int[] countArray = new int[tempArray.length];

        for (int i = 0; i < tempArray.length; i++)
        {
            countArray[i] = (Integer)tempArray[i];
        }
        
        int min = countArray[0];
        for (int i = 0; i < countArray.length; i++)
        {
            if (min > countArray[i])
            {
                min = countArray[i];
            }
        }
        
        for (Map.Entry<String, Integer> entry : temp.entrySet())
        {
            if (entry.getValue().intValue() == min)
            {
                return entry.getKey();
            }
        }
        return null;
    }
    
    public static void main(String[] args)
    {
        Test3 test = new Test3();
        Map<String, Integer> coolMap = new HashMap<String, Integer>();
        coolMap.put("101", 12);
        coolMap.put("102", 1);
        coolMap.put("103", 4);
        coolMap.put("104", 2);
        coolMap.put("105", 6);
        coolMap.put("106", 5);
        coolMap.put("107", 12);
        coolMap.put("108", 345);
        String rs = test.test(coolMap);
        System.out.println(rs);
    }
}

 Map temp
 
        for (Map.Entry entry : temp.entrySet())        {            System.out.println(entry.getKey() + entry.getValue());        }
 
=======================
int[] countArray = ~~~~~~
int min = countArray[0];for (int i = 0; i < countArray.length; i++){    if (min > countArray[i])   {        min = countArray[i];    }}
======