有如下一组数据: John Doe            3434.34
Tom Smith           123.22
Jane Baker          1378.00
        Todd Hall            99.22
        Ralph Smith         19.081 . 按名排序 , 降序输出。
2.按姓排序 ,升序输出。
输出形式为 : XXXX:XXX
 请不要说什么我给思路之类的废话   我要程序   用MAP 和SET 和LIST  做   谢谢 大家

解决方案 »

  1.   

    请参考:
    http://community.csdn.net/Expert/topic/5006/5006120.xml?temp=.2733728我也说句废话,要代码的提问方式,只能让你成为一名砌墙的泥水工,而不是一名建筑师。
      

  2.   

    import java.util.*;public class SortMap// interface SortedMap
    {
    public static void main(String[] args) 
    {
            TreeMap map=new TreeMap();
            map.put("John Doe","3434.34");
            map.put("Tom Smith","123.22");
            map.put("Jane Baker","1378.00");
            map.put("Todd Hall","99.22");
            map.put("Ralph Smith","19.08");
    //        map.subMap("John Doe","Todd Hall");  
    //        map.comparator();
            Object low=map.firstKey();
            Object high=map.lastKey();
            map.subMap(low,high);
            String str= map.toString();      
            String s=str.replace(", ","\n").replace("{","").replace("}","");
            System.out.println(s);
            
    }}
      

  3.   

    我这个程序是按"名"降序,"名"相同再按"姓"升序,不晓得是不是这个意思.
    import java.util.TreeSet;class Student implements Comparable<Student>{
    String name=null;
    String xing=null;
    float sz;
    public Student(String name,String xing,float sz) {

    this.name=name;
    this.xing=xing;
    this.sz=sz;
    }
    public int compareTo(Student o) {
    int p=this.name.compareTo(o.name);
    if(p>0)
    return -1;
    else if(p<0)
    return 1;
    else if(p==0){
    int k=this.xing.compareTo(o.xing);
    if(k>0)
    return 1;
    else if(k<0)
    return -1;
    else if(k==0)
    return 0;
    }
    return -1;
    }public String toString() {
    return "名:"+name+"\t姓:"+xing+"\t数字:"+sz;
    }
    }public class PP {
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    Student s1=new Student("John","Doe",3434.34f);
    Student s2=new Student("Tom","Smith",123.22f);
    Student s3=new Student("Jabe","Baker",1378.00f);
    Student s4=new Student("Todd","Hall",99.22f);
    Student s5=new Student("Ralph","Smith",19.08f);
    TreeSet<Student> set=new TreeSet<Student>();
    set.add(s1);
    set.add(s2);
    set.add(s3);
    set.add(s4);
    set.add(s5);
    for(Student st:set){
    System.out.println(st);
    }
    }}
      

  4.   

    import java.util.Comparator;
    import java.util.Scanner;
    import java.util.TreeSet;class Student implements Comparable<Student>{
    String name=null;
    String xing=null;
    float sz;
    public Student(String name,String xing,float sz) {

    this.name=name;
    this.xing=xing;
    this.sz=sz;
    }
    public int compareTo(Student o) {// 1 . 按"名"降序,名相同按"姓"升序 
    int p=this.name.compareTo(o.name);
    if(p>0)
    return -1;
    else if(p<0)
    return 1;
    else if(p==0){
    int k=this.xing.compareTo(o.xing);
    if(k>0)
    return 1;
    else if(k<0)
    return -1;
    else if(k==0)
    return 0;
    }
    return -1;
    }public String toString() {
    return "名:"+name+"\t姓:"+xing+"\t数字:"+sz;
    }
    }public class PP { /**
     * John Doe            3434.34
           Tom Smith           123.22
           Jane Baker          1378.00
           Todd Hall            99.22
           Ralph Smith         19.08     1 . 按名排序 , 降序输出。
         2.按姓排序 ,升序输出。
         输出形式为 : XXXX:XXX
     */
    static Comparator<Student> c1=new Comparator<Student>(){//1 . 按名排序 
    public int compare(Student o1, Student o2) {
    int pk=o1.name.compareTo(o2.name);
    if(pk>0)
    return -1;
    else if(pk<0)
    return 1;
    else
    return 0;
    }
    };
    static Comparator<Student> c2=new Comparator<Student>(){//2.按姓排序
    public int compare(Student o1, Student o2) {
    int pk=o1.xing.compareTo(o2.xing);
    if(pk>0)
    return 1;
    else if(pk<0)
    return -1;
    else
    return 0;
    }
    };

    static TreeSet tianjia(TreeSet<Student> set){
    Student s1=new Student("John","Doe",3434.34f);
    Student s2=new Student("Tom","Smith",123.22f);
    Student s3=new Student("Jabe","Baker",1378.00f);
    Student s4=new Student("Todd","Hall",99.22f);
    Student s5=new Student("Ralph","Smith",19.08f);
    set.add(s1);
    set.add(s2);
    set.add(s3);
    set.add(s4);
    set.add(s5);
    return set;

    }
    static void printlnSet(TreeSet<Student> set){
    for(Student st:set){
    System.out.println(st);
    }

    }
    public static void main(String[] args) {
    // TODO Auto-generated method stub

    TreeSet<Student> set=null;
    Scanner sc=new Scanner(System.in);
    System.out.println("请选择排序方式:(1) 按'名'降序,名相同按'姓'升序(2) 按名排序 (3)按姓排序");
    int sele=Integer.valueOf(sc.nextLine());
    if(sele==1){
    set=new TreeSet<Student>();
    set=tianjia(set);
    printlnSet(set);
    }
    else if(sele==2){
    set=new TreeSet<Student>(c1);
    set=tianjia(set);
    printlnSet(set);
    }
    else if(sele==3){
    set=new TreeSet<Student>(c2);
    set=tianjia(set);
    printlnSet(set);
    }
    else{
    System.out.println("选择不正确....");
    }
    }
    }