集合类学习之HashMap经典储存 分拣存储与面向对象组合

 

HashMap:键值对(key-value)

通过对象来对对象进行索引,用来索引的对象叫做key,其对应的对象叫做value.

默认是1:1关系(一对一)

存在则覆盖,当key已经存在,则利用新的value覆盖原有的value

 

例1:给定一个字符串,求出字符串中每一个单词在字符串中出现的次数

旨意:map的分拣思想。

第一种分拣思想:(1)先为key创建对应的容器(2)使用容器,存放key对应的值

  1 /**
  2  * 作为包装类,用来存放英文单词,和该英文单词出现的次数
  3 * @author qjc
  4 * @date 2016-3-9
  5 */
  6 public class Str {
  7     private String st;
  8     private int count;
  9    //....
 10 }
 11 
 12 /**
 13  * 字符串:this is a cat and that is a nice and where is the food
 14  * 将该字符串的每个单词出现的次数统计出来
 15  * 【分拣的思想】
 16  *  第一种:为所有key创建容器
 17  *        之后存放对应的value
 18  *  第二种:第一次创建容器,并存放value
 19  *       第二次之后,直接使用容器存放value
 20 * @author qjc
 21 * @date 2016-3-9 
 22 *
 23  */
 24 public class TestMap {
 25     
 26     public static void main(String[] args) {
 27         test4();
 28         
 29     }
 30     
 31     //第一种分拣思路 (1)先为所有的key创建对应的容器(2)为对应key的容器中存放值
 32     public static void test1(){
 33         String sts="this is a cat and that is a nice and where is the food";
 34         //将字符串分割成一个个单词,并存放入数组中
 35         String[] strings=sts.split(" ");
 36         //创建一个map对象,用来存放单词和单词出现的次数
 37         Map<String, Str> countMap=new HashMap<String, Str>();
 38         //第一种分拣思想
 39         //第一步:为所有的key创建容器,
 40         for(int i=0;i<strings.length;i++){
 41             String temp=strings[i];
 42             //判断map是否含有此key,如果有返回true,否则返回false
 43             //第一次为所有的key创建容器
 44             if(!countMap.containsKey(temp)){
 45                 Str str=new Str();
 46                 countMap.put(temp, str);
 47             }
 48         }
 49         
 50         //第二步:使用容器,存放值
 51         for(String temp:strings){
 52             Str clsStr=countMap.get(temp);
 53             clsStr.setCount(clsStr.getCount()+1);
 54             clsStr.setSt(temp);
 55         }
 56         
 57         
 58         //测试countMap是否算是成功达到目的
 59         Set<String> keys=countMap.keySet();
 60         for (String key:keys) {
 61             Str sd=countMap.get(key);
 62             Integer cInteger=sd.getCount();
 63             System.out.println("字母:"+key+"--次数:"+cInteger);
 64         }
 65         
 66     }
 67     //第一种分拣思路 (1)先为所有的key创建对应的容器(2)为对应key的容器中存放值
 68     public static void test2(){
 69         String sts="this is a cat and that is a nice and where is the food";
 70         //将字符串分割成一个个单词,并存放入数组中
 71         String[] strings=sts.split(" ");
 72         //创建一个map对象,用来存放单词和单词出现的次数
 73         Map<String, Str> countMap=new HashMap<String, Str>();
 74         //第一种分拣思想
 75         //第一步:为key创建容器的同时,并存放值
 76         for(int i=0;i<strings.length;i++){
 77             String temp=strings[i];
 78             //判断map是否含有此key,如果有返回true,否则返回false
 79             //先创建容器,之后为容器存放值
 80             if(!countMap.containsKey(temp)){
 81                 Str str=new Str();
 82                 countMap.put(temp, str);
 83             }
 84                 //使用容器存放值
 85                 Str str=countMap.get(temp);
 86                 str.setCount(str.getCount()+1);
 87             
 88         }
 89         
 90         //测试countMap是否算是成功达到目的
 91                 Set<String> keys=countMap.keySet();
 92                 for (String key:keys) {
 93                     Str sd=countMap.get(key);
 94                     Integer cInteger=sd.getCount();
 95                     System.out.println("字母:"+key+"--次数:"+cInteger);
 96                 }
 97     }
 98 
 99 }
100 
101 //第二种分拣思想:(1)第一次为key创建容器,并存key对应的值(2)第二次使用创建好的容器,存放key对应的值
102 
103 /* 【分拣的思想】
104  *  第一种:为所有key创建容器
105  *        之后存放对应的value
106  *  第二种:第一次创建容器,并存放value
107  *       第二次之后,直接使用容器存放value
108 * @author qjc
109 * @date 2016-3-9
110 *
111  */
112 public class TestMap {
113     public static void main(String[] args) {
114         test4();    
115     } 
116     //分拣第二种思想 (1)第一次为key创建容器,并存放值(2)第二次使用容器存放值
117     public static void test3(){
118         String sts="this is a cat and that is a nice and where is the food";
119         //将字符串分割成一个个单词,并存放入数组中
120         String[] strings=sts.split(" ");
121         //创建一个map对象,用来存放单词和单词出现的次数
122         Map<String, Str> countMap=new HashMap<String, Str>();
123         //第一种分拣思想
124         //第一步:为key创建容器的同时,并存放值
125         for(int i=0;i<strings.length;i++){
126             String temp=strings[i];
127             //判断map是否含有此key,如果有返回true,否则返回false
128             //第一次创建容器,并为容器中存放值
129             if(!countMap.containsKey(temp)){
130                 Str str=new Str();
131                 str.setCount(1);
132                 countMap.put(temp, str);
133             }else{
134                 //第二次使用容器存放值
135                 Str str=countMap.get(temp);
136                 str.setCount(str.getCount()+1);
137             }
138         }
139         
140         //测试countMap是否算是成功达到目的
141                 Set<String> keys=countMap.keySet();
142                 for (String key:keys) {
143                     Str sd=countMap.get(key);
144                     Integer cInteger=sd.getCount();
145                     System.out.println("字母:"+key+"--次数:"+cInteger);
146                 }
147     }
148     
149     
150     //第二种分拣思路:(1)第一次为key创建容器,并存放值(2)第二次使用容器存放值
151     public static void test4(){
152         String sts="this is a cat and that is a nice and where is the food";
153         //将字符串分割成一个个单词,并存放入数组中
154         String[] strings=sts.split(" ");
155         //创建一个map对象,用来存放单词和单词出现的次数
156         Map<String, Str> countMap=new HashMap<String, Str>();
157         //第一种分拣思想
158         //第一步:为key创建容器的同时,并存放值
159         for(int i=0;i<strings.length;i++){
160             String temp=strings[i];
161             //判断map是否含有此key,如果有返回true,否则返回false
162             //第一次创建容器,并为容器中存放值
163             Str str=null;
164             if(null==(str=countMap.get(temp))){
165                  str=new Str();
166                 str.setCount(1);
167                 countMap.put(temp, str);
168             }else{
169                 //第二次使用容器存放值
170                  str=countMap.get(temp);
171                 str.setCount(str.getCount()+1);
172             }
173         }
174         
175         //测试countMap是否算是成功达到目的
176                 Set<String> keys=countMap.keySet();
177                 for (String key:keys) {
178                     Str sd=countMap.get(key);
179                     Integer cInteger=sd.getCount();
180                     System.out.println("字母:"+key+"--次数:"+cInteger);
181                 }
182     }
183 }

例2:

分拣思想的应用

 需求:查询出学生List集合,对学生集合进行加工,将学生按照班级分类,并求出班级的总分和平均分

 思路:map分拣思想。需要创建一个班级po,班级po里存放学生信息,该班集的总分,班级号码。

/**
 *思考: 
 *    定义一个Student类,属性:name 姓名,no班号,score 成绩   
 *    现在将若干Student对象放入List,请统计出每个班级的总分和平均分
 *方案: 面向对象+分拣存储(1:N)
* @author qjc
 * 2016-3-9
 */
public class MapDemo02 {
    /**
     * 模拟考试 测试数据到List
     */
    public static List<Student> exam(){
        List<Student> list = new ArrayList<>();
        list.add(new Student("张三", "java", 84));
        list.add(new Student("李四", "java", 88));
        list.add(new Student("王五", "c++", 90));
        list.add(new Student("赵六", "c++", 98));
        return list;
    }
    /**
     * 统计分析
     *     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){
            String no = stu.getNo(); //班级编号
            double score = stu.getScore(); //成绩
            //2、分拣 查看是否存在该编号的班级
            //如果不存在,创建班级
            ClassRoom room = map.get(no);
            if(null==room){
                room = new ClassRoom(no);
                map.put(no, room);
            }
            //存在,放入学生
            room.getStudents().add(stu);
            //计算总分
            room.setTotal(room.getTotal()+score);
        }
        return map;
     }
    /**
     * 查看每个班级的总分和平均分 --->遍历map
     */
    public static void view(Map<String, ClassRoom> map){
        Set<String> keySet = map.keySet();
        Iterator<String> iter = keySet.iterator();
        while(iter.hasNext()){
            String no = iter.next();
            ClassRoom room = map.get(no);
            //查看总分  计算平均分
            double total = room.getTotal();
            double avg = total/room.getStudents().size();
            System.out.println(no+"---->"+total+"---->"+avg);
        }
    }
    public static void main(String[] args) {
        //1、考试
        List<Student> list = exam();
        //2、分析成绩
        Map<String, ClassRoom> map = count(list);
        //3、查看成绩(总分、平均分)
        view(map);
    }
}

/**
 * 一个班级多个学生(学生列表)
 * @author qjc
 * 2016-3-9
 */
public class ClassRoom {
    private String no;//班级
    private List<Student> students; //班级列表
    private double total; //总分
    public ClassRoom() {
        students = new ArrayList<>();
    }
    public ClassRoom(String no) {
        this();
        this.no = no;
    }
    //…..
}
 
public class Student {
    private String name; 
    private String no; //班级
    private double score; //成绩
    //…..
}
原文地址:https://www.cnblogs.com/dooor/p/5285488.html