Collection接口

简介: Collection接口

集合:对象的容器,定义了多个对象操作的常用方法,实现数组的功能;长度不固定;位于java.uitl.*包;

一:

1、Collection接口:

(1)List 有序集合,允许相同元素和null,有下标

LinkedList 非同步,允许相同元素和null,遍历效率低插入和删除效率高

ArrayList 非同步,允许相同元素和null,实现了动态大小的数组,遍历效率高,用的多

Vector 同步,允许相同元素和null,效率低

Stack 继承自Vetor,实现一个后进先出的堆栈

(2)Set 无序集合,不允许相同元素,最多有一个null元素,无下标

HashSet 无序集合,不允许相同元素,最多有一个null元素

TreeSet 有序集合,不允许相同元素,最多有一个null元素

2、Collection接口的功能:

(1)单个元素的的添加和删除

public boolean add(Object obj)//向collection中添加指定元素
public boolean remove(Object obj) //删除collection中的指定元素

(2)组元素的添加、删除

public boolean addAll(Collection col) //向collection中添加指定的一组对象
public boolean removeAll(Collection col)//删除collection中指定的一组对象//删除两个集合的交集
public boolean retainAll(Collection col)//删除仅在collection中而不在col中的对象//保留两个集合的交集,其他删除
public boolean clear()//清空collection中对象的个数

(3)查询

public boolean contains(Object obj) //查询collection中是否含指定的对象obj
public boolean isEmpty() //查询collection中是否存在对象
public int size()  //获取collection中对象的个数

Collection接口的代码:

package Fanxing;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/***
 * Collection接口
 * (1)添加元素
 * (2)删除元素
 * (3)遍历元素
 * (4)清空
 * @author AiY
 */
public class Demo1 {
    public static void main(String[] args) {
        //创建集合
        Collection collection=new ArrayList();
        //添加元素
        collection.add("苹果");
        collection.add("香蕉");
        collection.add("橘子");
        System.out.println("元素个数:"+collection.size());
        System.out.println(collection);
        //删除对象
        System.out.println("------删除元素后遍历--------");
        collection.remove("香蕉");
        System.out.println("删除之后元素为:"+collection);
        //遍历元素
        collection.add("西瓜");
        System.out.println("-----增强for遍历-----------");
            //增强for遍历,不显示下标
        for(Object object : collection){
            System.out.println(object);
        }
        System.out.println("-----迭代器遍历-------------");
            //使用迭代器遍历(主要遍历集合的一种方式)//hasNext()-有没有下一个元素;
            //next()-获取下一个元素//remove()-删除当前元素
        Iterator it =collection.iterator();
        while(it.hasNext()){
            String element=(String)it.next();//强制转换//element元素的意思
            System.out.println(element);
        }
        //清空
        System.out.println("-----清空后遍历-------");
        collection.clear();
        System.out.println("清空之后元素:"+collection);
    }
}

运行结果:

元素个数:3
[苹果, 香蕉, 橘子]
------删除元素后遍历--------
删除之后元素为:[苹果, 橘子]
-----增强for遍历-----------
苹果
橘子
西瓜
-----迭代器遍历-------------
苹果
橘子
西瓜
-----清空后遍历-------
清空之后元素:[]


相关文章
|
2月前
|
Java
Collection接口详解
Collection接口详解
|
7月前
|
安全 Java 程序员
|
13天前
List()接口
List()接口
|
7月前
|
存储 算法 Java
java集合框架------Collection接口,List接口与实现类,Set接口与实现类
java集合框架------Collection接口,List接口与实现类,Set接口与实现类
|
9月前
|
存储 算法 Java
Java Collection接口的子接口之Set接口及其Set接口的主要实现类HashSet,LinkedHashSet,TreeSet详解(一)
Java Collection接口的子接口之Set接口及其Set接口的主要实现类HashSet,LinkedHashSet,TreeSet详解
46 0
|
9月前
|
Java
Java Collection接口的子接口之Set接口及其Set接口的主要实现类HashSet,LinkedHashSet,TreeSet详解(二)
Java Collection接口的子接口之Set接口及其Set接口的主要实现类HashSet,LinkedHashSet,TreeSet详解
68 0
|
9月前
|
Java
Java集合框架Collection接口
Java集合框架是Java编程中的一个非常重要的部分,提供了一组用于处理数据集合的接口和类。其中Collection接口是Java集合框架的基础接口之一,定义了一些基本的集合操作,包括添加元素、删除元素、遍历集合等。在这里,我将为您详细介绍Java集合框架中的Collection接口。 Collection接口是Java集合框架中的基础接口,定义了一些基本的集合操作,包括添加元素、删除元素、遍历集合等。在Java中,Collection接口是一个顶层接口,它有两个主要的子接口:List和Set。其中,List是一个有序的集合,可以包含重复的元素;而Set是一个不重复的集合,元素是无序的。
43 0
|
11月前
|
API
迭代器 Collection以及List接口
迭代器 Collection以及List接口
48 0
|
存储
List接口
List接口
51 0
List接口