zoukankan      html  css  js  c++  java
  • Java Collection集合概述及其常用方法

    Collection集合概述

    • Java数组的长度是固定的,为了使程序能够方便地存储和操作数目不固定的一组数据,JDK类库提供了Java集合
    • 与数组不同的是,集合中不能存放基本类型数据,而只能存放对象的引用。
    • 数组只能存储同种数据类型的元素 ,集合可以存储不同类型的元素

    集合框架的介绍

    Collection集合的常用功能

    java.utiL.Collection接口

    • 所有单列集合的最顶层的接口,里边定义了所有单列集合共性的方法

    • 任意的单列集合都可以使用Collection接口中的方法

    Collection接口的共性方法

    • public boolean add(E e)          把给定的对象添加到当前集合中。
    • public void clear()              清空集合中所有的元素。
    • public boolean remove(E e)       把给定的对象在当前集合中册除。
    • public boolean contains(E e)     判断当前集合中是否包合给定的对象。
    • public boolean isEmpty()         判断当前集合是否为空。
    • public int size()                返回集合中元素的个数。
    • public Object[] toArray()        把集合中的元素,存储到数组中。

    add()方法

    作用:把给定的对象添加到当前集合中。

    import java.util.Collection;
    import java.util.ArrayList;
    
    public class DemoCollectionAdd {
        public static void main(String[] args) {
            // 使用多态,创建一个ArrayList对象
            Collection<String> collection = new ArrayList<>();
    
            // 输出该集合的内容是为空的(其中它重写了toString方法)
            System.out.println("没有进行任何操作的ArrayList集合对象:" + collection);
    
            // 往ArrayList集合中添加元素,返回值只一个boolean值,一般不用接收这个返回值
            boolean addReturn = collection.add("LeeHua");
            System.out.println("往集合中添加一个元素后的返回值:" + addReturn);
            System.out.println("使用add方法往集合里面添加了元素后:" + collection);
        }
    }
    输出结果:
    没有进行任何操作的ArrayList集合对象:[]
    往集合中添加一个元素后的返回值:true
    使用add方法往集合里面添加了元素后:[LeeHua]

    remove方法

    作用:把给定的对象在当前集合中册除。

    import java.util.Collection;
    import java.util.ArrayList;
    
    public class DemoCollectionRemove {
        public static void main(String[] args) {
            // 使用多态,创建一个ArrayList对象
            Collection<String> collection = new ArrayList<>();
            System.out.println("往集合中添加元素前的集合是:" + collection);
    
            // 往集合中添加元素
            collection.add("一号");
            collection.add("二号");
            collection.add("三号");
            collection.add("四号");
            System.out.println("往集合中添加元素后的集合是:" + collection);
    
            // 使用remove方法,把给定的对象在当前集合中册除
            // 如果要删除的元素存在该集合,那么就返回true
            // 否则返回false
            boolean removeReturn1 = collection.remove("一号");
            System.out.println("删除元素"一号"的返回值:" + removeReturn1);
            System.out.println("删除元素"一号"后的集合是:" + collection);
    
            boolean removeReturn2 = collection.remove("十号");
            System.out.println("删除元素"十号"的返回值:" + removeReturn2);
            System.out.println("删除元素"十号"后的集合是:" + collection);
        }
    }
    输出结果:
    往集合中添加元素前的集合是:[]
    往集合中添加元素后的集合是:[一号, 二号, 三号, 四号]
    删除元素"一号"的返回值:true
    删除元素"一号"后的集合是:[二号, 三号, 四号]
    删除元素"十号"的返回值:false
    删除元素"十号"后的集合是:[二号, 三号, 四号]

    contains方法

    作用:判断当前集合中是否包合给定的对象。

    import java.util.ArrayList;
    import java.util.Collection;
    
    public class DemoCollectionContains {
        public static void main(String[] args) {
            Collection<String> collection = new ArrayList<>();
    
            // 往集合中添加元素
            collection.add("对象1");
            collection.add("对象2");
            collection.add("对象3");
            collection.add("对象4");
            System.out.println("集合:" + collection);
    
            // 使用contains方法,判断当前集合中是否包合给定的对象
            // 如果包合给定的对象,那么就返回true
            // 否则返回false
            boolean containsReturn1 = collection.constains("对象100");
            System.out.println("是否包含"对象100":" + containsReturn1);
    
            boolean containsReturn2 = collection.constains("对象1");
            System.out.println("是否包含"对象1":" + containsReturn2);
        }
    }
    集合:[对象1, 对象2, 对象3, 对象4]
    是否包含"对象100":false
    是否包含"对象1":true

    isEmpty方法

    作用:判断当前集合是否为空。

    import java.util.ArrayList;
    import java.util.Collection;
    
    public class DemoCollectionIsEmpty {
        public static void main(String[] args) {
            // 使用多态,创建一个ArrayList对象
            Collection<String> collection = new ArrayList<>();
    
            // 判断集合是否为空
            boolean isEmptyReturn1 = collection.isEmpty();
            System.out.println("集合是否为空:" + isEmptyReturn1);
    
            // 向集合里面添加元素
            collection.add("一号元素");
            // 判断集合是否为空
            boolean isEmptyReturn2 = collection.isEmpty();
            System.out.println("集合是否为空:" + isEmptyReturn2);
        }
    }
    输出结果:
    集合是否为空:true
    集合是否为空:false

    size方法

    作用:返回集合中元素的个数。

    import java.util.ArrayList;
    import java.util.Collection;
    
    public class DemoCollectionSize {
        public static void main(String[] args) {
            // 使用多态,创建一个ArrayList对象
            Collection<String> collection = new ArrayList<>();
    
            // 使用size方法,查看集合中的元素个数
            int collectionSize1 = collection.size();
            System.out.println("collectionSize1 = " + collectionSize1);
    
            // 往集合中添加元素
            collection.add("一号元素");
            collection.add("二号元素");
            collection.add("三号元素");
            collection.add("四号元素");
            collection.add("五号元素");
    
            // 使用size方法,再次查看集合中的元素个数
            int collectionSize2 = collection.size();
            System.out.println("collectionSize2 = " + collectionSize2);
        }
    }
    输出结果:
    collectionSize1 = 0
    collectionSize2 = 5

    toArray方法

    作用:把集合中的元素,存储到数组中。

    import java.util.ArrayList;
    import java.util.Collection;
    
    /**
     * @Author: YiHua Lee
     * @Version: 1.8.0_201       Java SE 8
     * @Application: IntelliJ IDEA
     * @CreateTime: 2020/1/12 14:08
     * @Description:
     */
    public class DemoCollectionToArray {
        public static void main(String[] args) {
            // 使用多态,创建一个ArrayList对象
            Collection<String> collection = new ArrayList<>();
    
            // 往集合中添加元素
            collection.add("一号元素");
            collection.add("二号元素");
            collection.add("三号元素");
            collection.add("四号元素");
            collection.add("五号元素");
    
            // 使用toArray方法,把集合中的元素,存储到数组中。
            Object[] collectionToArray = collection.toArray();
    
            // 遍历输出
            for (int i = 0; i < collectionToArray.length; i++) {
                System.out.println(collectionToArray[i]);
            }
        }
    }
    输出结果:
    一号元素
    二号元素
    三号元素
    四号元素
    五号元素

    clear方法

    作用:清空集合中的所用元素

    import java.util.ArrayList;
    import java.util.Collection;
    
    public class DemoCollectionClear {
        public static void main(String[] args) {
            // 使用多态,创建一个ArrayList对象
            Collection<String> collection = new ArrayList<>();
    
            // 往集合中添加元素
            collection.add("一号元素");
            collection.add("二号元素");
            collection.add("三号元素");
            collection.add("四号元素");
            collection.add("五号元素");
            System.out.println("清空集合元素之前:" + collection);
    
            // 使用clear方法,清空集合中的所用元素
            collection.clear();
            System.out.println("清空集合元素之后:" + collection);
        }
    }
    输出结果:
    清空集合元素之前:[一号元素, 二号元素, 三号元素, 四号元素, 五号元素]
    清空集合元素之后:[]
  • 相关阅读:
    swift 获取iphone设备型号
    如何判断静态库是否支持64位
    MAC登录界面多了一个其他账户
    swift4 UIScrollView滑动手势与UIPageViewController冲突解决办法
    swift 保留两位小数以及前面不0
    本地通知
    swift3 UIColor扩展
    swift3 控件创建
    数据库--数据库事务
    数据库---触发器trigger
  • 原文地址:https://www.cnblogs.com/liyihua/p/12182562.html
Copyright © 2011-2022 走看看