Java基础 - 集合

集合

概述

继承体系

  • Collection 单列集合
  • Map 双列集合

Collection接口

概述

单列集合的顶层接口,它表示一组对象,这些对象也被称为Collection的元素
JDK不提供此接口的任何直接实现,它提供更具体的子接口(如Set和List)实现

创建Collection集合的对象

  • 多态的方式
  • 具体的实现类ArrayList
加端端老师vx领取最新Java资料哦

在这里插入图片描述

Collection 集合常用方法

  • boolean add(E e)添加元素
  • boolean remove(Object o)从集合中移除指定的元素
  • boolean removeif(Object o)根据条件进行删除
    注意事项:
    1.removeif括号中可以使用lambda表达式
    2.removeif底层会遍历集合,得到集合中的每一个元素
    3.底层会把每一个元素放到lambda表达式中去判断,true就删除
  • void clear() 清空集合
  • boolean contains(Object o)判断集合中是否存在指定的元素
  • boolean isEmpty()判断集合是否为空
  • int size0集合的长度,也就是集合中元素的个数
public class CollectionDemo2 {
   
    public static void main(String[] args) {
   

        Collection<String> collection = new ArrayList<>();

        // - boolean add(E e)添加元素
        collection.add("aaa");
        collection.add("bbb");
        collection.add("ccc");

        System.out.println(collection); // [aaa, bbb, ccc]

        //- boolean remove(Object o)从集合中移除指定的元素
        boolean result = collection.remove("aaa");
        boolean result2 = collection.remove("ddd");
        System.out.println(result); // true
        System.out.println(result2); // false
        System.out.println(collection); // [bbb, ccc]

        //- boolean removeif(Object o) 根据条件进行删除
        // 注意事项,removeif括号中可以使用lambda表达式

        collection.removeIf((String s)->{
   
            return s.length() == 3; // 删除字符串长度为三的字符串
        });

        System.out.println(collection);// []

        collection.add("aaa");
        collection.add("bbb");
        collection.add("ccc");

        System.out.println(collection); // [aaa, bbb, ccc]

        //- void clear()清空集合
        collection.clear();

        System.out.println(collection); // []

        //- boolean contains(Object o)判断集合中是否存在指定的元素
        collection.add("aaa");

        System.out.println(collection.contains("aaa")); // true
        System.out.println(collection.contains("ddd")); // false

        //- boolean isEmpty()判断集合是否为空
        System.out.println(collection.isEmpty()); // false
        //- int size0集合的长度,也就是集合中元素的个数
        System.out.println(collection.size()); // 1

    }
}

Iterator

迭代器,集合专业遍历方式,是一个接口,我们无法直接使用,需要使用Iterator接口的实现类对象,获取实现类的方式比较特殊,Collection接口中有一个方法,叫Iterator(),这个方法返回的就是迭代器的实现类对象,Iterator iterator()返回再此 collection 的元素上进行的迭代器。

使用步骤

1.使用集合中的方法iterator()获取迭代器的实现类对象,使用Iterator接口接收(多态)
2.使用Iterator接口当中的方法hasNext判断下还有没有下一个元素
3.使用Iterator接口中的方法next取出集合的下一个元素

常用方法:

public e next():返回迭代的下一个元素,取出元素后将迭代器往后移动一个索引
public boolean hasNext():如果仍有元素可以迭代,则返回true
default void remove():从底层集合中删除迭代器返回的最后一个元素(指向谁删除谁).

public class CollectionDemo3 {
   
    public static void main(String[] args) {
   
        Collection<String> coll = new ArrayList<>();
        //添加元素:
        coll.add("串串星人");
        coll.add("吐槽星人");
        coll.add("汪星人");
        //1.使用集合中的方法iterator()获取迭代器的实现类对象,使用Iterator接口接收(多态)
        //注意:Iterator<E>接口也是由泛型的,迭代器的泛型跟着集合走,集合是什么泛型,迭代器就是什么泛型

        Iterator<String> it = coll.iterator();
        //没有元素,在取出元素会抛出NoSuchElementException没有元素异常
        while(it.hasNext()){
   
            String e = it.next();
            System.out.println(e);
        }

        while(it2.hasNext()){
   
            String e = it2.next();
            if("串串星人".equals(e)){
   
                    it.remove();
            }
        }
        System.out.println("--------------------------------");
        for(Iterator<String> it3 = coll.iterator();it3.hasNext();){
   
            String e = it3.next();
            System.out.println(e);
        }
    }
}

继承体系

List 可重复
Set 不可重复

List 接口

概述

  • 有序的集合(存储和取出元素顺序相同)
  • 允许存储重复元素。
  • 有索引,可以使用普通的for循环遍历
常用方法

public void add(int index, E element): 将指定的元素,添加到该集合中的指定位置上。
E remove(int index)࿱

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值