zoukankan      html  css  js  c++  java
  • 对HashMap中的实体类进行排序

    [java] view plaincopyprint?
    1. package demo20130414;  
    2.   
    3. import java.util.Collection;  
    4. import java.util.Collections;  
    5. import java.util.Comparator;  
    6. import java.util.HashMap;  
    7. import java.util.Iterator;  
    8. import java.util.Map;  
    9. import java.util.Map.Entry;  
    10. import java.util.Set;  
    11. import java.util.ArrayList;  
    12. import java.util.List;  
    13. public class CharCounterDemo {  
    14.     /** 
    15.      */  
    16.     public static void main(String[] args) {  
    17.         String str = "aabbccdderffisfalgjgosdrugnsdsduhgdsfugsdogosdjgosdig";  
    18.         Map<Character, Integer> map = countAll(str);  
    19.         System.out.println(map);  
    20.         // 迭代Map: 迭代所有的key,迭代所有的value迭代Entry<key,value>   
    21.         // 迭代所有的value,实现统计所有的字符总数   
    22.         Collection<Integer> values = map.values();  
    23.         Iterator<Integer> ite = values.iterator();  
    24.         int total = 0;  
    25.         while (ite.hasNext()) {  
    26.             Integer n = ite.next();  
    27.             total += n;  
    28.             System.out.print(n + " ");  
    29.         }  
    30.         System.out.println("字符总数:" + total);  
    31.         // 迭代所有的key,利用迭代所有字符实现输出统计表格   
    32.         Set<Character> keys = map.keySet();  
    33.         System.out.println("字符串中所有字符的个数:" + keys.size());  
    34.         ArrayList<Character> list = new ArrayList<Character>(keys);  
    35.         Collections.sort(list);// 自然排序   
    36.         for (Iterator<Character> i = list.iterator(); i.hasNext();) {  
    37.             Character ch = i.next();  
    38.             int n = map.get(ch);  
    39.             System.out.print(ch + ":");  
    40.             System.out.println(n + " " + ((float) n / total) * 100 + "%");  
    41.             // System.out.print(ch);   
    42.         }  
    43.         System.out.println("照字符出现的数量排序输出:");  
    44.         //迭代Entry<key:Value> 实现按照字符出现的数量排序输出   
    45.         Set<Entry<Character, Integer>> entries = map.entrySet();  
    46.         List<Entry<Character,Integer>> entList=new ArrayList<Entry<Character,Integer>>(entries);  
    47.         Collections.sort(entList, new ByValue());  
    48.         for (Iterator<Entry<Character, Integer>> i = entList.iterator(); i.hasNext();) {  
    49.                 Entry<Character,Integer> entry=i.next();  
    50.                 Character ch=entry.getKey();  
    51.                 Integer n=entry.getValue();  
    52.                 System.out.print(ch + ":");  
    53.                 System.out.println(n + " " + ((float) n / total) * 100 + "%");  
    54.         }  
    55.     }  
    56.   
    57.     private static Map<Character, Integer> countAll(String str) {  
    58.         int length = str.length();  
    59.         Map<Character, Integer> map = new HashMap<Character, Integer>();  
    60.         for (int i = 0; i < length; i++) {  
    61.             char ch = str.charAt(i);  
    62.             if (map.containsKey(ch)) {// 如果包含ch   
    63.                 int n = map.get(ch);  
    64.                 map.put(ch, n + 1);  
    65.             } else {// 如果不包含ch   
    66.                 map.put(ch, 1);  
    67.             }  
    68.         }  
    69.         return map;  
    70.     }  
    71. }  
    72. class ByValue implements Comparator<Entry<Character,Integer>>{  
    73.     public int compare(Entry<Character, Integer> o1,  
    74.             Entry<Character, Integer> o2) {  
    75.         // TODO Auto-generated method stub   
    76.         return -(o1.getValue()-o2.getValue());//从大到小排列   
    77.         //return (o1.getValue()-o2.getValue());//从小到大排列   
    78.     }  
    79. }  

    转自:http://www.cdtarena.com/javapx/201305/8568.html

  • 相关阅读:
    ScrollView反弹效果的实现
    Unity 3D本地公布WebPlayer版时&quot;Failed to download data file&quot;解决方式
    win7休眠的开启与关闭方法命令行操作和图文结合的鼠标操作
    使用Javascript D3创建属于你的涂鸦作品
    android获取自己定义控件位置坐标,屏幕尺寸,标题栏,状态栏高度
    [Python]Use Flask-Admin with PostgreSQL
    [LeetCode] Best Time to Buy and Sell Stock
    spring实战五之Bean的自动检测
    FireBug使用总结
    javascript的window.onload()方法和jQuery的$(document).ready()的对比
  • 原文地址:https://www.cnblogs.com/cdtarena/p/3071478.html
Copyright © 2011-2022 走看看