zoukankan      html  css  js  c++  java
  • HashMap_经典存储_经典分拣思路

    Collection的概念
    Collection表示一组对象,它是集中,收集的意思,就是把一些数据收集起来
    Collection函数库是在java.util包下的一些接口和类,类是用来产生对象存放数据用的,而接口是访问数据的方式
    Collection函数库与数组的两点不同:
    1.数组的容量是有限制的,而Collection库没有这样的限制,它容量可以自动的调节
    2.Collection函数库只能用来存放对象,而数组没有这样的限制
    Collection接口是Collection层次结构中根接口,它定义了一些最基本的访问方法,让我们能用统一的方式通过它或它的子接口来访问数据
    区别:Collection代表一组对象,Collection函数库就是Java集合框架,Collection接口,是这个集合框架中的根接口
    存放在Collection库中的数据,被称为元素

                                   Collection
                                       |
                                   |      |
     set(无序、不可重复) List(有序、可重复) Map(key/Value键值对)
     |                                  |
     HashSet                          HashMap
    
     Collection接口:定义了存取一组对象的方法,其子接口Set和List分别定义了存储方式
     Set中的数据对象没有顺序且不可以重复
     List中的数据对象有顺序且可以重复
     Map接口定义了存储"键(key)-值(value)映射对"的方法
     接口中定义了一些规范,规范就是一些不变的东西,接口中定义了规范,实现类一定要实现
    

    1. HashMap_经典存储_经典分拣思路

    TestHashMap3.java

    package junit.matrix.map;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    
    /**
     * 简单的加入面向对象
     * 
     * TestHashMap2<BR>
     * 创建人:Matrix <BR>
     * 时间:2016年2月3日-下午8:23:00 <BR>
     * 
     * @version 1.0.0
     *
     */
    public class TestHashMap3 {
    
    	/**
    	 * 定义一个Student类,属性:name/姓名:no/班号,score/成绩
    	 * 
    	 * 现在将若干Student对象放入List,请统计每个班级的总分和平均分
    	 * 
    	 * 面向对象分拣存储
    	 * 
    	 * 不推荐使用Map<String,List<Student>>操作不方便 推荐使用Map<String,ClassRoom>
    	 * 
    	 * 思路:简单封装一个班级类即可
    	 * 
    	 * 方案:面向对象+分解存储
    	 * 
    	 */
    
    	public static void main(String[] args) {
    		// 1、考试
    		List<Student> stuList = exam();
    		// 2、分析成绩
    		Map<String, ClassRoom> map = count(stuList);
    		// 3、查看成绩(总分/平均分)
    		view(map);
    	}
    
    	/**
    	 * 第三步:总看每个班的总分和平均分
    	 */
    	public static void view(Map<String, ClassRoom> map) {
    		Set<String> keys = map.keySet();
    		// 获取迭代器对象
    		Iterator<String> keysIt = keys.iterator();
    		// 先判断
    		while (keysIt.hasNext()) {
    			// 再获取
    			String no = keysIt.next();
    			ClassRoom room = map.get(no);
    			// 查看总分 计算平均分
    			double total = room.getTotal();
    			double avg = total / room.getStuList().size();
    			// System.out.println(room.getStuList().size());
    			System.out.println("班级:" + no + "----------->总分:" + total + "------------------>平均分:" + avg);
    		}
    	}
    
    	/**
    	 * 第二步:对数据进行统计分析 1、面向对象 2、分拣存储
    	 */
    	public static Map<String, ClassRoom> count(List<Student> list) {
    		Map<String, ClassRoom> map = new HashMap<String, ClassRoom>();
    		// 1、遍历list
    		for (Student stu : list) {
    			// 2、分拣存储
    			// 查看是否存在该编号的班级,如果不存在则创建班级,如果存在则存放学生统计总分
    			// 班级编号
    			String no = stu.getNo();
    			// 分数
    			double score = stu.getScore();
    			// 班级如果不存在则创建班级
    			ClassRoom room = map.get(stu.getNo());
    			if (null == room) {
    				room = new ClassRoom(no);
    				map.put(no, room);
    			}
    			// 如果班级存在,放入学生
    			room.getStuList().add(stu);
    			// 计算总分
    			room.setTotal(room.getTotal() + score);
    		}
    		return map;
    	}
    
    	/**
    	 * 第一步:模拟考试 测试数据 到List中
    	 */
    	public static List<Student> exam() {
    		// 将学生成绩放置到list里面
    		List<Student> list = new ArrayList<Student>();
    		// 存放学生成绩
    		list.add(new Student("老裴", "A班", 80));
    		list.add(new Student("Matrix", "A班", 90));
    		list.add(new Student("dick", "A班", 98));
    		list.add(new Student("keke", "A班", 50));
    		list.add(new Student("mini", "A班", 78));
    		list.add(new Student("Array", "B班", 65));
    		list.add(new Student("Ada", "B班", 33));
    		list.add(new Student("vickey", "B班", 88));
    		list.add(new Student("Tom", "B班", 93));
    		// System.out.println(list.size());
    		return list;
    	}
    
    }
    


  • 相关阅读:
    oracle 数据库服务名怎么查
    vmware vsphere 6.5
    vSphere虚拟化之ESXi的安装及部署
    ArcMap中无法添加ArcGIS Online底图的诊断方法
    ArcGIS中字段计算器(高级计算VBScript、Python)
    Bad habits : Putting NOLOCK everywhere
    Understanding the Impact of NOLOCK and WITH NOLOCK Table Hints in SQL Server
    with(nolock) or (nolock)
    What is “with (nolock)” in SQL Server?
    Changing SQL Server Collation After Installation
  • 原文地址:https://www.cnblogs.com/SparseMatrix/p/5210835.html
Copyright © 2011-2022 走看看