黑马程序员全套Java教程_Java基础教程_集合进阶之Collection(二十四)

本文详细介绍了Java集合框架中的Collection接口,包括其特点、体系结构、常用方法、遍历方式以及实际应用。通过实例展示了如何创建Collection集合,添加、删除元素,以及使用迭代器进行遍历。同时,提供了存储和遍历学生对象的案例,帮助读者深入理解Collection接口的使用。
摘要由CSDN通过智能技术生成

1.1 集合知识回顾

  • 集合类的特点:提供一种存储空间可变的存储模型,存储的数据容量可以随时发生改变

1.2 集合类体系结构

  • 我们知道集合最主要的作用是用来存取数据的。
  • 集合存储数据:
    (1)单列数据通过Collection集合存储,Collection集合也被称为单列集合;双列数据通过Map集合实现,Map集合也被称为双列集合。
    在这里插入图片描述
    (2)单列集合中数据存储:List集合存储元素是可重复的,Set集合存储元素不可以重复。
    在这里插入图片描述
    (3)但是这些集合都只是接口,并不能直接创建对象并使用,所以下面还有具体的实现类。
    在这里插入图片描述

1.3 Collection集合概述和使用

  • Collection集合概述:
    (1)是单例集合的顶层接口,它表示一组对象,这些对象也称为Collection的元素;
    (2)JDK不提供此接口的任何直接实现,他提供更具体的子接口(如Set和List)实现。
  • 创建Collection集合的对象:
    (1)多态的方式;
    (2)具体的实现类ArrayList。
    public static void main(String[] args) {
        //创建Collection集合对象
        Collection<String> c = new ArrayList<>();

        //添加元素:public boolean add(E e)
        c.add("hello");
        c.add("good");
        c.add("guy");

        //输出集合对象
        System.out.println(c);//[hello, good, guy]
    }

1.4 Collection集合的常用方法

方法名说明
boolean add(E e)添加元素
boolean remove(Object o)从集合中移除指定的元素
void clear()清空集合中的元素
boolean contains(Object o)判断集合中是否存在指定的元素
boolean isEmpty()判断结合是否为空
int size()集合的长度,也就是集合中元素的个数
public class CollectionDemo {
    public static void main(String[] args) {
        //创建Collection集合对象
        Collection<String> c = new ArrayList<>();

        //添加元素:public boolean add(E e)
        c.add("hello");
        c.add("good");
        c.add("guy");
        c.add("hello");
        System.out.println(c);//[hello, good, guy]

        c.remove("hello");
        System.out.println(c);//[good, guy, hello],说明remove只会移除一个指定的元素
        System.out.println(c.size());//3

        c.clear();
        System.out.println(c);//[]
        System.out.println(c.isEmpty());//true
        System.out.println(c.size());//0

        c.add("hello");
        c.add("good");
        c.add("guy");
        System.out.println(c.size());//3

        System.out.println(c.contains("guy"));//true

        System.out.println(c.isEmpty());//false

        System.out.println(c.size());//3
    }

1.5 Collection结合的遍历

  • Iterator:迭代器,集合的专用遍历方式。
    (1)Iterator iterator():返回此集合中元素的迭代器,通过集合的Iterator()方法得到;
    (2)迭代器是通过集合的Iterator方法得到的,所以我们说它是依赖于集合而存在的(ArrayList的内部类Itr实现了Iterator接口)。
  • Iterator中的常用方法:
    (1)E next():返回迭代器中的下一个元素。
    (2)boolean hasNext():如果迭代器具有更多的元素,则返回true。
    public static void main(String[] args) {
        Collection<String> c = new ArrayList<>();
        c.add("hello");
        c.add("good");
        c.add("guy");
        Iterator<String> it = c.iterator();//多态

        /*
        System.out.println(it.next());//hello
        System.out.println(it.next());//good
        System.out.println(it.next());//guy
        System.out.println(it.next());//运行时异常:NoSuchElementException
         */

        while (it.hasNext()){
            String s = it.next();
            System.out.println(s);
        }
    }

1.6 集合使用步骤

在这里插入图片描述

案例:Collection集合存储学生对象并遍历

  • 需求:创建一个学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合。
public class Student {
    private int age;
    private String name;

    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }

    public Student(){}

    public Student(int age, String name) {
        this.age = age;
        this.name = name;
    }
    .......................................................
}
public class CollectionDemo {
    public static void main(String[] args) {
        Collection<Student> c = new ArrayList<>();
        c.add(new Student(1,"aaa"));
        c.add(new Student(33,"bbb"));
        Student s = new Student();
        s.setAge(18);
        s.setName("hhh");
        c.add(s);
        Iterator<Student> it = c.iterator();
        while (it.hasNext()){
            System.out.println(it.next());
            //Student{age=1, name='aaa'}
            //Student{age=33, name='bbb'}
            //Student{age=18, name='hhh'}
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值