[零基础学JAVA]Java SE应用部分-35.JAVA类集之二-阿里云开发者社区

开发者社区> 数据库> 正文
登录阅读全文

[零基础学JAVA]Java SE应用部分-35.JAVA类集之二

简介:
JAVA按以下重要顺序 
· 面向对象 
· 类集框架 
· JDBC 
· 文件编程
本季目标
主要讲解以下的内容: 
· Collection 
· List 
· Set 
· Map 
· Iterator 
· ListIteator 
· Enumeration 
· for…each语句(JDK 1.5的新特性) 
· Comparable接口 
· 二叉树
1、类集结构
(1)类集的产生目的
类集简单来讲就是一个动态的对象数组,此对象数组可以改变大小,可以任意的实现对象的增加、删除、输出。所有的类集存放java.util包中。
(2)类集的划分 
1、存放单值:Collection 
2、存放一对值:Map 
3、输出:Iterator
(3)类集的具体概念 
<1>Collection(只能存放一个值) 
|- 主要功能:用于输出使用 
|- 子接口:List(允许有重复的元素,而且加入的顺序就是输出的顺序) 
    |- 子类:ArrayList,是在Java 2之后推出的,是新的类,是使用异步处理方式,其性能较高 
    |- 子类:Vector,是在JDK 1.0的时候就推出,因为是旧的类,有很多List所没有的功能,是使用同步处理方式,其线程安全性较高,会比较慢。使用Vector除了可以使用Iterator输出之外,也可以使用Enumeration进行输出。两者是通用的。Vector子类允许直接使用addElement(Object obj),也是可以向集合中加入元素的。 
    |- 自己新增的方法: 
        |- 取每一个对象:public Object get(int ind) ; 
    |- 子类:Stack(栈) 
|- 子接口:Set(不允许有重复元素,重复元素会替换)?如何可以区分重复元素呢?  
    |- 子类:HashSet:是无序列存放 
    |- 子类:TreeSet:有序存放,是通过Comparable接口完成的 
|- 常用方法: 
    |- 增加一个对象:public boolean add(Object obj) 
    |- 取得类集的长度:public int size() ; 
    |- 判断集合中的内容是否为空:public boolean isEmpty() 
    |- 删除一个对象:public boolean remove(Object obj) :就必须牵扯到对象的比较状态。 
    |- 实例化Iterator对象:public Iterator iterator() ; 
<2>JDK 1.5提供了一个方便的输出操作:for…each语句。 
<3>Iterator接口输出是最常见的
 
|- 常用方法 
    |- public boolean hasNext():判断是否有下一个元素 
    |- public Object next():取出下一个元素 
|- 子接口:ListIterator,可以进行双向输出,只对于List接口有用 
    |- 常用方法:具备了Iterator接口中的全部内容 
        |- public boolean hasPrevious() 
        |- public Object previous()  
<4>Enumeration(在1.5之后也加入、泛型支持哈~) 
|- 常用方法: 
    |- 判断是否有下一个元素:public boolean hasMoreElements() 
    |- 取出元素:public Object element() 
5、 Map(存放一对值) 
|- 功能:与Collection的功能不一样,Map的主要功能是用于查找使用的。 
|- 注意点: 
    |- 使用Map操作时,不能直接使用Iterator进行输出。 
    |- 而且里面不能有重复key。 
    |- 如果非要使用Iterator进行输出,则必须按以下的步骤进行操作: 
|- Map --> Set --> Iterator --> Map.Entry --> key与value的分离 
|- 常用子类: 
    |- HashMap:无序存放,是新的类,是JDK 1.2时推出的,是异步处理,性能较高 
    |- Hashtable:是旧的类,是JDK 1.0时推出的,是线程安全的,性能较低 
        |- Properties(重点):属性文件操作类 
    |- TreeMap:有序存放 
|- 常用方法: 
    |- 向集合中保存数据:public Object put(Object key,Object value):返回的是value 
    |- 从集合中查找数据:public Object get(Object key):根据key查找。 
    |- 将Map数据变为Set实例:Set<Map.Entry<K,V>> entrySet() 
    |- 将全部的key变为set实例:Set<K> keySet() 
    |- 将全部的vlaue变为Collection接口实例:Collection<V> values() 
|- Map.Entry:保存key与value 
    |- public Object getKey() 
    |- public Object getValue() 
|- 存放key的注意点:
################Michael分割线####################
2、代码讲解
(6)实例一 
例如,一个学校里有多个学生?问如何可以表现出关系
一对多哈,类似数据库
import java.util.*;    
class School{    
        private String name;    
        //一个学校有多个学生    
        private List<Student> allStudents;    
        public School(String name){    
                this.setName(name);    
                this.setAllStudents(new ArrayList<Student>());    
        }    
        //向学校中加入学生    
        public void add(Student stu){    
                this.allStudents.add(stu);    
        }    
        public void setName(String name){    
                this.name = name;    
        }    
        public String getName(){    
                return this.name;    
        }    
        public void setAllStudents(List<Student> allStudents){    
                this.allStudents = allStudents;    
        }    
        public List<Student> getAllStudents(){    
                return this.allStudents;    
        }    
}    
class Student{    
        private String name;    
        private int age;    
        //一个学生属于一个学校    
        private School school;    
        public Student(String name,int age){    
                this.setName(name);    
                this.setAge(age);    
        }    
        public void setName(String name){    
                this.name = name;    
        }    
        public String getName(){    
                return this.name;    
        }    
        public void setAge(int age){    
                this.age = age;    
        }    
        public int getAge(){    
                return this.age;    
        }    
        public void setSchool(School school){    
                this.school = school;    
        }    
        public School getSchool(){    
                return this.school;    
        }    
}    
public class Demo11{    
        public static void main(String[] args){    
                School sch = new School("51cto");    
                Student stu1 = new Student("张三",20);    
                Student stu2 = new Student("李四",25);    
                Student stu3 = new Student("王五",23);    
                Student stu4 = new Student("赵六",28);    
                Student stu5 = new Student("孙七",35);    
                //设置关系    
                sch.add(stu1);    
                stu1.setSchool(sch);    
                sch.add(stu2);    
                stu2.setSchool(sch);    
                sch.add(stu3);    
                stu3.setSchool(sch);    
                sch.add(stu4);    
                stu4.setSchool(sch);    
                sch.add(stu5);    
                stu5.setSchool(sch);    
                //肯定此时可以从一个学校里打印出全部的学生信息    
                //学生也可以清楚的知道自己属于哪一个学校    
                System.out.println("学校名称:"+sch.getName());    
                List<Student> all = sch.getAllStudents();    
                Iterator<Student> iter = all.iterator();    
                while(iter.hasNext()){    
                        Student s = iter.next();    
                        System.out.println("\t|- 学生姓名:"+s.getName()+",年龄:"+s.getAge());    
                }    
        }    
}
看下效果:
image
我们也可以通过学生查找学校信息哈~
import java.util.*;    
class School{    
        private String name;    
        //一个学校有多个学生    
        private List<Student> allStudents;    
        public School(String name){    
                this.setName(name);    
                this.setAllStudents(new ArrayList<Student>());    
        }    
        //向学校中加入学生    
        public void add(Student stu){    
                this.allStudents.add(stu);    
        }    
        public void setName(String name){    
                this.name = name;    
        }    
        public String getName(){    
                return this.name;    
        }    
        public void setAllStudents(List<Student> allStudents){    
                this.allStudents = allStudents;    
        }    
        public List<Student> getAllStudents(){    
                return this.allStudents;    
        }    
}    
class Student{    
        private String name;    
        private int age;    
        //一个学生属于一个学校    
        private School school;    
        public Student(String name,int age){    
                this.setName(name);    
                this.setAge(age);    
        }    
        public void setName(String name){    
                this.name = name;    
        }    
        public String getName(){    
                return this.name;    
        }    
        public void setAge(int age){    
                this.age = age;    
        }    
        public int getAge(){    
                return this.age;    
        }    
        public void setSchool(School school){    
                this.school = school;    
        }    
        public School getSchool(){    
                return this.school;    
        }    
}    
public class Demo11{    
        public static void main(String[] args){    
                School sch = new School("51cto");    
                Student stu1 = new Student("张三",20);    
                Student stu2 = new Student("李四",25);    
                Student stu3 = new Student("王五",23);    
                Student stu4 = new Student("赵六",28);    
                Student stu5 = new Student("孙七",35);    
                //设置关系    
                sch.add(stu1);    
                stu1.setSchool(sch);    
                sch.add(stu2);    
                stu2.setSchool(sch);    
                sch.add(stu3);    
                stu3.setSchool(sch);    
                sch.add(stu4);    
                stu4.setSchool(sch);    
                sch.add(stu5);    
                stu5.setSchool(sch);    
                //肯定此时可以从一个学校里打印出全部的学生信息    
                //学生也可以清楚的知道自己属于哪一个学校    
                System.out.println("学校名称:"+sch.getName());    
                List<Student> all = sch.getAllStudents();    
                Iterator<Student> iter = all.iterator();    
                while(iter.hasNext()){    
                        Student s = iter.next();    
                        System.out.println("\t|- 学生姓名:"+s.getName()+",年龄:"+s.getAge()+"-->"+s.getSchool().getName());    
                }    
        }    
}
这是典型的双向关系模型哈~
image 
进一步扩展呢?一个学校有多个班级,一个班级有多个学生?
import java.util.*;    
class School{    
        private String name;    
        //一个学校有多个学生    
        private List<Cls> allClss;    
        public School(String name){    
                this.setName(name);    
                this.setAllClss(new ArrayList<Cls>());    
        }    
        //向学校中加入学生    
        public void add(Cls cls){    
                this.allClss.add(cls);    
        }    
        public void setName(String name){    
                this.name = name;    
        }    
        public String getName(){    
                return this.name;    
        }    
        public void setAllClss(List<Cls> allClss){    
                this.allClss = allClss;    
        }    
        public List<Cls> getAllClss(){    
                return this.allClss;    
        }    
}    
class Cls{    
        private String name;    
        private School school;    
        private List<Student> allStudents;    
        public Cls(String name){    
                this.setName(name);    
                this.setAllStudents(new ArrayList<Student>());    
        }    
        //向班级中加入学生    
        public void add(Student stu){    
                this.allStudents.add(stu);    
        }    
        public List<Student> getAllStudents(){    
                return this.allStudents;    
        }    
        public void setAllStudents(List<Student> allStudents){    
                this.allStudents = allStudents;    
        }    
        public void setSchool(School school){    
                this.school = school;    
        }    
        public School getSchool(){    
                return this.school;    
        }    
        public void setName(String name){    
                this.name = name;    
        }    
        public String getName(){    
                return this.name;    
        }    
}    
class Student{    
        private String name;    
        private int age;    
        //一个学生属于一个学校    
        private Cls cls;    
        public Student(String name,int age){    
                this.setName(name);    
                this.setAge(age);    
        }    
        public void setName(String name){    
                this.name = name;    
        }    
        public String getName(){    
                return this.name;    
        }    
        public void setAge(int age){    
                this.age = age;    
        }    
        public int getAge(){    
                return this.age;    
        }    
        public void setCls(Cls cls){    
                this.cls = cls;    
        }    
        public Cls getCls(){    
                return this.cls;    
        }    
}    
public class Demo12{    
        public static void main(String[] args){    
                School sch = new School("51cto");    
                Cls c1 = new Cls("Java一班");    
                Cls c2 = new Cls("Java二班");    
                Cls c3 = new Cls("Java三班");    
                Student stu1 = new Student("张三",20);    
                Student stu2 = new Student("李四",25);    
                Student stu3 = new Student("王五",23);    
                Student stu4 = new Student("赵六",28);    
                Student stu5 = new Student("孙七",35);    
                //设置关系    
                sch.add(c1);    
                c1.setSchool(sch);    
                sch.add(c2);    
                c2.setSchool(sch);    
                sch.add(c3);    
                c3.setSchool(sch);    
                c1.add(stu1);    
                stu1.setCls(c1);    
                c2.add(stu2);    
                stu2.setCls(c2);    
                c2.add(stu3);    
                stu3.setCls(c2);    
                c3.add(stu4);    
                stu4.setCls(c3);    
                c3.add(stu5);    
                stu5.setCls(c3);    
                //如果要输出全部的内容,则肯定要进行双重循环    
                System.out.println("学校名称:"+sch.getName());    
                Iterator<Cls> iter1 = sch.getAllClss().iterator();    
                while(iter1.hasNext()){    
                        Cls c = iter1.next();    
                        System.out.println("\t|- 班级名称:"+c.getName());    
                        //输出一个班级中的全部学生    
                        Iterator<Student> iter2 = c.getAllStudents().iterator();    
                        while(iter2.hasNext()){    
                                Student s = iter2.next();    
                                System.out.println("\t\t|- 学生姓名:"+s.getName()+",学生年龄:"+s.getAge()+" --> "+s.getCls().getSchool().getName());    
                        }    
                }    
        }    
}
看下效果:
image
(7)实例二 
例如有一种情况: 
一个学生可以选多门课程,一门课程允许有多个学生参加。
import java.util.*;    
class Student{    
        private String name;    
        private int age;    
        //一个学生可以选多门课程    
        private List<Course> allCourses;    
        public Student(String name,int age){    
                this.setName(name);    
                this.setAge(age);    
                this.setAllCourses(new ArrayList<Course>());    
        }    
        public void add (Course course){    
                this.allCourses.add(course);    
        }    
        public void setName(String name){    
                this.name = name;    
        }    
        public String getName(){    
                return this.name;    
        }    
        public void setAge(int age){    
                this.age = age;    
        }    
        public int getAge(){    
                return this.age;    
        }    
        public void setAllCourses(List<Course> allCourses){    
                this.allCourses = allCourses;    
        }    
        public List<Course> getAllCourses(){    
                return this.allCourses;    
        }    
}    
class Course{    
        private String name;    
        private float grade;    
        private List<Student> allStudents;    
        public Course(String name,float grade){    
                this.setName(name);    
                this.setGrade(grade);    
                this.setAllStudents(new ArrayList<Student>());    
        }    
        public void add(Student student){    
                this.allStudents.add(student);    
        }    
        public void setName(String name){    
                this.name = name;    
        }    
        public void setGrade(float grade){    
                this.grade = grade;    
        }    
        public void setAllStudents(List<Student> allStudents){    
                this.allStudents = allStudents;    
        }    
        public String getName(){    
                return this.name;    
        }    
        public float getGrade(){    
                return this.grade;    
        }    
        public List<Student> getAllStudents(){    
                return this.allStudents;    
        }    
}    
public class Demo13{    
        public static void main(String[] args){    
                Student stu1 = new Student("张三",20);    
                Student stu2 = new Student("李四",25);    
                Student stu3 = new Student("王五",23);    
                Student stu4 = new Student("赵六",28);    
                Student stu5 = new Student("孙七",35);    
                Course c1 = new Course("JAVA SE 基础",2.0f);    
                Course c2 = new Course("JAVA EE WEB",3.0f);    
                Course c3 = new Course("分布式开发 ",5.0f);    
                //设置关系    
                stu1.add(c1);    
                c1.add(stu1);    
                stu1.add(c2);    
                c2.add(stu1);    

                stu2.add(c2);    
                c2.add(stu2);    

                stu3.add(c1);    
                c1.add(stu3);    
                stu3.add(c2);    
                c2.add(stu3);    
                stu3.add(c3);    
                c3.add(stu3);    

                stu4.add(c3);    
                c3.add(stu4);    
                stu5.add(c3);    
                c3.add(stu5);    
                //随便验证一个学生所选择的课程以及一门课程所参加的学生    
                System.out.println("学生姓名:"+stu1.getName());    
                Iterator<Course> iter1 = stu1.getAllCourses().iterator();    
                while(iter1.hasNext()){    
                        Course c = iter1.next();    
                        System.out.println("\t|- 课程名称:"+c.getName()+",学分:"+c.getGrade());    
                }    
                System.out.println("课程名称:"+c3.getName());    
                Iterator<Student> iter2 = c3.getAllStudents().iterator();    
                while(iter2.hasNext()){    
                        Student s = iter2.next();    
                        System.out.println("\t|- 学生姓名:"+s.getName()+",学生年龄:"+s.getAge());    
                }    
        }    

看下效果:
image
#####################Michael分割线#########################






本文转自redking51CTO博客,原文链接:http://blog.51cto.com/redking/139795,如需转载请自行联系原作者

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享: