Overview
List, Set, Map 集合
这是CW3的关键部分。
List, Set, Map 集合
这是CW3的关键部分。
60.List
List的特性:有序、可重复。
我们将学习关于列表
○如何创建一个列表
○如何添加和访问它的数据
○如何插入和删除它的数据
○如何使用它来解决问题
●要创建列表,请在菱形<>操作符中指定对象/泛型类型:
Add添加x到第a位,a位开始的往后移。
○ adding 加
list.add(5)
○ inserting 插入
list.add(1, 3)
○ assigning 分配
list.set(2, 7)
○ retrieving 检索第几位的数据
list.get(2)
○ length List数据总长度 int
list.size()
○ remove 移除第几位
list.remove(1)
○ remove all 清除全部
list.clear()
○ Create a list from an array 创建
ArrayList vs LinkedList 1
总结:
ArrayList Resizing Array
优势:频繁访问,
linkedList Doubly Linked List
优势:频繁插入清除。
打印数据
我们将学习关于列表
○如何创建一个列表
○如何添加和访问它的数据
○如何插入和删除它的数据
○如何使用它来解决问题
●要创建列表,请在菱形<>操作符中指定对象/泛型类型:
List list1 = new ArrayList();
List list2 = new LinkedList();
List list = new ArrayList<>();
list.add("A");
list.add("B");
list.add("C");
list.add(1, "X");
list.set(2, "Y");
System.out.println(list);
Output
[A X Y C]
Set将第三位的变为xAdd添加x到第a位,a位开始的往后移。
○ adding 加
list.add(5)
○ inserting 插入
list.add(1, 3)
○ assigning 分配
list.set(2, 7)
○ retrieving 检索第几位的数据
list.get(2)
○ length List数据总长度 int
list.size()
○ remove 移除第几位
list.remove(1)
○ remove all 清除全部
list.clear()
○ Create a list from an array 创建
List list = Arrays.asList(10, 20, 30)
List is an interfaceArrayList vs LinkedList 1
总结:
ArrayList Resizing Array
优势:频繁访问,
linkedList Doubly Linked List
优势:频繁插入清除。
打印数据
public class List {
public static void main(String[] args) {
List list = Arrays.asList(10, 20, 30);
for (int x : list) {
System.out.print(x + " ");
}}}
Output:10 20 30
61.Set
Set接口是一个无序、不可重复的集合
Set接口有几个常见的实现类:
1. HashSet – implemented using a hash table
2. TreeSet – implemented using a self-balancing tree data structure
set.add(5)
○ checking membership 检查是否包含5 输出boolean
set.contains(5)
○ remove 移除5
set.remove(5)
○ union 将集合c中所有元素加到set中
set.addAll(c)
○ intersection 保留set和c的交集
set.retainAll(c)
○ difference 移除set和c的交集
set.removeAll(c)
○ contain/subset 检查set是否是c的子集
set.containsAll(c)
○ size set元素的个数
set.size()
○ clear contents 清除set
set.clear()
Set set = new HashSet<>();
Set set = new TreeSet<>();
○ adding 单纯导入5set.add(5)
○ checking membership 检查是否包含5 输出boolean
set.contains(5)
○ remove 移除5
set.remove(5)
○ union 将集合c中所有元素加到set中
set.addAll(c)
○ intersection 保留set和c的交集
set.retainAll(c)
○ difference 移除set和c的交集
set.removeAll(c)
○ contain/subset 检查set是否是c的子集
set.containsAll(c)
○ size set元素的个数
set.size()
○ clear contents 清除set
set.clear()
62.Map
通过查看Map接口描述,看到Map有多个子类,这里我们主要讲解常用的HashMap集合、LinkedHashMap集合。
HashMap:存储数据采用的哈希表结构,元素的存取顺序不能保证一致。由于要保证键的唯一、不重复,需要重写键的hashCode()方法、equals()方法。
LinkedHashMap:HashMap下有个子类LinkedHashMap,存储数据采用的哈希表结构+链表结构。通过链表结构可以保证元素的存取顺序一致;通过哈希表结构可以保证的键的唯一、不重复,需要重写键的hashCode()方法、equals()方法。
TreeMap:TreeMap集合和Map相比没有特有的功能,底层的数据结构是红黑树;可以对元素的键进行排序,排序方式有两种:自然排序和比较器排序
一般会有对应关系,所以叫key/value pairs
○ map.put(key, val)
adding the mapping key → val 添加映射键→val
○ map.get(key)
get the value for a key 获取一个键的值
○ map.containsKey(key)
test whether the map has the key 测试该地图是否具有该密钥
○ map.containsValue(value)
test whether the map has the value 测试该映射是否具有该值
○ map.remove(key)
delete a mapping 删除映射
○ map.keySet() 返回一个包含所有键的集合
HashMap
LinkedHashMap
TreeMap
○ map.put(key, val)
adding the mapping key → val 添加映射键→val
○ map.get(key)
get the value for a key 获取一个键的值
○ map.containsKey(key)
test whether the map has the key 测试该地图是否具有该密钥
○ map.containsValue(value)
test whether the map has the value 测试该映射是否具有该值
○ map.remove(key)
delete a mapping 删除映射
○ map.keySet() 返回一个包含所有键的集合
Swordsman tanjiro = new Swordsman("Tanjiro", 5);
Swordsman zenitsu = new Swordsman("Zenitsu", 2);
Swordsman inosuke = new Swordsman("Inosuke", 10);
Map troops = new HashMap<>();
troops.put("Tanjiro", tanjiro);
troops.put("Zenitsu", zenitsu);
troops.put("Inosuke", inosuke);
Swordsman swordsman = troops.get("Kyojuro");
System.out.println(swordsman);
输出null因为没有该映射
Comments NOTHING