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>();
//搞一個(gè)集合,裝學(xué)生對(duì)象
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);
}
}請(qǐng)問(wèn)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代表的是誰(shuí)?this.count在與誰(shuí)(bean.count)比較?this.count?又代表誰(shuí)呢?
所返回的正數(shù)負(fù)數(shù)是什么意思?有什么用?求大神指點(diǎn),有點(diǎn)暈。
求大神幫忙看看。
小小雨點(diǎn)
2016-10-19 21:22:58