List, Set and Map are all interfaces: they define how these respective types
work, but they don’t provide implementation code
overview
1. List(列表):
(1) 创建、访问和操作列表:ArrayList 和 LinkedList。
Creating, accessing, and manipulating a list (两种实现List接口的具体类)
(2) 在方法中使用列表,或作为类的实例变量使用。
Using lists in methods and as instance variables in a class.
2. Set 和 Map(集合与映射): 它们的特性 (Characteristics)及操作方法(operations),并用于解决实际问题。
一、Array Deficiencies
数组的局限性
静态大小:数组需要在声明时定义大小(size),无法动态扩展(grow arbitrarily)。
二、Java Collections Framework
Java 集合框架
集合框架(Collections Framework)是Java编程语言中一个库,它提供了一套接口和类,用于存储和操作对象集合。通过集合框架,程序员可以更方便地处理数据集合
提供更强大灵活的工具管理对象集合(object collections),包括:
List(列表):有序集合,允许重复元素。
Set(集合):无序集合,元素唯一。
Map(映射):键值对集合,用于快速查找。
三、 列表(List)
1. 特点
• 列表是一种有序集合(collection of objects),允许重复元素。
• 大小可以动态增减,无需提前定义大小。
grow or shrink dynamically to accommodate their contents
都是 List 接口的实现,提供相同的操作,但底层机制不同:
ArrayList:基于数组实现,适合频繁读取。 quick access and random reads.
LinkedList:基于链表实现,适合频繁插入和删除操作。frequent insertions and deletions.
2. Creating Lists
创建方式
泛型类型(Generic Type):
• 列表可以通过泛型 <Type> 来定义存储的数据类型。
泛型类型在尖括号 < > 中声明,确保类型安全。
List<String> list1 = new ArrayList<>();
List<String> list2 = new LinkedList<>();
如果左侧和右侧泛型类型相同,右侧的泛型声明可以省略。
List<String> list1 = new ArrayList<>();
List<String> list2 = new LinkedList<>();