Java集合的应用(单词和分数的统计)

1.统计每个单词出现的次数 :

思路:第一次创建容器,并且放入值,第二次使用容器存放对应的值即可。

实体类

public class Letter {
    private String name; //单词的名字
    private int count;  //单词出现的次数
    public Letter(){

    }
    public Letter(String name,int count){
        this.name=name;
        this.count=count;

    }
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}

实现方法

   /**
     * 处理思路:第一次创建容器,并且放入值
     * 第二次使用容器存放对应的值即可
     */
    public static void method(Map<String, Letter> map, String str) {
        String[] strings = str.split(" ");
        for (int i = 0; i < strings.length; i++) {
            if (!map.containsKey(strings[i])) {
                map.put(strings[i], new Letter(strings[i], 1)); //第一次创建容器,并且放入值
            } else {
                //在容器中存放值
                Letter letter = map.get(strings[i]);
                letter.setCount(letter.getCount() + 1);
            }
        }
    }  

测试方法 

public static void main(String[] args) {
        Map<String, Letter> map = new HashMap<String, Letter>();
        String str = "this is a cat and that is a mice and where is the food";
        method(map, str);
        Set<Map.Entry<String, Letter>> entries = map.entrySet();
        for (Map.Entry<String, Letter> entry : entries) {
            System.out.println("单词:" + entry.getKey() + ",出现" + entry.getValue().getCount() + "次数");
        }

    }

2.统计班级的总分数

实体类 :Student 

public class Student {
    private String name;
    private String stNo;
    private int score;

    public Student() {
    }

    public Student(String name, String stNo, int score) {
        this.name = name;
        this.stNo = stNo;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getStNo() {
        return stNo;
    }

    public void setStNo(String stNo) {
        this.stNo = stNo;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }
}

ClassRoom

public class ClassRoom {
    private String stNo;
    private List<Student> studentList;
    private int totalScore;

    public ClassRoom() {
    }
    public ClassRoom(String stNo,int totalScore){
        this.stNo=stNo;
        this.totalScore=totalScore;
    }
    public ClassRoom(String stNo, List<Student> studentList, int totalScore) {
        this.stNo = stNo;
        this.studentList = studentList;
        this.totalScore = totalScore;
    }

    public String getStNo() {
        return stNo;
    }

    public void setStNo(String stNo) {
        this.stNo = stNo;
    }

    public List<Student> getStudentList() {
        return studentList;
    }

    public void setStudentList(List<Student> studentList) {
        this.studentList = studentList;
    }

    public int getTotalScore() {
        return totalScore;
    }

    public void setTotalScore(int totalScore) {
        this.totalScore = totalScore;
    }
}  

实现的方法

/**
     * 统计班级的分数
     * 分析思路:第一次创建容器并且放入值,第二次直接向容器中存放值即可
     */
    public static void tjScore(Map<String,ClassRoom> map,List<Student> studentList){
        for (Student student:studentList){
            String stNo= student.getStNo();
            if (!map.containsKey(stNo)){
                map.put(stNo,new ClassRoom(stNo,student.getScore()));
            }else{
                ClassRoom classRoom = map.get(stNo);
                classRoom.setTotalScore(classRoom.getTotalScore()+student.getScore());
            }
        }
    }  

测试的方法

 public static void main(String[] args) {
        //存放学生
        List<Student> studentList=new ArrayList<Student>();
        studentList.add(new Student("aa","001",80));
        studentList.add(new Student("bb","001",80));
        studentList.add(new Student("cc","002",80));
        studentList.add(new Student("dd","002",80));
        studentList.add(new Student("ff","003",80));
        studentList.add(new Student("ee","003",80));
        Map<String,ClassRoom> map=new HashMap<String, ClassRoom>();
        tjScore(map,studentList);
        Set<Map.Entry<String, ClassRoom>> entries = map.entrySet();
        for (Map.Entry<String, ClassRoom> entry:entries){
            System.out.println("班级:"+entry.getKey()+",的总分:"+entry.getValue().getTotalScore());
        }
    }

 

每天进步一丢丢

完成。

 

posted @ 2020-04-23 15:33  阿福聊编程  阅读(208)  评论(0编辑  收藏  举报