集合框架及背后的数据结构(部分)

本文详细介绍了Java集合框架,包括Collection、Set、List、Map等接口,以及它们的常用操作如添加元素、删除元素、获取元素数量等。同时展示了Map接口的方法,如put、get、containsKey等,帮助读者理解和应用Java集合框架。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

介绍

Java 集合框架 Java Collection Framework ,又被称为容器 container ,是定义在 java.util 包下的一组接口 interfaces 和其实现类 classes 。
其主要表现为将多个元素 element 置于一个单元中,用于对这些元素进行快速、便捷的存储 store 、检索retrieve 、管理 manipulate ,即平时我们俗称的增删查改 CRUD 。
例如,一副扑克牌(一组牌的集合)、一个邮箱(一组邮件的集合)、一个通讯录(一组姓名和电话的映射关系)等等。

类和接口总览
在这里插入图片描述

Java 集合框架的优点及作用

使用成熟的集合框架,有助于我们便捷、快速的写出高效、稳定的代码…
学习背后的数据结构知识,有助于我们理解各个集合的优缺点及使用场景…

接口 interfaces

基本关系说明
在这里插入图片描述

Collection: 用来存储管理一组对象 objects ,这些对象一般被成为元素 elements
Set : 元素不能重复,背后隐含着查找/搜索的语义
SortedSet : 一组有序的不能重复的元素
List : 线性结构
Queue : 队列
Deque : 双端队列
Map : 键值对 Key-Value-Pair ,背后隐含着查找/搜索的语义
SortedMap : 一组有序的键值对

Collection 常用方法

将元素 放入集合中

public static void main(String[] args) {
        Collection<String> collection = new ArrayList<>();
        collection.add("hello");
        collection.add("hello2");
        //collection.add(1);
        //尖括号当中 放的类型 一定要是 类类型 不能是简单的基本类型
        Collection<Integer> collection2 = new ArrayList<>();
        collection2.add(1);
        collection2.add(2);
        collection2.add(13);

        System.out.println(collection);
        System.out.println(collection2);
    }

在这里插入图片描述

删除集合中的所有元素

public static void main(String[] args) {
        Collection<String> collection = new ArrayList<>();
        collection.add("hello");
        collection.add("hello2");
        System.out.println(collection);
        collection.clear();
        System.out.println(collection);

        //尖括号当中 放的类型 一定要是 类类型 不能是简单的基本类型
        Collection<Integer> collection2 = new ArrayList<>();
        collection2.add(1);
        collection2.add(2);
        collection2.add(13);
        System.out.println(collection2);
        collection2.clear();
        System.out.println(collection2);
    }

在这里插入图片描述

返回一个装有所有集合中元素的数组

public static void main(String[] args) {
        Collection<String> collection = new ArrayList<>();
        collection.add("hello");
        collection.add("hello2");
        System.out.println(collection);
        Object[] objects = collection.toArray();
        System.out.println(Arrays.toString(objects));
    }

在这里插入图片描述

如果元素 e 出现在集合中,删除其中一个

 public static void main(String[] args) {
        Collection<String> collection = new ArrayList<>();
        collection.add("hello");
        collection.add("hello2");
        collection.remove("hello");
        System.out.println(collection);
    }

在这里插入图片描述

返回集合中的元素个数

public static void main(String[] args) {
        Collection<String> collection = new ArrayList<>();
        collection.add("hello");
        collection.add("hello2");
        collection.add("hello3");
        int ret = collection.size();
        System.out.println(ret);
    }

在这里插入图片描述

Map 常用方法

将指定的 k-v 放入 Map (V put(K key, V value))|| 根据指定的 k 查找对应的 v,没有找到用默认值代替 (V get(Object k))|| 判断是否包含 key (boolean containsKey(Object key))

public static void main(String[] args) {

        Map<String,String> map = new HashMap<>();
        map.put("国民女神","高圆圆");
        map.put("及时雨","宋江");

        String ret = map.getOrDefault("及时雨","博哥");
        System.out.println(ret);
        boolean flg = map.containsKey("国民女神2");
        System.out.println(flg);
        
    }

在这里插入图片描述

将所有键值对返回(Set<Map.Entry<K, V>> entrySet())|| 判断是否包含 key(boolean containsKey(Object key))|| 判断是否包含 value(boolean containsValue(Object value))

public static void main(String[] args) {
        Map<String,String> map = new HashMap<>();
        map.put("及时雨","宋江");
        map.put("国民女神","高圆圆");
        System.out.println(map);
        System.out.println("====================");

        Set<Map.Entry<String, String>> entrySet = map.entrySet();
        for( Map.Entry<String, String> entry : entrySet) {
            System.out.println("key: "+entry.getKey()+" value:"+entry.getValue());
        }

    }

在这里插入图片描述

public static void main(String[] args) {
        TreeMap<String,String> map2 = new TreeMap<>();
        map2.put("及时雨","宋江");
        map2.put("国民女神","高圆圆");
        System.out.println(map2);

        HashMap<String,String> map = new HashMap<>();
        map.put("及时雨","宋江");
        map.put("国民女神","高圆圆");
        System.out.println(map);
    }

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

粉色的志明

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值