一个集合,每个元素均为对象类,形式如下:
id   name   course
---------------------
101  peter  java
101  peter  net
102  scott  java
102  scott  net
102  scott  xml
103  smith  oracle
103  smith  sybase
103  smith  db2要求显示结果如下:
101 peter java net
102 scott java net xml
103 smith oracle sybase db2怎么做.是不是用TreeSet啊

解决方案 »

  1.   

    package com.rcom10002;import java.util.ArrayList;
    import java.util.Collection;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.TreeMap;/**
     * @author Administrator
     */
    public class Main {    static final String[][] infos = { { "101", "peter", "java" }, { "101", "peter", "net" },
                { "102", "scott", "java" }, { "102", "scott", "net" }, { "102", "scott", "xml" },
                { "103", "smith", "oracle" }, { "103", "smith", "sybase" }, { "103", "smith", "db2" } };
        static class Info {
            String id = null;
            String name = null;
            String info = null;
            public Info(String id, String name, String info) {
                super();
                this.id = id;
                this.name = name;
                this.info = info;
            }
        }
        static Collection c = new ArrayList();
        static {
            for (int i = 0; i < infos.length; i++) {
                c.add(new Info(infos[i][0], infos[i][1], infos[i][2]));
            }
        }    public static void main(String[] args) {
            Iterator itr = c.iterator();
            Map map = new TreeMap();
            while (itr.hasNext()){
                Info info = (Info)itr.next();
                String keyInMap = info.id + " " + info.name;
                if (map.containsKey(keyInMap)){
                    map.put(keyInMap, map.get(keyInMap) + " " +  info.info);
                    continue;
                }
                map.put(keyInMap, keyInMap + " " + info.info);
            }
            itr = map.values().iterator();
            while (itr.hasNext()){
                System.out.println(itr.next().toString());
            }
        }
    }
    /*
    101 peter java net
    102 scott java net xml
    103 smith oracle sybase db2
    */
      

  2.   

    <del>工具类Collections.sort()</del>开始看错了 :(