• 三、分拣存储分组归类


    一、将单词统计出来
    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

     

  • 相关阅读:
    初始化mysql数据库 /usr/bin/mysql_install_db执行时报错
    CentOS7安装mysql兼容性问题
    CentOS7网络连接问题以及重启网络服务失败
    CentOS7安装nginx
    zookeeper启动时报错:Error contacting service. It is probably not running问题
    CentOS查看卸载openjdk
    使用yum命令时提示:Another app is currently holding the yum lock
    修改eclipse中文件打开默认方式
    [程序员代码面试指南]链表问题-单链表的选择排序(选择排序)
    [程序员代码面试指南]链表问题-删除无序链表中重复出现的节点
  • 原文地址:https://www.cnblogs.com/dangjingwei/p/16006357.html
Copyright © 2020-2023  润新知