Java中的Collection集合

本文介绍了Java中的Collection和Map集合,包括它们的子类如List(如ArrayList和LinkedList)和Set(如HashSet),以及如何创建、操作和遍历这些集合。重点讲解了添加、移除、判断元素存在、清空等基本操作。
摘要由CSDN通过智能技术生成

关于集合的介绍

集合分为Collection集合和Map集合(Collection集合是单列集合和Map集合双列集合)Collection和Map都为接口

Collection集合又分为List集合(集合元素可重复)和Set集合(集合元素不可重复 )List和Set都为接口

List集合分为ArrayList和LinkedList(ArrayList和LinkedList都为实现类

Map集合分为HashMap集合(HashMap为实现类

Collection集合的创建

package dayhou40.day46;
​
import java.util.ArrayList;
import java.util.Collection;
​
public class Collectiontest {
    public static void main(String[] args) {
        Collection<String> collection=new ArrayList<>();
        collection.add("hello");
        collection.add("world");
        for (String s : collection) {
            System.out.print(s+" ");
        }
    }
}
​

创建Collection集合要使用多态因为Collection集合是接口没有具体的实现方法

Collection集合中的常用方法

  1. boolean add(E e) 添加元素

  2. boolean remove(Object o)从集合中移除指定元素

  3. void clean() 清除集合中的所有元素

  4. boolean contians(Object o)判断集合中是否存在指定元素

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

  6. int size()集合中的元素个数

package dayhou40.day46;
​
import java.util.ArrayList;
import java.util.Collection;
​
public class Collectiontest {
    public static void main(String[] args) {
        Collection<String> collection=new ArrayList<>();
        collection.add("hello");
        collection.add("world");
        collection.add("world");
        collection.add("hi");
        collection.add("好好学习");
        System.out.println(collection.isEmpty());
        System.out.println(collection.remove("world"));
        System.out.println(collection.contains("world"));
        System.out.println(collection.size());
        System.out.println(collection);
        collection.clear();
        System.out.println("===================");
        System.out.println(collection);
    }
}
​

Collection集合的遍历

package dayhou40.day46;
​
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
​
public class Collectiontest {
    public static void main(String[] args) {
        Collection<String> collection=new ArrayList<>();
        collection.add("hello");
        collection.add("world");
        collection.add("world");
        collection.add("hi");
        collection.add("好好学习");
        Iterator<String> it = collection.iterator();
        while (it.hasNext()){
            String next = it.next();
            System.out.println(next);
        }
    }
}
​

Collection集合用迭代器进行遍历

在Idea中快速书写迭代器的方法 集合对象名.iterator()然后再按住ALt+Enter自动生成

用迭代器里面的hasNext()判断集合中还有没有元素

再用next()获取集合中的下一个元素

List集合的特征

  1. 有序性:存储和取出的顺序一致

  2. 可重复性:存储的元素可以重复

List集合的特有方法

  1. void add(int index,E element)在此集合中的中的指定位置插入元素

  2. E remove(int index)删除指定索引处的位置,,并返回删除的元素

  3. E set(int index,E element)修改指定索引出的元素,并返回被修改的元素

  4. E get(int index)返回指定索引出的元素

这些方法是Collection集合中没的

  • 26
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值