对Map进行排序

/**

     * 对数据集Map进行排序

     * 

     * @param map

     * @return

     * @Description:

     */

    public Map<String, Integer> sortMapByStringInteger(Map<String, Integer> map)

    {

        Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();

        if (map != null && !map.isEmpty())

        {

            List<Map.Entry<String, Integer>> entryList = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());

            Collections.sort(entryList, new Comparator<Map.Entry<String, Integer>>()

            {

                public int compare(Entry<String, Integer> entry1, Entry<String, Integer> entry2)

                {

                    if (entry1.getValue() >= entry2.getValue())

                    {

                        return 1;

                    }

                    else

                    {

                        return -1;

                    }

                }

            });

            Iterator<Map.Entry<String, Integer>> iter = entryList.iterator();

            Map.Entry<String, Integer> tmpEntry = null;

            while (iter.hasNext())

            {

                tmpEntry = iter.next();

                sortedMap.put(tmpEntry.getKey(), tmpEntry.getValue());

            }

        }

        return sortedMap;

    }

猜你喜欢

转载自gaochunhu.iteye.com/blog/2392828
今日推荐