• 三、分拣存储分组归类


    一、将单词统计出来
    package mypro01; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; //分拣存储--》分类归类 public class MapDemo01 { public static void main(String[] args) { //分割字符串 String[] arr="this is a cat and that is a nice is dang jing is ".split(" "); //分拣存储 Map<String,Integer> map=new HashMap<String,Integer>(); for(String key:arr) { if(!map.containsKey(key)) { map.put(key, 1); }else { map.put(key, map.get(key)+1); } } //查看每个单词出现的次数 Set<String> ketSet = map.keySet(); Iterator<String> it = ketSet.iterator(); while(it.hasNext()) { String key = it.next(); Integer value=map.get(key); System.out.println("key:"+key+"----->value:"+value); } } }

    二、面向对象存储

    package mypro01;
    
    public class Student {
        private     String name;
        private  String id;
        private  double score;
        
        public Student(String name, String id, double score) {
            super();
            this.name = name;
            this.id = id;
            this.score = score;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getId() {
            return id;
        }
        public void setId(String id) {
            this.id = id;
        }
        public double getScore() {
            return score;
        }
        public void setScore(double score) {
            this.score = score;
        }
        
        
    }
    package mypro01;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class ClassRoom {
        private String no;
        private List<Student> stuList;
        private double total;
        public ClassRoom(String no, List<Student> stuList, double total) {
            super();
            this.no = no;
            this.stuList = stuList;
            this.total = total;
        }
        public String getNo() {
            return no;
        }
        public void setNo(String no) {
            this.no = no;
        }
        public List<Student> getStuList() {
            return stuList;
        }
        public void setStuList(List<Student> stuList) {
            this.stuList = stuList;
        }
        public double getTotal() {
            return total;
        }
        public void setTotal(double total) {
            this.total = total;
        }
        public ClassRoom() {
            stuList=new ArrayList<Student>();
            
        }
        public ClassRoom(String no) {
            this();
            this.no=no;
        }
    }
    package mypro01;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    
    public class MapDemo02 {
        public static void main(String[] args) {
            //靠手
            List<Student> stulist=exam();
            Map<String,ClassRoom> map=count(stulist);
            view(map);
        }
        //计算分数
        public static void view(Map<String,ClassRoom> map) {
            Set<String> keyset=map.keySet();
            Iterator<String> itkey=keyset.iterator();
            while(itkey.hasNext()) {
                String no=itkey.next();
                ClassRoom room=map.get(no);
                double total=room.getTotal();
                double avg=total/room.getStuList().size();
                System.out.println(no+"--->"+total+"--->"+avg);
            }
        }
        
        //分类
        public static Map<String,ClassRoom> count(List<Student> list){
            Map<String,ClassRoom> map=new HashMap<String,ClassRoom>();
            for(Student stu:list) {
                String no=stu.getId();
                double score=stu.getScore();
                ClassRoom room=map.get(no);
                if(null==room) {
                    room =new ClassRoom(no);
                    map.put(no, room);
                }
                
                room.getStuList().add(stu);
                room.setTotal(room.getTotal()+score);
            }
            
            return map;
        }
        
        //模拟考试成绩到其中
        public static List<Student> exam(){
            List<Student> list=new ArrayList<Student>();
            
            list.add(new Student("张三","1",80));
            list.add(new Student("李四","2",40));
            list.add(new Student("王五","1",44));
            list.add(new Student("马六","2",22));
            list.add(new Student("马2","3",22));
            
            return list;
        }
    }
    结果:
    1--->124.0--->62.0 2--->62.0--->31.0 3--->22.0--->22.0

     

  • 相关阅读:
    一条insert语句批量插入多条记录
    分析器错误消息: 未能加载类型“WebApplication._Default”
    Avi视频生成缩略图时,提示“尝试读取或写入受保护的内存。这通常指示其他内存已损坏”
    DataGridView 的单元格的边框、 网格线样式的设定【转】
    2015届求职经历(转)
    现在有m组n个有序数组,例如{1,2,3,4},{2,3,4,6},{1,3,5,7},在这些数组中选择第k小的数据,然后返回这个值
    给定数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X
    ASP.Net中使用Report Service
    为自己尝试写点东西吧,程序员们!(转)
    ubuntu菜单面板丢了怎么找回
  • 原文地址:https://www.cnblogs.com/dangjingwei/p/16006357.html
Copyright © 2020-2023  润新知