zoukankan      html  css  js  c++  java
  • Java--HashMap排序

     1 package connection;
     2 
     3 import java.util.Collections;
     4 import java.util.Comparator;
     5 import java.util.HashMap;
     6 import java.util.LinkedHashMap;
     7 import java.util.LinkedList;
     8 import java.util.List;
     9 import java.util.Map;
    10 import java.util.Map.Entry;
    11 
    12 
    13 public class Test {
    14     
    15     public static void main(String[] args) {
    16         Map<String, String> userMap = new HashMap<String, String>();
    17         userMap.put("cn-1410165inv", "E141@0165#inv");
    18         userMap.put("cn-3330147inv", "E333@0147#inv");
    19         userMap.put("cn-1410165invkio", "J02amN$8");
    20         userMap.put("cn-1410165invrob", "U1SzpU+m");
    21         userMap.put("cn-3330147invkio", "J6]bJK,c");
    22         userMap.put("cn-3330147invrob", "W0zr%Y,q");
    23         
    24         for(Map.Entry<String, String> s : userMap.entrySet()) {
    25             System.out.println(s.getKey() + "    " + s.getValue());
    26         }
    27         
    28         
    29         List<Map.Entry<String, String>> sortList = new LinkedList<Map.Entry<String,String>>(userMap.entrySet());
    30         Collections.sort(sortList, new Comparator<Map.Entry<String, String>>() {
    31 
    32             @Override
    33             public int compare(Entry<String, String> o1, Entry<String, String> o2) {
    34                 String suffix1 = o1.getKey().split("-\d+")[1];
    35                 String suffix2 = o2.getKey().split("-\d+")[1];
    36                 int len1 = suffix1.length();
    37                 int len2  = suffix2.length();
    38                 if(len1 != len2) {
    39                     return len1-len2;
    40                 }
    41                 int min = len1>=len2?len2:len1;
    42                 for(int i=0; i<min; i++) {
    43                     String c1 = suffix1.substring(i, i+1);
    44                     String c2 = suffix2.substring(i, i+1);
    45                     if(c1.toCharArray()[0] - c2.toCharArray()[0] == 0) {
    46                         continue;
    47                     } else {
    48                         return c1.toCharArray()[0] - c2.toCharArray()[0];
    49                     }
    50                 }
    51                 //continue
    52                 len1 = o1.getKey().length();
    53                 len2  = o2.getKey().length();
    54                 if(len1 != len2) {
    55                     return len1-len2;
    56                 }
    57                 min = len1>=len2?len2:len1;
    58                 for(int i=0; i<min; i++) {
    59                     String c1 = o1.getKey().substring(i, i+1);
    60                     String c2 = o2.getKey().substring(i, i+1);
    61                     if(c1.toCharArray()[0] - c2.toCharArray()[0] == 0) {
    62                         continue;
    63                     } else {
    64                         return c1.toCharArray()[0] - c2.toCharArray()[0];
    65                     }
    66                 }
    67                 return 0;
    68             }
    69         });
    70         
    71         System.out.println("--------------------------------------");
    72         Map<String, String> sortMap = new LinkedHashMap<String, String>(); 
    73         for(Entry<String, String> entry: sortList) { 
    74             sortMap.put(entry.getKey(), entry.getValue()); 
    75         } 
    76         for(Map.Entry<String, String> s : sortMap.entrySet()) {
    77             System.out.println(s.getKey() + "    " + s.getValue());
    78         }
    79         
    80     }
    81 
    82 }

    颜色区域是关键代码。

  • 相关阅读:
    使用JDBC连接MySql时出现:The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration
    Mysql Lost connection to MySQL server at ‘reading initial communication packet', system error: 0
    mysql-基本命令
    C# 监听值的变化
    DataGrid样式
    C# 获取当前日期时间
    C# 中生成随机数
    递归和迭代
    PHP 时间转几分几秒
    PHP 根据整数ID,生成唯一字符串
  • 原文地址:https://www.cnblogs.com/microcat/p/6343518.html
Copyright © 2011-2022 走看看