关于Collection集合

本文介绍了Java集合框架的基础知识,包括Collection接口及其子接口List和Set的特点。通过示例展示了如何使用ArrayList和LinkedList实现List接口,以及HashSet和TreeSet实现Set接口的基本操作,如添加、删除、查找和遍历元素。此外,还提供了一个综合案例,模拟斗地主发牌的过程,涉及到集合的创建、洗牌和分配等操作。

Collection:单列集合类的根接⼝,⽤于存储⼀系列符合某种规则的元素,它有两个重要的⼦接 ⼝,分别是 java.util.List 和 java.util.Set 。其中, List 的特点是元素有序、元素可重 复。 Set 的特点是元素⽆序,⽽且不可重复。 List 接⼝的主要实现类有 java.util.ArrayList 和 java.util.LinkedList , Set 接⼝的主要实现类有 java.util.HashSet 和 java.util.TreeSet 。

 

方法演示

package gather;
/*
Collection是所有单列集合的⽗接⼝,因此在Collection中定义了单列集合(List和Set)通⽤的⼀些⽅法,
这些⽅法可⽤于操作所有的单列集合。⽅法如下
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() : 把集合中的元素,存储到数组中。
 */



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

public class Colletion01 {
    public static void main(String[] args) {
        //使用集合
        //多态
        //泛型规定存储数据
        Collection<String> collection = new ArrayList<String>();

        //添加
       collection.add("皮辉辉");
       collection.add("龙儿子");
       collection.add("秀秀秀");
        System.out.println(collection);

        //boolean contains(E e) 判断0是否在集合中存在
        System.out.println("判断 皮辉辉 是否在集合中 "+collection.contains("皮辉辉"));

        //boolean remove/消除   删除在集合中的o元素
        System.out.println("删除龙儿子"+collection.remove("龙儿子"));
        System.out.println(collection);

        //size()  集合中有几个元素
        System.out.println("集合中有"+collection.size()+"个元素" );

        //object[] toArray()转换成一个object数组
        Object[] objects = collection.toArray();
        //遍历数组
        for (int i = 0; i < objects.length; i++) {
            System.out.println(objects[i]);
        }

        //void clear()  清空集合
        collection.clear();
        System.out.println("集合内容为空"+collection);

        //boolean isEMpty()   判断是否为空
        System.out.println(collection.isEmpty());
    }
} 

 

 

集合综合案例

package com.wenj;

import com.sun.javafx.image.BytePixelSetter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.FormatFlagsConversionMismatchException;

/*
    集合综合案例
    斗地主发牌
     */
public class Brand {
    public static void main(String[] args) {
        /*
        1:准备发牌操作
         */
        //创建盘盒,来存储牌面的
        ArrayList<String> pokerBox = new ArrayList<String>();
        //创建花色集合
        ArrayList<String> colcos = new ArrayList<String>();
        //创建数字集合
        ArrayList<String> numbers = new ArrayList<String>();

        //分别给花色 以及 数字集合添加元素
        colcos.add("♥");
        colcos.add("♦");
        colcos.add("♠");
        colcos.add("♣");
        for (int i = 2; i <= 10; i++) {
            numbers.add(i + " ");
        }
        numbers.add("J");
        numbers.add("Q");
        numbers.add("K");
        numbers.add("A");
        //创造牌,接拼牌操作
        //拿出每一个花色,然后跟每一个数字 进行结合 存储到牌盒中
        for (String colcor : colcos) {
            //color 每一个花色
            //遍历数组集合
            for (String number : numbers) {
                //结合
                String card = colcor + number;    //card  卡片
                //存储到牌盒中
                pokerBox.add(card);
            }
        }
        //大小王
        pokerBox.add("小王");
        pokerBox.add("大王");
        // System.out.println(pokerBox);
        //洗牌 将牌盒中的索引打乱
        //Ccollections类 工具类 都是静态方法
        //shuffer方法
        /*
         * static void shuffle(List<?> list)
         * 使⽤默认随机源对指定列表进⾏置换
         */

        //洗牌
        Collections.shuffle(pokerBox);
        //System.out.println(pokerBox);
        //发牌
        //创建  三个玩家集合  创建一个底牌集合
        ArrayList<String> Player1 = new ArrayList<String>();
        ArrayList<String> Player2 = new ArrayList<String>();
        ArrayList<String> Player3 = new ArrayList<String>();
        ArrayList<String> dipa = new ArrayList<String>();

        //遍历 牌盒  必须知道索引
        for (int i = 0; i < pokerBox.size(); i++) {
            //获取牌面
            String card = pokerBox.get(i);
            //留出三张牌 存到 底牌集合中
            if (i >= 51) {  //存到底牌集合中
                dipa.add(card);
            } else {
                //玩家1  %3 == 0
                if (i % 3 == 0) {
                    Player1.add(card);
                } else if (i % 3 == 1) {  //玩家二
                    Player2.add(card);
                } else {  //玩家三
                    Player3.add(card);
                }

            }
        }
        //牌
        System.out.println("1号:"+Player1);
        System.out.println("2号:"+Player2);
        System.out.println("3号:"+Player3);
        System.out.println("底牌:"+dipa);
    }
}
集合Collection)是 Java 中用于存储多个对象的容器。集合分为 Collection 和 Map 两种体系,这里主要介绍 Collection 体系 [^2]。 Collection 是一个接口,它是 List 和 Set 的父接口 [^1]。Colletion 作为集合的根接口,其子接口 List 和 Set 分别继承该根接口 [^3]。 - **List 集合及其实现类**:List 集合的实现类有 ArrayList 集合、LinkedList 集合以及 Vector 集合。这些实现类继承了 List 接口的方法,并且各自还有独特的方法。 - **Set 集合及其实现类**:Set 集合的实现类包括 HashSet 集合、LinkedHashSet 集合以及 TreeSet 集合。同样,这些实现类继承了 Set 接口的方法,也具备自身特有的方法 [^3]。 与数组相比,集合的优势明显。数组一旦初始化后,长度就固定了,存储数据对象无法动态扩展,并且对数组进行添加、修改、删除操作不太方便,同时数组可以存储重复元素。而集合能解决这些问题 [^2]。 另外,还有与 Collection 相关的 Collections 类,它是工具类,和 Collection 不同,Collection 是接口,是容器;而 Collections 是类,用于创建元素集合,它提供了排序、混淆等很多实用方法 [^1]。 ### 示例代码 以下是一个简单使用 Collection 集合的 Java 代码示例: ```java import java.util.ArrayList; import java.util.Collection; public class CollectionExample { public static void main(String[] args) { // 创建一个 Collection 对象,使用 ArrayList 实现 Collection<String> collection = new ArrayList<>(); // 添加元素 collection.add("apple"); collection.add("banana"); collection.add("cherry"); // 打印集合元素 for (String element : collection) { System.out.println(element); } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值