package?text2;
import?java.io.File;
import?java.io.IOException;
import?java.util.ArrayList;
import?java.util.HashMap;
import?java.util.List;
import?java.util.Map;
import?java.util.Scanner;
import?java.util.Set;
import?java.util.TreeSet;
class??NameBean?implements?Comparable<NameBean>{
private?String?name;
private?Integer?count;
public?int?compareTo(NameBean?bean)?{
if(this.count?>?bean.count){
return?1;
}else?if(this.count?<?bean.count){
return?-1;
}else{
return?this.name.compareTo(bean.name);
}
}
public?NameBean(String?name,?Integer?count)?{
super();
this.name?=?name;
this.count?=?count;
}
public?String?toString()?{
return?name?+"出現(xiàn),?"?+?count;
}
}
public?class?Demo2?{
public?static?void?main(String[]?args)?throws?IOException?{
List<String>?names?=?new?ArrayList<String>();
Map<String,?Integer>?nameMap?=?new?HashMap<String,?Integer>();
//搞一個集合,裝學生對象
Set<NameBean>?set?=?new?TreeSet<NameBean>();
Scanner?sc?=?new?Scanner(new?File("out/student.txt"));
while?(sc.hasNextLine())?{
String?line?=?sc.nextLine();
String[]?arr?=?line.split(",");
String?name?=?arr[1];
names.add(name);
if?(nameMap.containsKey(name))?{
Integer?val?=?nameMap.get(name);
nameMap.put(name,?val?+?1);
}?else?{
nameMap.put(name,?1);
}
}
System.out.println(names);
System.out.println(nameMap);
//================
for?(Map.Entry<String,?Integer>?entry?:?nameMap.entrySet())?{
//if?(entry.getValue()?>?1)?{
//System.out.println(entry.getKey()?+?"-->"+?(entry.getValue()?));
//}
set.add(new?NameBean(entry.getKey(),entry.getValue()));
}
System.out.println(set);
}
}請問public?int?compareTo(NameBean?bean)?{
if(this.count?>?bean.count){
return?1;
}else?if(this.count?<?bean.count){
return?-1;
}else{
return?this.name.compareTo(bean.name);
}
}里面的bean.count?與bean.name代表的是誰?this.count在與誰(bean.count)比較?this.count?又代表誰呢?
所返回的正數(shù)負數(shù)是什么意思?有什么用?求大神指點,有點暈。
添加回答
舉報
0/150
提交
取消