面向对象程序设计之常用集合类

容器集合类,是后端的基础,本文以面向对象语言c#与java为例,了解常用的集合类。

c#

List<T>:

System.Collections.Generic.List<T> 是动态数组的泛型集合类。它提供了类型安全性和高效的元素访问。

cs 复制代码
List<string> list = new List<string>();
list.Add("apple"); // 添加元素
list.Insert(1, "pear"); // 在指定位置插入元素
list.Remove("apple"); // 根据元素值删除
list.RemoveAt(0); // 根据索引删除元素
list[0] = "cherry"; // 修改指定索引的元素值
int index = list.IndexOf("pear"); // 查找元素的索引

LinkedList<T>:

System.Collections.Generic.LinkedList<T> 是双向链表的泛型集合类。它支持在任意位置进行元素的插入和删除操作。

cs 复制代码
LinkedList<string> linkedList = new LinkedList<string>();
linkedList.AddLast("apple"); // 添加元素到末尾
linkedList.AddFirst("pear"); // 添加元素到开头
linkedList.RemoveFirst(); // 移除第一个元素
var node = linkedList.First;
linkedList.AddAfter(node, "cherry"); // 修改指定位置的元素值
bool b = linkedList.Contains("apple"); // 判断是否包含某元素

HashSet<T>:

System.Collections.Generic.HashSet<T> 是存储唯一元素的哈希集合类。它提供了快速的查找和插入操作。

cs 复制代码
HashSet<string> set = new HashSet<string>();
set.Add("apple"); // 添加元素
set.Remove("apple"); // 删除指定元素
bool exists = set.Contains("apple"); // 判断是否包含某元素

Dictionary<TKey, TValue>:

System.Collections.Generic.Dictionary<TKey, TValue> 是泛型键值对的哈希表集合类。它提供了快速的查找性能,用于存储唯一的键。

cs 复制代码
Dictionary<string, int> dict = new Dictionary<string, int>();
dict.Add("apple", 5); // 添加键值对
dict.Remove("apple"); // 根据键删除键值对
dict["apple"] = 10; // 更新指定键的值
bool exists = dict.ContainsKey("apple"); // 判断是否包含指定键

java

ArrayList:

java.util.ArrayList 是动态数组实现的集合类,可以根据需要自动增长大小。它允许存储任意类型的对象,并支持按索引访问。

java 复制代码
ArrayList<String> list = new ArrayList<>();
list.add("apple"); // 添加元素
list.add(1, "pear"); // 在指定位置插入元素
list.remove("apple"); // 根据元素值删除
list.remove(0); // 根据索引删除元素
list.set(0, "cherry"); // 修改指定索引的元素值
int index = list.indexOf("pear"); // 查找元素的索引

LinkedList:

java.util.LinkedList 是双向链表实现的集合类。它支持在任意位置进行元素的插入和删除操作,但随机访问效率较低。

java 复制代码
LinkedList<String> linkedList = new LinkedList<>();
linkedList.add("apple"); // 添加元素到末尾
linkedList.addFirst("pear"); // 添加元素到开头
linkedList.remove(); // 移除并返回第一个元素
linkedList.removeFirst(); // 移除第一个元素
linkedList.set(0, "cherry"); // 修改指定索引的元素值
boolean b = linkedList.contains("apple");

HashSet:

java.util.HashSet 是基于哈希表实现的集合类,用于存储唯一的元素。它不保证元素的顺序。

java 复制代码
HashSet<String> set = new HashSet<>();
set.add("apple"); // 添加元素
set.remove("apple"); // 删除指定元素
boolean exists = set.contains("apple"); // 判断是否包含某元素

HashMap:

java.util.HashMap 是基于哈希表实现的键值对集合类。它提供了快速的查找性能,不保证元素的顺序。

java 复制代码
HashMap<String, Integer> map = new HashMap<>();
map.put("apple", 5); // 添加键值对
map.remove("apple"); // 根据键删除键值对
map.put("apple", 10); // 更新指定键的值
int value = map.get("apple"); // 根据键获取值
相关推荐
Grey Zeng2 小时前
Java SE 25新增特性
java·jdk·jdk新特性·jdk25
追逐时光者3 小时前
精选 4 款基于 .NET 开源、功能强大的 Windows 系统优化工具
后端·.net
雨白3 小时前
Java 线程通信基础:interrupt、wait 和 notifyAll 详解
android·java
TF男孩3 小时前
ARQ:一款低成本的消息队列,实现每秒万级吞吐
后端·python·消息队列
AAA修煤气灶刘哥4 小时前
别让Redis「歪脖子」!一次搞定数据倾斜与请求倾斜的捉妖记
redis·分布式·后端
Lei活在当下5 小时前
【业务场景架构实战】4. 支付状态分层流转的设计和实现
架构·android jetpack·响应式设计
AAA修煤气灶刘哥5 小时前
后端人速藏!数据库PD建模避坑指南
数据库·后端·mysql
你的人类朋友5 小时前
什么是API签名?
前端·后端·安全
昵称为空C7 小时前
SpringBoot3 http接口调用新方式RestClient + @HttpExchange像使用Feign一样调用
spring boot·后端
架构师沉默7 小时前
设计多租户 SaaS 系统,如何做到数据隔离 & 资源配额?
java·后端·架构