第9章 集合
9.1 Java集合框架
- 队列 Queue 实现:循环数组(有界集合、容量有限)、链表(对象数量没有上限、效率不如数组);
- Collection:集合类的基本接口;
- Iterator:next()、hasNext() 、remove();(for each 可以处理实现了Iterable接口的对象)
iterator();
size();
isEmpty();
contains();
containsAll();
add();
addAll();
remove();
removeAll();
retainAll();
clear();
toArray();
put(key, value);
get(key);
add(index, element);
get(index);
set(index, element);
9.2 集合框架中的接口
![在这里插入图片描述](https://img-blog.csdnimg.cn/ec47dd5ab6444049ae87f689571a34e1.png)
9.3 具体集合
![在这里插入图片描述](https://img-blog.csdnimg.cn/842153eb7ce54a08b66f49cf24f91bc4.png)
- LinkedList可以使用ListIterator双向遍历链表元素,以及添加和删除元素(不支持快速随机访问);
- TreeSet:有序集合(红黑树),需提供比较方法(实现Comparable接口);
- PriorityQueue:堆(获取最小元素、自动排序);
9.4 映射
getOrDefault(key, default);
keySet();
values();
entrySet();
entry.getKey(); entry.getValue();
WeakHashMap
9.5 视图与包装器
List<String> names = List.of("Peter", "Paul", "Mary");
var names = new ArrayList<>(List.of("Peter", "Paul", "Mary"));
Set<Integer> numbers = Set.of(1, 2, 3);
Map<String, Integer> scores = Map.of("Peter", 1, "Paul", 2, "Mary", 3);
var name = names.subList(1, 2);
name.clear();
Collections.unmodifiableCollection();
Collections.unmodifiableList();
Collections.unmodifiableSet();
......
var map = Collections.sybchronizedMap(new HashMap<String, Employee>());
List<String> safeStrings = Collections.checkedList<strings, String.class);
9.6 算法
Collections.sort(...);
Collections.reverseOrder();
Collections.shuffle();
Collections.binarySearch(c, element);
Collections.replaceAll();
var staff = new HashSet<>(List.of(values));
Object[] values = staff.toArray();
String[] values = staff.toArray(new String[0]);
9.7 遗留的集合
var settings = new Properties();
settings.setProperty("widty", "600.9");
BitSet();
set();
get();