容器分类

对容器中的对象按属性分类,用面向对象的思路看看如何实现。

案例:学生类Student中,包含属性(成绩,班级,姓名)。生成若干学生对象,求出各个班级的平均分。

这个问题若干直接使用循环判断,找到所有的班级种类再按班级找学生,再存储,过程非常复杂。

面向对象的思路:建立一个班级类,用于储存属于这个班级的学生;再用一个Map容器储存(班级名称,班级对象)。

代码:

  1 package com.hlyc.Stream;
  2 
  3 import java.util.ArrayList;
  4 import java.util.HashMap;
  5 import java.util.List;
  6 import java.util.Map;
  7 import java.util.Map.Entry;
  8 import java.util.Set;
  9 
 10 public class Demo1 {
 11     public static void main(String args[]){
 12         List list = new ArrayList();
 13         generate(list);
 14         Map<String, ClassRoom> map = new HashMap<String, ClassRoom>(); 
 15         count(list, map);
 16         Set<Entry<String, ClassRoom>> entrySet = map.entrySet();
 17         for(Entry<String, ClassRoom> entry : entrySet)
 18             System.out.println(entry.getKey() + "班" + "	总分:" 
 19                     + entry.getValue().getTotal() + "	平均分:" 
 20                                 + entry.getValue().getTotal()/entry.getValue().getList().size());
 21     }
 22     public static void generate(List<Student> list){
 23         list.add(new Student("1", 120, "c"));
 24         list.add(new Student("2", 130, "x"));
 25         list.add(new Student("3", 140, "y"));
 26         list.add(new Student("3", 105, "z"));
 27         list.add(new Student("2", 107, "a"));
 28         list.add(new Student("1", 110, "b"));
 29     }
 30     public static void count(List<Student> list, Map<String, ClassRoom> map){
 31         for(Student student : list){
 32             String cid = student.getCid();
 33             double grade = student.getGrade();
 34             if(!map.containsKey(cid))
 35                 map.put(cid, new ClassRoom());
 36             map.get(cid).setTotal(map.get(cid).getTotal() + grade);
 37             map.get(cid).getList().add(student);
 38         }
 39     }
 40 }
 41 class Student{
 42     private String cid;
 43     private double grade;
 44     private String name;
 45     public String getCid() {
 46         return cid;
 47     }
 48     public void setCid(String cid) {
 49         this.cid = cid;
 50     }
 51     public double getGrade() {
 52         return grade;
 53     }
 54     public void setGrade(double grade) {
 55         this.grade = grade;
 56     }
 57     public String getName() {
 58         return name;
 59     }
 60     public void setName(String name) {
 61         try {
 62             this.name = name;
 63         } finally {
 64             // TODO: handle finally clause
 65         }
 66     }
 67     public Student(String cid, double grade, String name) {
 68         this.cid = cid;
 69         this.grade = grade;
 70         this.name = name;
 71     }
 72     public Student(){
 73         
 74     }
 75     @Override
 76     public String toString() {
 77         return "Student [cid=" + cid + ", grade=" + grade + ", name=" + name + "]";
 78     }
 79 }
 80 class ClassRoom{
 81     private String cid;
 82     private double total;
 83     List<Student> list;
 84     public String getCid() {
 85         return cid;
 86     }
 87     public void setCid(String cid) {
 88         this.cid = cid;
 89     }
 90     public double getTotal() {
 91         return total;
 92     }
 93     public void setTotal(double total) {
 94         this.total = total;
 95     }
 96     public List<Student> getList() {
 97         return list;
 98     }
 99     public void setList(List<Student> list) {
100         this.list = list;
101     }
102     public ClassRoom(String cid, double total, List<Student> list) {
103         this.cid = cid;
104         this.total = total;
105         this.list = list;
106     }
107     public ClassRoom(){
108         this.list = new ArrayList();
109     }
110     @Override
111     public String toString() {
112         return "ClassRoom [cid=" + cid + ", total=" + total + ", list=" + list + "]";
113     }
114 }

这样就很简单了。不相信的同学可以试试使用循环遍历加判断的方法做。

原文地址:https://www.cnblogs.com/cxy2016/p/7157242.html