前两天留的题(),给了两个如图txt文件,大概就是把频率最高的导演,主持人,演员统计出来,再算一下各类节目占比
用到了读写文件,HashMap,排序之类的……以下代码(。)
因为需要排序所以通过实现Comparable接口新建一个类
public class Word implements Comparable<Word>{
private String text;//名字
private int times;//次数
public Word (String text,int times) {
this.text = text;
this.times = times;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public int getTimes() {
return times;
}
public void setTimes(int times) {
this.times = times;
}
public int compareTo(Word a) {
return (-1) * (this.getTimes() - a.getTimes());//因为是大小降序排序,所以逆序输出
}
}
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
public class WordCount {
public static void main(String[] args) {
String line;
HashMap <String, Word> map = new HashMap<>();
HashMap <String,Word> map2 = new HashMap<>();
try(BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("C:/Users/hasee/Desktop/作业/a.txt")))){
boolean flag = false;
while((line = in.readLine()) != null) {
if(flag == false) {
flag = true;//第一行不要
}else {
String[] words = line.split(" ");
String tmp = words[1];
if(map.containsKey(tmp)) {
Word w = map.get(tmp);
w.setTimes(w.getTimes()+1);
map.put(tmp, w);
}else {
map.put(tmp, new Word(tmp,1));
}
String[] host = words[2].split("、");
for(String t : host) {
if(map2.containsKey(t)) {
Word k = map2.get(t);
k.setTimes(k.getTimes()+1);
map2.put(t, k);
}else {
map2.put(t, new Word(t,1));
}
}
}
}
}catch(Exception ex) {
ex.printStackTrace();
}
ArrayList<Word> words = new ArrayList<Word>(map.values());
Collections.sort(words);
System.out.println("主持春晚最多的导演:");
for(int i = 0; i < 5; i++) {
Word k = words.get(i);
System.out.println("姓名:" + k.getText() + " 次数:" + k.getTimes());
}
words = new ArrayList<>(map2.values());
Collections.sort(words);
System.out.println("主持春晚最多的主持人:");
for(int i = 0; i < 10; i++) {
Word k = words.get(i);
System.out.println("姓名:" + k.getText() + " 次数:" + k.getTimes());
}
}
}
public class WordCountB {
public static void main(String[] args) {
String line;
HashMap<String, Integer> map = new HashMap<>();
HashMap<String, Word> map2 = new HashMap<>();
map.put("歌曲", 0);
map.put("舞蹈", 0);
map.put("相声", 0);
map.put("小品", 0);
map.put("其他", 0);
try(BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("C:/Users/hasee/Desktop/作业/b.txt")))){
while((line = in.readLine()) != null) {
String a[] = line.split(" ");
String t = a[1];
if(map.containsKey(t)) {
map.put(t, map.get(t)+1);
}else {
map.put("其他", map.get("其他")+1);
}
String tmp = a[3];
String h[] = a[3].split("、");
for(String i : h) {
if(map2.containsKey(i)) {
Word w = map2.get(i);
w.setTimes(w.getTimes()+1);
map2.put(i, w);
}else {
map2.put(i, new Word(i,1));
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
ArrayList<Word> al = new ArrayList<>(map2.values());
Collections.sort(al);
System.out.println("参加春晚次数最多的演员名字及参演次数:");
for(int i = 0; i < 10; i++) {
Word tmp = al.get(i);
System.out.println("姓名:" + tmp.getText() + "参演次数:" + tmp.getTimes());
}
Integer sum = map.get("舞蹈") + map.get("歌曲") + map.get("相声") + map.get("小品") + map.get("其他");
System.out.println(sum);
Iterator<Entry<String,Integer>> iter = map.entrySet().iterator();
while(iter.hasNext()) {
Map.Entry<String, Integer> entry = iter.next();
String key = entry.getKey();
Integer value = entry.getValue();
double g = (double)value/(double)sum;
System.out.println(key + "的比例是:" + g);
}
}
}