Java集合的概念与Collection接口+代码总结

19 篇文章 0 订阅

什么是集合

  • 概念:对象的容器,定义了对多个对象进行操作的常用方法。可实现数组的功能。
  • 和数组区别:
    • (1)数组长度固定,集合长度不固定
    • (2)数组可以存储基本类型和引用类型,集合只能储存引用类型
  • 位置:java.util.*;

Collection接口

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QfbqPded-1609935464397)(D:\学习\tupian\集合框架\Collection体系集合\Collection体系集合.PNG)]

  • Collection:该体系结构的根为接口,代表一组对象,称为“集合”。
  • List接口的特点:有序、有下标、元素可重复。
  • Set接口的特点:无序、无下标、元素不能重复。

Collection父接口

  • 特点:代表一组任意类型的对象,无序、无下标、不能重复。。

  • 方法:

    • boolean add(Object obj) 添加一个对象。
    • boolean addAll(Collection c) 将一个集合中的所有对象添加到此集合中。
    • void clear() 清空此集合中的所有对象。
    • boolean contains(Object o) 检查此集合中是否包含o对象。
    • boolean equals(Object o) 比较此集合是否与指定对象想等。
    • boolean isEmpty() 判断此集合是否为空。
    • boolean remove(Object o) 在此集合中移除o对象
    • int size() 返回此集合中的元素个数。
    • Object[] toArray() 将此集合转换成数组。
  • 实例:

    public class Collectiontest {
        public static void main(String[] args) {
    
            Collection collection = new ArrayList();
            // 添加集合
            System.out.println("----------添加集合-----------");
            collection.add("苹果");
            collection.add("西瓜");
            collection.add("榴莲");
            System.out.println("元素个数:"+collection.size());
            System.out.println(collection.toString());
            // 删除元素
            System.out.println("----------删除元素-----------");
            collection.remove("榴莲");
            System.out.println("元素个数:"+collection.size());
            System.out.println(collection.toString());
            // 遍历元素
            // 使用增强for
            System.out.println("----------使用增强for遍历-----------");
            for (Object o:collection) {
                System.out.println(o);
            }
            // 使用迭代器
            // hasNext();有没有下一个元素
            // next();获取下一个元素
            // remove();删除当前元素
            System.out.println("----------使用迭代器遍历-----------");
            Iterator it = collection.iterator();
            while(it.hasNext()){
                String s = (String)it.next();
                System.out.println(s);
                // 不能使用collection删除方法
                // collection.remove(s);
                it.remove();// 在while里全删,在外删最后一位
            }
            System.out.println(collection.toString());
    
            // 判断
            System.out.println("----------判断-----------");
            System.out.println("判断集合里是否有西瓜:"+collection.contains("西瓜"));
            System.out.println("判断集合是否为空:"+collection.isEmpty());
        }
    }
    

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-41nZeN5i-1609935464403)(D:\学习\tupian\集合框架\Collection体系集合\coll.png)]

  • 实例2:

    public class Student {
        private String name;
        private int 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 Student(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        public Student() {
        }
    
        // 重写toString方法,让我们看的清楚
        @Override
        public String toString() {
            return "Student:[name="+name+",age="+age+"]";
        }
    }
    
    public class Demo2{
        public static void main(String[] args){
            // 新建Collection对象
            Collection collection = new ArrayList();
            Student s1 = new Student("张三",20);
            Student s2 = new Student("张无忌",18);
            Student s3 = new Student("王二",22);
            // 1 添加数据
            collection.add(s1);
            collection.add(s2);
            collection.add(s3);
            System.out.println("元素个数:"+collection.size());
            System.out.println(collection.toString());
            // 2 删除
            // collection.remove(s1);
            // collection.remove(new Student("王二", 22));
            // collection.clear();
            // System.out.println("删除之后:"+collection.size());
            // 3 遍历
            // 3.1 增强for
            System.out.println("-----增强for-----");
            for(Object object:collection){
                Student s = (Student)object;
                System.out.println(s.toString());
            }
            // 3.2迭代器:hasNext() next(); remove(); 迭代器过程中不能
        	Iterator it = collection.iterator();
            while(it.hasNext()){
                Student s = (Student)is.next();
                System.out.println(s.toString());
            }
            // 4 判断
            System.out.println(collection.contains(s1));
            System.out.println(collection.isEmpty());
        }
    }
    

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GlPuuTPF-1609935464405)(D:\学习\tupian\集合框架\Collection体系集合\ec.png)]

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值