• Java学习之集合(Collection接口)


    集合类的由来:
      对象用于封装特有数据,对象多了需要存储,如果对象的个数不确定,就使用集合容器进行存储
    集合特点:
      1、用于存储对象的容器
      2、集合长度可变
      3、不可以存储基本数据类型
    集合体系的顶层Collection接口
    Collection接口
      |--List:有序(存入和取出的顺序一致),元素都是索引,元素可以重复
        |--Vector:内部是数组数据结构,是同步的。增删,查询都很慢(已几乎不用)
        |--ArrayList:内部是数组数据结构,是不同步的。替代Vector。查询速度快
        |--LinkedList:内部是链接列表数据结构,是不同步的。增删元素的速度很快。
      |--Set:元素不能重复,是无序
        Set接口中的方法和Collection一致
        |--HashSet:内部数据结构是哈希表,是不同步的。
          |--LinkedHashSet:内部数据结构是哈希表和链接列表,简单说就是有序的HashSet
        |--TreeSet:内部数据结构是二叉树,是不同步的。
    常见方法:
      1、添加
        boolean add(E e);
        boolean addAll(collection<? extends E> c);
      2、删除
        boolean remove(object o);
        boolean removeAll(collection<?> c);//将两个集合中的相同元素从调用removeAll的集合中删除
         void clear();//清空
      3、判断
        boolean contains(object o);
        boolean containsAll(collection<?> c);//是否全部包含c集合中的元素
        boolean isEmpty();//判断集合中是否有元素
      4、获取
         int size();
         iterator<E> iterator();//迭代器,取出元素的方式
     
      5、其他
        boolean retainAll(collection<?> c);取交集
        Object[] toArray();//将集合转成数组
     1 import java.util.ArrayList;
     2 import java.util.Collection;
     3 import java.util.Iterator;
     4 
     5 public class CollectionDemo {
     6 
     7     public static void main(String[] args) {
     8     Collection coll = new ArrayList();
     9     coll.add("abc1");
    10     coll.add("abc2");
    11     coll.add("abc3");
    12     coll.add("abc4");
    13     /*
    14      * 注释快捷键:ctrl+/ 单行注释 shift+ctrl+/ 多行注释 alt+shift+j 文档注释
    15      */
    16     /*
    17      * Iterator it=coll.iterator(); while(it.hasNext()) {
    18      * System.out.println(it.next()); }
    19      */
    20     // 用for循环释放资源
    21     for (Iterator it = coll.iterator(); it.hasNext();) {
    22         System.out.println(it.next());
    23     }
    24 
    25     Collection coll2 = new ArrayList();
    26     coll2.add("abc1");
    27     coll2.add("abc2");
    28     coll2.add("abc5");
    29 
    30     System.out.println("====================retainAll===============");
    31     System.out.println(coll.retainAll(coll2));
    32     for (Iterator it = coll.iterator(); it.hasNext();) {
    33         System.out.println(it.next());
    34     }
    35     coll.clear();
    36     coll.add("abc1");
    37     coll.add("abc2");
    38     coll.add("abc3");
    39     coll.add("abc4");
    40 
    41     Collection coll3 = new ArrayList();
    42     coll3.add("abc1");
    43     coll3.add("abc3");
    44     coll3.add("abc5");
    45 
    46     System.out.println("====================removeAll===============");
    47     System.out.println(coll.removeAll(coll3));
    48 
    49     for (Iterator it = coll.iterator(); it.hasNext();) {
    50         System.out.println(it.next());
    51     }
    52     }
    53 }

    结果:

  • 相关阅读:
    全局上下文(GO)
    重写数组拓展方法
    spring + spring mvc 使用 maven 编译出现异常
    shiro 配置注解后无权访问不进行页面跳转异常:org.apache.shiro.authz.UnauthorizedException: Subject does not have permission
    shiro 配置注解异常 java.lang.ClassNotFoundException: org.aspectj.util.PartialOrder$PartialComparable
    kaptcha 配置
    maven 安装 jar 包
    jsp 页面 javax.servlet.jsp.JspException cannot be resolved to a type 异常
    操作系统下载地址
    java file 常用操作
  • 原文地址:https://www.cnblogs.com/WarBlog/p/12097286.html
Copyright © 2020-2023  润新知