集合---Collection

一.集合的概述

  1. Collection:java是一种面向对象的语言,面向对象对事物的描述是通过对象体现出来的,要存储对象,要使用容器存储
    先阶段学习的容器有:数组和字符串缓冲区
    数组:长度固定
    字符串缓冲区:始终返回一个字符串,并不是想要的结果
    因此:java提供了集合:Collection
  2. 集合和数组的区别
    • 长度的区别:
        数组:长度固定
        集合:可变
    • 存储数据类型的区别:
        数组:可以存储基本数据类型和引用数据类型
        集合:只能存储引用数据类型
    • 存储类型元素的区别:
        数组:存储同一种数据类型的元素
        集合:可以存储多种数据类型的元素
  3. Collection:层次结构中的根接口.
    Collection:表示一组对象,这些对象称为Collection的元素.一些Collection允许有重复的元素,一些不允许.一些是有序的,一些是无须的
  4. Collection接口的子接口set和list
  5. 集合的体系结构
    这里写图片描述

二.Collection接口的常见方法

添加功能和转换功能

  1. boolean add(E e):添加功能
  2. boolean addAll(Collection c):添加一个集合中的元素
  3. Object[] toArray():把集合转成数组,可以实现集合的遍历
import java.util.ArrayList;
import java.util.Collection;
class Student{
    private String name;
    private int age;
    public Student() {
    }
    public Student(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }


}
public class Demo1 {
    public static void main(String[] args) {
        //创建集合对象
        Collection c = new ArrayList();

        //创建学生对象
        Student s1 = new Student("王一",21);
        Student s2 = new Student("王二",22);
        Student s3 = new Student("王三",23);
        Student s4 = new Student("王四",24);

        //将对象添加到集合中
        c.add(s1);
        c.add(s2);
        c.add(s3);

        Collection c2 = new ArrayList();
        c2.add(s4);
        c2.add(s3);

        //添加一个集合中的元素
        c.addAll(c2);

        Object[] obj = c.toArray();

        //遍历输出
        for(int x = 0 ; x < obj.length ; x ++){
            Student s = (Student)obj[x];
            System.out.println(s.getName()+"---"+s.getAge());
        }
        System.out.println("--------------");   
    }
}

结果:
王一---21
王二---22
王三---23
王四---24
王三---23
--------------

删除功能

  1. boolean remove(Object o):删除指定的元素
  2. boolean removeAll(Collection c):删除一个集合中的元素(思考:删除该集合中的一个元素算是删除呢还是删除所有算是删除———删除一个算是删除)
  3. void clear():删除一个集合中的所有元素
import java.util.ArrayList;
import java.util.Collection;

class Student{
    private String name;
    private int age;
    public Student() {
    }
    public Student(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }


}
public class Demo1 {
    public static void main(String[] args) {
        //创建集合对象
        Collection c = new ArrayList();

        //创建学生对象
        Student s1 = new Student("王一",21);
        Student s2 = new Student("王二",22);
        Student s3 = new Student("王三",23);
        Student s4 = new Student("王四",24);

        //将对象添加到集合中
        c.add(s1);
        c.add(s2);

        //删除指定的元素
        c.remove(s2);

        Collection c2 = new ArrayList();
        c2.add(s1);
        c2.add(s2);
        c2.add(s3);
        c2.add(s4);


        //删除一个集合中的元素
        //c1中只有一个元素,删除这一个元素就算删除
        c.removeAll(c2);

        Object[] obj = c.toArray();

        //遍历输出
        for(int x = 0 ; x < obj.length ; x ++){
            Student s = (Student)obj[x];
            System.out.println(s.getName()+"---"+s.getAge());
        }
        System.out.println("--------------");   

        Object[] obj2 = c2.toArray();

        //遍历输出
        for(int x = 0 ; x < obj2.length ; x ++){
            Student s = (Student)obj2[x];
            System.out.println(s.getName()+"---"+s.getAge());
        }
        System.out.println("--------------");   
    }
}

结果:
--------------
王一---21
王二---22
王三---23
王四---24
--------------

判断功能

  1. boolean contains(Object o):判断该集合中是否包含指定的元素
  2. boolean containsAll(Collection c):判断是否包含一个集合中的元素(包含一个算是包含,还是包含所有元素算术包含?—–该集合中包含c集合的所有元素才算包含)
  3. boolean isEmpty():判断集合是否为空
  4. boolean retainAll(Collection c):对一个集合取交集(A集合对B集合取交集.交集的元素要去A集合中,返回值类型表达的意思是:A集合的元素是否发生变化)
import java.util.ArrayList;
import java.util.Collection;

class Student1{
    private String name;
    private int age;
    public Student1() {
    }
    public Student1(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }


}
public class Demo2{
    public static void main(String[] args) {
        //创建集合对象
        Collection c = new ArrayList();

        //创建学生对象
        Student1 s1 = new Student1("王一",21);
        Student1 s2 = new Student1("王二",22);
        Student1 s3 = new Student1("王三",23);
        Student1 s4 = new Student1("王四",24);

        //将对象添加到集合中
        c.add(s1);
        c.add(s2);
        c.add(s3);


        //指定的元素
        System.out.println(c.contains(s1));

        Collection c2 = new ArrayList();
        c2.add(s3);
        c2.add(s4);


        //一个集合中的元素
        //包含c2中的所有元素才算包含
        System.out.println(c.containsAll(c2));
    }
}

结果:
true
false

长度功能

  1. int size():获取长度
import java.util.ArrayList;
import java.util.Collection;

class Student3{
    private String name;
    private int age;
    public Student3() {
    }
    public Student3(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }


}
public class Demo3{
    public static void main(String[] args) {
        //创建集合对象
        Collection c = new ArrayList();

        //创建学生对象
        Student3 s1 = new Student3("王一",21);
        Student3 s2 = new Student3("王二",22);
        Student3 s3 = new Student3("王三",23);
        Student3 s4 = new Student3("王四",24);

        c.add(s1);
        c.add(s2);
        c.add(s3);
        c.add(s4);

        //求集合的长度
        System.out.println(c.size());
    }
}

结果:
4

三.Iterator接口

  1. Iterator iterator():表示对集合中的元素进行迭代(遍历)
  2. Object next():返回迭代的下一个元素(获取元素)
    it.next()不要多次使用!只使用一次即可(自定义引用类型),多次使用会创建多个对象
  3. boolean hasNext():如果仍有元素可以迭代,则返回 true
    学生类:
public class Student{
    private String name;
    private int age;
    public Student() {
    }
    public Student(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}

测试类:

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class Demo1 {
    public static void main(String[] args) {
        //创建集合对象
        Collection c = new ArrayList();

        //创建学生对象
        Student s1 = new Student("王一",21);
        Student s2 = new Student("王二",22);
        Student s3 = new Student("王三",23);
        Student s4 = new Student("王四",24);

        //将学生对象加入集合中
        c.add(s1);
        c.add(s2);
        c.add(s3);
        c.add(s4);

        //迭代器遍历

        //获取迭代器对象,多态
        Iterator i = c.iterator();
        while(i.hasNext()){
            //创建了一个对象s
            Student s = (Student)i.next();
            System.out.println(s.getName()+"---"+s.getAge());
        }
    }
}

结果:
王一---21
王二---22
王三---23
王四---24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值