Map典型应用 按要求排序


题:
某个文本文件中存储如下格式信息:

张小兵 北京
胡进 河北
将达民 四川
高力 河北
卢刊令 北京
....

读入这个文件,要求输出内容为:
北京:
卢刊令
张小兵

河北:
高力
胡进

四川:
将达民

....

即,先按省份的拼音序,每个省份内再按姓名拼音序来排列。

源码MapSorting.java


import java.io.File;  
import java.io.InputStreamReader;  
import java.io.BufferedReader;  
import java.io.FileInputStream;  
import java.text.CollationKey;
import java.text.Collator;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeSet;

class K3 implements Comparator<Object> {

    @SuppressWarnings("unchecked")
    public int compare(Object a, Object b) {
        if (a instanceof Entry == false || 
                b instanceof Entry == false) {
            return 0;
        }
        Collator col = Collator.getInstance(java.util.Locale.CHINA);
        // Province
        Entry<String, String> m1 = (Entry<String, String>) a;
        Entry<String, String> m2 = (Entry<String, String>) b;
        CollationKey c1 = col.getCollationKey(m1.getValue());
        CollationKey c2 = col.getCollationKey(m2.getValue());
        int p1 = col.compare(c1.getSourceString(), c2.getSourceString());
        if (p1!=0) {
            return p1;
        }
        // Name
        CollationKey c3 = col.getCollationKey(m1.getKey());
        CollationKey c4 = col.getCollationKey(m2.getKey());
        int p2 = col.compare(c3.getSourceString(), c4.getSourceString());
        if (p2!=0) {
            return p2;
        }
        return 0;
    }
}
        
public class MapSorting {   
    public static void main(String args[]) {
        Map<String, String> map = new HashMap<String, String>();
        
        try {
            String encoding = "UTF-8";
            String path = "/Users/Tro/Desktop/Trial.txt";
            /*
             *张小兵 北京
             *胡进 河北
             *将达民 四川
             *高力 河北
             *卢刊令 北京
             */
            File file = new File(path);
            InputStreamReader reader = new InputStreamReader(
                    new FileInputStream(file), encoding);
            BufferedReader br = new BufferedReader(reader);
            String line = "";
            line = br.readLine();
            while (line!=null) {
                map.put(line.split(" ")[0], line.split(" ")[1]);
                line = br.readLine();
            }
            br.close();
        }
        catch (Exception e) {
            System.out.println(e);
        }
        
        Set<Entry<String, String>> t = new TreeSet<Entry<String, String>>(new K3());
        Iterator<Entry<String, String>> ite = map.entrySet().iterator();
        while(ite.hasNext()) {
            Entry<String, String> item = ite.next();
            t.add(item);
        }
        Iterator<Entry<String, String>> ite2 = t.iterator();
        String s1 = "";
        while(ite2.hasNext()) {
            Entry<String, String> item2 = ite2.next();
            if (!s1.equals(item2.getValue())) {
                s1 = (String) item2.getValue();
                System.out.println("\n"+s1+":");
                System.out.println(item2.getKey());
            }
            else {
                System.out.println(item2.getKey());
            }
        }       
    }
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注