WordCount升级版(?)

前两天留的题(),给了两个如图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);
		}
		
		
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值