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 +"出现, " + 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 又代表谁呢?
所返回的正数负数是什么意思?有什么用?求大神指点,有点晕。
添加回答
举报
0/150
提交
取消