java集合框架

两大接口:CollectionMap接口

一、Collection接口:

子接口:1>List(序列):排列有序且可重复,常用实现类:ArrayList(数组序列)

           2>Queue(队列):常用实现类:LinkedList(链表)(同时也是List的一个实现类)

           3>Set(集合):无序不可重复,常用实现类:HashSet(哈希集)


二、Map接口:

一般靠HashMap类来实现接口


三、存储对象区别

     Collection下的类对象存储为单个元素

     Map:HashMap则是以键值对<key-value>成对存储的


下面通过模拟学生选课来进一步解析:

      Course类:

    

package com.zhaixiaobin.collecton;

public class Course {
     private int id;
     private String name;
     public int getId() {
        return id;
    }
    public String getName() {
        return name;
    }
    public Course(int id,String name)
     {
         this.id=id;
         this.name=name;
     }

}

           Student类:

   

package com.zhaixiaobin.collecton;

import java.util.HashSet;
import java.util.Set;

public class Students {
    private int id;
    private String name;
    public Set courses;
    public int getId() {
        return id;
    }
    public String getName() {
        return name;
    }
    
    public Students(int id,String name)
    {
        this.id=id;
        this.name=name;
        this.courses=new HashSet();
    }
}

             ListTest类(测试类):

 下面代码实现了四种添加元素的方法和两种对添加到集合中元素的遍历方法以及一些其他的方法:

 添加元素:1》使用ArrayList类的add(元素)方法:

                     形式:ArrayList对象.add();

               2》使用ArrayList类的add(int index,元素)方法加到指定下标方法:

                     形式:ArrayList对象.add(int index,元素);

                    注意:index不能越界,否则会出现下标越界异常

               3》使用ArrayList类的addAll(Arrays.asList(元素数组))方法:

                      形式ArrayList对象.addALL(Arrays.asList(元素数组))

               4》使用ArrayList类的addALL(int index ,Arrays.asList(元素数组))方法

                     形式ArrayList对象.addALL(int index,Arrays.asList(元素数组))

    遍历:  1》使用for循环

               2》使用迭代器(Iterator)

    修改元素:ArrayList对象的set方法

                 形式:ArrayList对象.set(int index,新的元素);

    删除单个元素:ArrayList对象的remove方法

                       形式:ArrayList对象.remove(元素对象)

                               或者

                               ArrayList对象.remove(元素下标)  

     删除多个元素:ArrayList对象的removeAll方法

                        形式:ArrayList对象.removeAll(Arrays.asList(元素数组));                      

package com.zhaixiaobin.collecton;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class ListTest {
    public List Coursetoselect;
    public ListTest()
    {
        this.Coursetoselect=new ArrayList();
    }
    public void addTets()
    {
        Course cr1=new Course(1,"数据结构");
        Coursetoselect.add(cr1);//此时"数据结构"在下标为0的位置
        Course temp1=(Course)Coursetoselect.get(0);//获得下标为0的元素即"数据结构"
        System.out.println("添加了课程:"+temp1.getId()+" "+temp1.getName());//打印输出
        
        Course cr2=new Course(2,"C语言");
        Coursetoselect.add(cr2);//此时"c语言"在下标为1的位置
        Course temp2=(Course)Coursetoselect.get(1);//获得下标为1的元素即"c语言"
        System.out.println("添加了课程:"+temp2.getId()+" "+temp2.getName());
//        
//        Course cr3=new Course(3,"操作系统");
//        Coursetoselect.add(1,cr3);//这个add方法是将元素填加到指定下标的位置即1,所以此时"操作系统"在下标为1的位置,从而c语言被挤到了下标为2 的位置
//        Course temp3=(Course)Coursetoselect.get(2);//此时获得的是c语言
//        System.out.println("添加了课程:"+temp3.getId()+" "+temp3.getName());
        
        /*
         * 使用addAll(Arrays.asList)方法添加
         */
        Course [] course2={new Course(3, "数据库"),new Course(4,"计算机网络")};
        Coursetoselect.addAll(Arrays.asList(course2));
        Course temp3=(Course) Coursetoselect.get(2);
        Course temp4=(Course) Coursetoselect.get(3);
        System.out.println("添加了两门课程:"+temp3.getId()+" "+temp3.getName()+";"+temp4.getId()+":"+temp4.getName());
        /*
         * 使用addAll(int index,Arrays.asList)将元素数组添加到指定的位置下标下
         */
        Course [] course3={new Course(5,"大学英语"),new Course(6,"大学物理")};
        Coursetoselect.addAll(2, Arrays.asList(course3));//将数组元素添加到下标为2的位置,即原来为数据库的位置
        Course temp5=(Course) Coursetoselect.get(2);//获得下标为2的位置,现在是大学英语
        Course temp6=(Course) Coursetoselect.get(3);//获得下标为3的位置,现在是大学物理
        System.out.println("添加了两门课程:"+temp5.getId()+" "+temp5.getName()+";"+temp6.getId()+":"+temp6.getName());
    }
    /*
     * 通过for循环遍历添加的元素
     */
    public  void ListGet()
    {
        System.out.println("已选课程为:");
        for(int i=0;i<Coursetoselect.size();i++)
        {
            Course cr=(Course) Coursetoselect.get(i);
            System.out.println("课程号:"+cr.getId()+" "+"课程名:"+cr.getName());
        }
        System.out.println();
    }
    /*
     * 通过迭代器来遍历添加的元素
     */
    public void testIterator()
    {
        Iterator it=Coursetoselect.iterator();
        System.out.println("已选课程为");
        while(it.hasNext())
        {
            Course ctr=(Course) it.next();
            System.out.println("课程号:"+ctr.getId()+" "+"课程名:"+ctr.getName());
        }

         /*
          * 修改List中的元素
          */
         public void testModify()
        {
           Coursetoselect.set(1, new Course(7,"毛概"));//将下标为1(即c语言位置)的元素修改为毛概
           System.out.println();
        }


    }
    public static void main(String[] args) {
        ListTest lt1=new ListTest();
        lt1.addTets();
        lt1.ListGet();
        lt1.testIterator();

          lt1.testModify();//调用修改方法
          lt1.testIterator();


    }

}

输出结果:

添加了课程:1 数据结构
添加了课程:2 C语言
添加了两门课程:3 数据库;4:计算机网络
添加了两门课程:5 大学英语;6:大学物理
已选课程为:
课程号:1 课程名:数据结构
课程号:2 课程名:C语言
课程号:5 课程名:大学英语
课程号:6 课程名:大学物理
课程号:3 课程名:数据库
课程号:4 课程名:计算机网络

已选课程为
课程号:1 课程名:数据结构
课程号:2 课程名:C语言
课程号:5 课程名:大学英语
课程号:6 课程名:大学物理
课程号:3 课程名:数据库
课程号:4 课程名:计算机网络


  已选课程为
  课程号:1 课程名:数据结构
  课程号:7 课程名:毛概
  课程号:5 课程名:大学英语
  课程号:6 课程名:大学物理
  课程号:3 课程名:数据库
  课程号:4 课程名:计算机网络


引入:泛型(只能存储某个特定类型的元素,方便获取,而不需要每次转换为相应的类型)

  创建一个具有泛型的集合对象:List<类型名>对象名

package com.zhaixiaobin.collecton;

import java.util.ArrayList;
import java.util.List;

public class TestGeneric {
    public List<Course> courses;
    public TestGeneric()
    {
        this.courses=new ArrayList<Course>();
    }
    public void TestADD()
    {
        Course cr1=new Course(1,"大学语文");
        courses.add(cr1);
        Course cr2=new Course(2,"高等数学");
        courses.add(cr2);
    }
    public void testForEach()
    {
        System.out.println("输出添加课程");
        for(Course cr:courses)
        {
            System.out.println("课程号:"+cr.getId()+" 课程名:"+cr.getName());
        }
    }
    public static void main(String[] args) {
      TestGeneric tg=new TestGeneric();
      tg.TestADD();
      tg.testForEach();

    }

}

输出结果:

输出添加课程
课程号:1 课程名:大学语文
课程号:2 课程名:高等数学

 注意:泛型还可以添加泛型子类型的对象

 java集合框架

package com.zhaixiaobin.collecton;

import java.util.ArrayList;
import java.util.List;

public class TestGeneric {
    public List<Course> courses;
    public TestGeneric()
    {
        this.courses=new ArrayList<Course>();
    }
    public void TestADD()
    {
        Course cr1=new Course(1,"大学语文");
        courses.add(cr1);
        Course cr2=new Course(2,"高等数学");
        courses.add(cr2);
    }
    public void testForEach()
    {
        System.out.println("输出添加课程");
        for(Course cr:courses)
        {
            System.out.println("课程号:"+cr.getId()+" 课程名:"+cr.getName());
        }
    }
    /*
     * 泛型集合可以添加泛型子类型的对象实例
     */
    public void TestChild()
    {
        ChildCourse ccr=new ChildCourse();
        ccr.setId(3);
        ccr.setName("离散书学");
        courses.add(ccr);
    }
    public static void main(String[] args) {
      TestGeneric tg=new TestGeneric();
      tg.TestADD();
      tg.TestChild();
      tg.testForEach();
    }

}

输出结果:

输出添加课程
课程号:1 课程名:大学语文
课程号:2 课程名:高等数学
课程号:3 课程名:离散数学

注意:泛型不能使用基本类型,例如int ,long,boolean等,而应该使用它们的包装类Integer,Long,Boolean


Set接口:(存储的元素无序不可重复),实现类:HashSet

Students类:

package com.zhaixiaobin.collecton;

import java.util.HashSet;
import java.util.Set;

public class Students {
    public static final String CoursetoSelect = null;
    private int id;
    private String name;
    public Set<Course> courses;
    public int getId() {
        return id;
    }
    public String getName() {
        return name;
    }
    
    public Students(int id,String name)
    {
        this.id=id;
        this.name=name;
        this.courses=new HashSet<Course>();
    }
}

TestSet类:

package com.zhaixiaobin.collecton;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

import jdk.nashorn.internal.runtime.regexp.joni.ScanEnvironment;

public class TestSet {
    public List<Course> CoursetoSelect;
    public TestSet()
    {
        CoursetoSelect=new ArrayList<Course>();
    }
    public void addTets()
    {
        Course cr1=new Course(1,"数据结构");
        CoursetoSelect.add(cr1);//此时"数据结构"在下标为0的位置
        Course temp1=(Course)CoursetoSelect.get(0);//获得下标为0的元素即"数据结构"
//        System.out.println("添加了课程:"+temp1.getId()+" "+temp1.getName());//打印输出
        
        Course cr2=new Course(2,"C语言");
        CoursetoSelect.add(cr2);//此时"c语言"在下标为1的位置
        Course temp2=(Course)CoursetoSelect.get(1);//获得下标为1的元素即"c语言"
//        System.out.println("添加了课程:"+temp2.getId()+" "+temp2.getName());
//        
//        Course cr3=new Course(3,"操作系统");
//        Coursetoselect.add(1,cr3);//这个add方法是将元素填加到指定下标的位置即1,所以此时"操作系统"在下标为1的位置,从而c语言被挤到了下标为2 的位置
//        Course temp3=(Course)Coursetoselect.get(2);//此时获得的是c语言
//        System.out.println("添加了课程:"+temp3.getId()+" "+temp3.getName());
        
        /*
         * 使用addAll(Arrays.asList)方法添加
         */
        Course [] course2={new Course(3, "数据库"),new Course(4,"计算机网络")};
        CoursetoSelect.addAll(Arrays.asList(course2));
        Course temp3=(Course) CoursetoSelect.get(2);
        Course temp4=(Course) CoursetoSelect.get(3);
//        System.out.println("添加了两门课程:"+temp3.getId()+" "+temp3.getName()+";"+temp4.getId()+":"+temp4.getName());
        /*
         * 使用addAll(int index,Arrays.asList)将元素数组添加到指定的位置下标下
         */
        Course [] course3={new Course(5,"大学英语"),new Course(6,"大学物理")};
        CoursetoSelect.addAll(2, Arrays.asList(course3));//将数组元素添加到下标为2的位置,即原来为数据库的位置
        Course temp5=(Course) CoursetoSelect.get(2);//获得下标为2的位置,现在是大学英语
        Course temp6=(Course) CoursetoSelect.get(3);//获得下标为3的位置,现在是大学物理
//        System.out.println("添加了两门课程:"+temp5.getId()+" "+temp5.getName()+";"+temp6.getId()+":"+temp6.getName());
    }
    public void testForEach()
    {
        for (Object obj : CoursetoSelect) {
            Course cr=(Course)obj;
            System.out.println("课程号:"+cr.getId()+" 课程名:"+cr.getName());
           }   
        }
    public void testForEachforSet(Students student)
    {
        for (Course course : student.courses) {
            System.out.println("选择了课程:"+course.getId()+" "+course.getName());
        }
    }
    public static void main(String[] args) {
        TestSet ts=new TestSet();
        ts.addTets();
        ts.testForEach();
        Students stu=new Students(1, "张三");
        System.out.println("欢迎"+stu.getName()+"同学选课");
        Scanner in=new Scanner(System.in);
        for(int i=0;i<3;i++)
        {
            System.out.println("请输入ID:");
            int courseId=in.nextInt();
            for(Course cr:ts.CoursetoSelect)
            {
                if(cr.getId()==courseId)
                {
                    stu.courses.add(cr);
                }
            }
                          
        }
        ts.testForEachforSet(stu); 
    }

}

输出结果:

课程号:1 课程名:数据结构
课程号:2 课程名:C语言
课程号:5 课程名:大学英语
课程号:6 课程名:大学物理
课程号:3 课程名:数据库
课程号:4 课程名:计算机网络
欢迎张三同学选课
请输入ID:
1
请输入ID:
2
请输入ID:
3
选择了课程:2 C语言
选择了课程:1 数据结构
选择了课程:3 数据库