Collection集合汇总

Collectioin(java)

Collection简介

打开帮助文档
java.utill //使用时需要导包
Interface Collection

集合层次结构中的根界面 。 集合表示一组被称为其元素的对象。 一些集合允许重复元素,而其他集合不允许。 有些被命令和其他无序。 JDK不提供此接口的任何直接实现:它提供了更具体的子接口的实现,如Set和List 。 该界面通常用于传递集合,并在需要最大的通用性的情况下对其进行操作

创建collection集合的对象

创建collection集合的对象
通常以多态的方式
用Arraylist类 Implements Collection

import java.util.ArrayList;
import java.util.Collection;
public class Collection01 {
    public static void main(String[] args) {
        Collection<String> c = new ArrayList<>();//多态的形式
        /*
        boolean add(E e)确保此集合包含指定的元素(可选操作)。
        如果此集合由于调用而更改,则返回true 。 (如果此集合不允许重复,并且已包含指定的元素,则返回false。 )
         */
        c.add("hello");
        c.add("world");
        c.add("java");
        System.out.println(c);//[hello, world, java],说明ArrayList<>重写了toString方法

    }
}
//运行得到  [hello, world, java]

Collection集合 的常用方法

import java.util.ArrayList;
import java.util.Collection;
public class Collection02 {
    public static void main(String[] args) {
        Collection< String> c = new ArrayList<>();

        //boolean add(E e):添加元素
//        System.out.println(c.add("hello"));
//        System.out.println(c.add("world"));
//        System.out.println(c.add("java"));
        /*
             public boolean add(E e) {
            ensureCapacityInternal(size + 1);  // Increments modCount!!
            elementData[size++] = e;
//            return true;//总是返回true,故直接使用
            }
         */
        c.add("hello");
        c.add("world");
        c.add("java");

        //boolean remove(Object o):从集合中移除指定的元素
//        System.out.println(c.remove("java"));//true
//        System.out.println(c.remove("javaee"));//false

        //void clear():清除集合中的元素
//        c.clear();

        //boolean contains(Object o):判断集合中是否存在指定的元素
//        System.out.println(c.contains("hello"));//true
//        System.out.println(c.contains("Hello"));//false

        //boolean isEmpty():判断集合是否为空
//        System.out.println(c.isEmpty());

        //int size():集合的长度,也就是集合中元素的个数
//        System.out.println(c.size());

        //输出集合对象
        System.out.println(c);
    }
}

Collection集合的遍历

在此介绍一个集合专用的遍历方式:迭代器
Iterator:迭代器,集合的专用遍历方式
Iterator iterator():返回此集合元素的迭代器,通过集合的iterator()方法得到
迭代器是通过集合的iterator()方法得到的,所以我们说他是依赖集合而存在的

Iterator中的常用方法:
E next():返回迭代中的下一个元素
boolean hasNext():如果迭代具有更多元素,则返回true

下面是代码实现

public class Collection03 {
    public static void main(String[] args) {
        Collection<String> c = new ArrayList<>();
        c.add("hello");
        c.add("world");
        c.add("java");

        // Iterator<E>iterator():返回此集合元素的迭代器,通过集合的iterator()方法得到
        Iterator<String> itr = c.iterator();//返回的是Iterator<E>的implements类Itr的对象
        /*这是部分源码
            public Iterator<E> iterator() {
                return new Itr();
            }
            private class Itr implements Iterator<E> {
            ......
            }
         */
        // E next():返回迭代中的下一个元素
//        System.out.println(itr.next());
//        System.out.println(itr.next());
//        System.out.println(itr.next());
//        System.out.println(itr.next());//NoSuchElementException表示被请求的元素不存在。
        while(itr.hasNext()){
//            System.out.println(itr.next());
            String s = itr.next();//我们可能会对s这个对象再次进行操作
            System.out.println(s);
        }
    }
}

以上就是Collection集合的简易介绍
下面是框架图
image

posted @ 2022-05-02 22:01  HFUUwzy  阅读(56)  评论(0编辑  收藏  举报