zoukankan      html  css  js  c++  java
  • Collection集合

    Collection集合

    集合:集合是Java中提供的一种容器,可以用来存储多个数据。

    集合与数组都是容器,它们的区别是什么?

    1、数组的长度是固定的,集合的长度是可变的。

    2、数组中存储的是同一类型的元素,可以存储基本数据类型值。集合存储的都是对象。而且对象的类型可以不同。在开发中,一般当对象多的时候,使用集合进行存储。

    集合框架

    Collection常用方法

      • booleanadd(E e) 确保此集合包含指定的元素(可选操作)。
        boolean addAll(Collection c) 将指定集合中的所有元素添加到此集合(可选操作)。
        void clear() 从此集合中删除所有元素(可选操作)。
        boolean contains(Object o) 如果此集合包含指定的元素,则返回 true
        boolean containsAll(Collection c) 如果此集合包含指定 集合中的所有元素,则返回true。
        boolean equals(Object o) 将指定的对象与此集合进行比较以获得相等性。
        int hashCode() 返回此集合的哈希码值。
        boolean isEmpty() 如果此集合不包含元素,则返回 true
        Iterator iterator() 返回此集合中的元素的迭代器。
        default Stream parallelStream() 返回可能并行的 Stream与此集合作为其来源。
        boolean remove(Object o) 从该集合中删除指定元素的单个实例(如果存在)(可选操作)。
        boolean removeAll(Collection c) 删除指定集合中包含的所有此集合的元素(可选操作)。
        default boolean removeIf(Predicate filter) 删除满足给定谓词的此集合的所有元素。
        boolean retainAll(Collection c) 仅保留此集合中包含在指定集合中的元素(可选操作)。
        int size() 返回此集合中的元素数。
        default Spliterator spliterator() 创建一个Spliterator在这个集合中的元素。
        default Stream stream() 返回以此集合作为源的顺序 Stream
        Object[] toArray() 返回一个包含此集合中所有元素的数组。
        T[] toArray(T[] a) 返回包含此集合中所有元素的数组; 返回的数组的运行时类型是指定数组的运行时类型。
    public class MyCollection {
       public static void main(String[] args) {
           Collection<String> obj = new ArrayList<String>();
           obj.add("111");
           obj.add("222");
           obj.add("333");
           obj.add("444");
           System.out.println(obj);
           boolean remove = obj.remove("444");
           System.out.println(remove);
           System.out.println(obj);

           boolean contains = obj.contains("222");
           System.out.println(contains);

           int size = obj.size();
           System.out.println(size);

           Object[] array = obj.toArray();
           for (int i = 0; i < array.length; i++) {
               System.out.println(array[i]);
          }

           obj.clear();
           System.out.println(obj);

           boolean empty = obj.isEmpty();
           System.out.println(empty);

      }
    }

     

  • 相关阅读:
    运行自动安装apk代码,报错: Original error: Could not find aapt Please set the ANDROID_HOME environment variable with the Android SDK root directory path.
    已安装Appium-Python-Client,但appium无法导入WebDriver
    打开uiautomatorviewer.bat闪退
    常见的python面试题【杭州多测师】【python笔试题】
    支付功能怎么测试?
    自动化运维工具——ansible详解
    服务端编程——异常+校验器+环境变量
    服务端编程——初始koa
    用postman发送请求,在koa中获取的请求body为undefined
    jQuery入口函数测试
  • 原文地址:https://www.cnblogs.com/lxy522/p/12815373.html
Copyright © 2011-2022 走看看