1. 人员名单重建问题
有文件“人员名单.txt”,其记录可能如下(可以参看考生文件夹下该文件的内容):
李爱华,北京
张立,吉林
吴祖含,上海
张颖,河北
李文虎,北京
许林,湖南
赵平复,河北
唐笑,北京
刘小明,河北
董其云,北京
对程序的要求是:读入该文件,变换后,输出文件“人员名单2.txt”。
要求在新的文件中,以省份(或直辖市)为依据分类。先输出省份,再输出该省份的人员数量,接下来是该省份人员列表。不同省份记录间用空行分隔。
省份间的先后次序可以不考虑。
同一省份的人员必须要按姓名的拼音序进行排列。
如上的“人员名单.txt”文件,重新整理后的输出文件“人员名单2.txt”内容应该为:
北京
4
董其云我的代码如下:import java.io.*;
import java.util.*;
class Student implements Comparable<Student>
{
private String name;
private String adress;
static TreeMap<Student ,Integer> map=new TreeMap<Student ,Integer>();
Student()
{

}
public String getAdress() {
return adress;
}
public void setAdress(String adress) {
this.adress = adress;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

Student(String name,String adress)
{
this.name=name;
this.adress=adress;
}
public void myPut(Student s,int n)
{
map.put(s, n);


}
public void myGet()
{
Set <Student>keySet=map.keySet();
Iterator<Student> it=keySet.iterator();
Student st=it.next();
System.out.println(st.adress);
System.out.println(st.name);
while(it.hasNext())
{
Student s=it.next();
if(s.adress.equals(st.adress))
{
System.out.println(s.name);
}
else
{
st=s;
System.out.println();
System.out.println(s.adress);
System.out.println(s.name);
}

}
}
public boolean equals(Object obj)
{
if(!(obj instanceof Student))throw new RuntimeException("运行异常");
Student s=(Student) obj;
int n=this.getName().compareTo(s.getName());
if(n==0)return this.adress.equals(s.adress);
return this.name.equals(s.name);
}
public int hashCode()
{
return this.name.hashCode()+adress.hashCode();
}
public String toString()
{
return this.name+"   "+this.adress;
}
public int compareTo(Student arg0) {
int n=this.adress.compareTo(arg0.adress);
if(n==0) 
{
return this.name.compareTo(arg0.name);
}
return  n;
}
}
class ReadTxt
{
private Student stu=new Student();
public void read()
{
FileReader fr=null;
BufferedReader br=null;

try
{
fr=new FileReader("E:\\大赛题\\java-辅导资料\\Java其它参考资料\\编程大题\\人员名单重建问题\\人员名单.txt");
br=new BufferedReader(fr);
String s=null;
String str[]=new String[2];
int i=0;
while((s=br.readLine())!=null)
{
str=s.split(",");
stu=new Student(str[0],str[1]);
stu.myPut(stu,i);
i++;

}
stu.myGet();
   
   

}
catch(Exception e)
{
e.getStackTrace();
}
finally
{
try
{
br.close();
fr.close();
}catch(Exception e)
{

}
}
}
}
public class Demo {
public static void main(String[] args) {
ReadTxt rt=new ReadTxt();
rt.read(); }}

运行结果如下:
上海
吴祖含北京
唐笑
李文虎
李爱华
董其云吉林
张立河北
刘小明
张颖
赵平复湖南
许林请问:为什么没有实现按地址及姓名排序,实现后  怎么统计该省份的人员数量

解决方案 »

  1.   


    楼主 TreeMap 不应该是 TreeMap<String,List<Student>>比如<"北京",{"董其云","李爱华","李文虎","唐笑"}> 吗?
      

  2.   

    那你说我实现comparable了为什么没有排序啊
      

  3.   


    ++,这样你统计人员数量就方便了。比如统计北京的人员,只要map.get("北京").size()就可以了。
      

  4.   

    那我只能表示你的写的compareTo方法不对,如果是英文应该没问题,是中文呢?你就应该去找中文字典序应该怎么实现。谢谢!
      

  5.   


    liaihua beijing
    zhangling jiling
    wuzhuhan shanghai
    zhangying hebei
    liwenhu beijing
    xuling hunan
    zhaopingfu hebie
    tangxiao beijing
    liuxiangming hebei
    dongqiyun beijing
    {beijing=[dongqiyun, liaihua, liwenhu, tangxiao], hebei=[liuxiangming, zhangying], hebie=[zhaopingfu], hunan=[xuling], jiling=[zhangling], shanghai=[wuzhuhan]}
      

  6.   

    {beijing=[dongqiyun, liaihua, liwenhu, tangxiao], hebei=[liuxiangming, zhangying], hebie=[zhaopingfu], hunan=[xuling], jiling=[zhangling], shanghai=[wuzhuhan]}
    这个怎么实现呢?
      

  7.   


    import java.io.*;
    import java.util.*;
    //为了代码简洁,我做了些改动。具体中文编码问题,你可以去google解决。
    class Student{
    String name = null;
    String adress = null;
    Student(){
    }
    Student(String name, String adress) {
    this.name = name;
    this.adress = adress;
    }
    }class DoMap{
    static TreeMap<String, TreeSet<String>> map = new TreeMap<String, TreeSet<String>>();
    public static void myGet(){
    System.out.println(map);
    }
    public static void myPut(Student s){
    if(map.containsKey(s.adress)){
    map.get(s.adress).add(s.name);
    return;
    }
    else{
    TreeSet<String> set= new TreeSet<String>();
    set.add(s.name);
    map.put(s.adress,set);
    return;
    }
    }
    }
    class ReadTxt {
    Student stu = new Student();
    public void read() {
    FileReader fr = null;
    BufferedReader br = null;
    try {
    fr = new FileReader("test.txt");
    br = new BufferedReader(fr);
    String s = null;
    String str[] = new String[2];

    int i = 0;
    while ((s = br.readLine())!=null) {
    str = s.split(",");
    System.out.println(str[0]+" "+str[1]);
    stu = new Student(str[0],str[1]);
    DoMap.myPut(stu);
    i++;
    }
    DoMap.myGet();
    } catch (Exception e) {
    e.getStackTrace();
    } finally {
    try {
    br.close();
    fr.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
    }
    public class Demo {
    public static void main(String[] args) {
    ReadTxt rt = new ReadTxt();
    rt.read();
    }
    }
      

  8.   


    import java.io.*;
    import java.text.CollationKey;
    import java.text.Collator;
    import java.util.*;class Student{
    String name = null;
    String adress = null;
    Student(){
    }
    Student(String name, String adress) {
    this.name = name;
    this.adress = adress;
    }
    }
    class DoMap{
    static CollatorComparator comparator = new CollatorComparator();
    static TreeMap<String, TreeSet<String>> map = new TreeMap<String, TreeSet<String>>(comparator);
    public static void myGet(){

    for(Iterator it=map.keySet().iterator();it.hasNext();){
    String s =(String)it.next();
    System.out.println(s);
    System.out.println(map.get(s).size());
    for(Iterator its = map.get(s).iterator();its.hasNext();){
    System.out.println(its.next());
    }
    System.out.println();
    }

    }
    public static void myPut(Student s){
    if(map.containsKey(s.adress)){
    map.get(s.adress).add(s.name);
    return;
    }
    else{
    TreeSet<String> set= new TreeSet<String>(comparator);
    set.add(s.name);
    map.put(s.adress,set);
    return;
    }
    }
    }
    class ReadTxt {
    Student stu = new Student();
    public void read() {
    FileReader fr = null;
    BufferedReader br = null;
    try {
    fr = new FileReader("test.txt");
    br = new BufferedReader(fr);
    String s = null;
    String str[] = new String[2];
    int i = 0;
    while ((s = br.readLine())!=null) {
    str = s.split(",");
    stu = new Student(str[0],str[1]);
    DoMap.myPut(stu);
    i++;
    }
    DoMap.myGet();
    } catch (Exception e) {
    e.getStackTrace();
    } finally {
    try {
    br.close();
    fr.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
    }
    class CollatorComparator implements Comparator{
    Collator collator = Collator.getInstance();
    public int compare(Object element1, Object element2){
        CollationKey key1 = collator.getCollationKey(element1.toString());
        CollationKey key2 = collator.getCollationKey(element2.toString());
        return key1.compareTo(key2);
    }
    }
    public class Demo {
    public static void main(String[] args) {
    ReadTxt rt = new ReadTxt();
    rt.read();
    }
    }
    运行结果//
    北京
    4
    董其云
    李爱华
    李文虎
    唐笑河北
    3
    刘小明
    张颖
    赵平复湖南
    1
    许林吉林
    1
    张立上海
    1
    吴祖含
      

  9.   

    谢啦 可是我这个明明你的差不多怎么还是不行呢?
    import java.io.*;
    import java.text.CollationKey;
    import java.text.Collator;
    import java.util.*;
    class People
    {
    String name;
    String address;
    People()
    {

    }
    People(String name,String address)
    {
    this.name=name;
    this.address=address;
    }
    }
    class DoMap2
    {   
    static TreeMap<String,TreeSet<String>> map=new TreeMap<String,TreeSet<String>>(new MyCompar());
    public static void myGet()
    {
    Set <String>keySet=map.keySet();
    Iterator <String>it=keySet.iterator();
    while(it.hasNext())
    {
    String adress=it.next();
    System.out.println(adress);
    TreeSet<String> name=map.get(adress);
    System.out.println(name.size());
    Iterator<String> itName=name.iterator();
    while(itName.hasNext())
    {
    System.out.println(itName.next());
    }
    System.out.println();

    }
    }
    public static void add(People p)
    {
    if(map.get(p.address)==null)
    {
    TreeSet<String> set=new TreeSet<String>();
    set.add(p.name);
    map.put(p.address, set);
    }
    else
    {
    map.get(p.address).add(p.name);
    }
    }
    }
    class Input
    {
    public static void read()
    {
    FileReader fr=null;
    BufferedReader br=null;
    try
    {
    fr=new FileReader("E:\\大赛题\\java-辅导资料\\Java其它参考资料\\编程大题\\人员名单重建问题\\人员名单.txt");
    br=new BufferedReader(fr);
    String s=null;
    String str[]=new String[2];
    while((s=br.readLine())!=null)
    {
    str=s.split(",");
    People p=new People(str[0],str[1]);
    DoMap2.add(p);
    }
    DoMap2.myGet();
    }
    catch(Exception e)
    {
    e.getStackTrace();
    }
    }

    }
    public class Copy {
    public static void main(String[] args) {
    Input.read();

    }}
    class MyCompar implements Comparator<String>
    {
        Collator c=Collator.getInstance();
    public int compare(String arg0, String arg1) {
    CollationKey key1=c.getCollationKey(arg0.toString());
    CollationKey key2=c.getCollationKey(arg1.toString());
         return key1.compareTo(key2);
    }


    }
      

  10.   

    TreeSet<String> set=new TreeSet<String>();
    TreeSet<String> set= new TreeSet<String>(comparator);你没仔细看啊。