Collection集合的常见使用(Java)

一、Collection本身是接口,不能被实例化,被实现后,可实例化使用

(1)Collection的常用子接口的实现类:

1、List体系:ArrayList、Vector、LinkedList

2、Set体系:HashSet、TreeSet

二:Collection常用方法:

判断功能:

boolean contain(Object o)判断集合中是否包含某元素

boolean containsAll(Collection c)判断集合中是否包含某集合c的所有元素

boolean isEmpty()判断集合是否为空

添加元素:

boolean add(object obj)添加一个元素

boolean addAll(Collection c)将集合c的全部元素添加到原集合

添加功能的返回值为布尔值,如果成功将返回true

删除功能:

void chear()移除所有元素

boolean remove(Object o)移除一个元素

boolean removeAll(Collection c)移除一个集合的元素,其效果为移除原集合中与c中相同的元素

int size()返回元素的个数

Iterator迭代器使用案例:

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

public class MyIterator {
    public static void main(String[] args) {
        Collection c = new ArrayList();
        c.add(1);
        c.add(2);
        c.add(3);
        c.add(4);

        Iterator iterator = c.iterator();
        /*Iterator iterator()方法:获取集合对应的迭代器*/

        while (iterator.hasNext()){
            /*boolean hasNext()方法:判断当前位置是否有元素*/

            Integer integer = (Integer) iterator.next();
            /*Object next()方法:获取当前位置的元素,并移动到下一个位置*/

            System.out.println(integer);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值