@@ -4470,12 +4470,13 @@ PriorityQueue 是优先级队列,底层存储结构为 Object[],默认实现
44704470
44714471java.utils.Collections:集合**工具类**,Collections并不属于集合,是用来操作集合的工具类
44724472Collections有几个常用的API:
4473- `public static <T> boolean addAll(Collection<? super T> c, T... e)`:给集合对象批量添加元素
4474- `public static void shuffle(List<?> list)`:打乱集合顺序。
4475- `public static <T> void sort(List<T> list)`:将集合中元素按照默认规则排序。
4476- `public static <T> void sort(List<T> list,Comparator<? super T> )`:集合中元素按照指定规则排序
4477- `public static <T> List<T> synchronizedList(List<T> list)`:返回由指定 list 支持的线程安全 list
4478- `public static <T> Set<T> singleton(T o)`:返回一个只包含指定对象的不可变组
4473+
4474+ * `public static <T> boolean addAll(Collection<? super T> c, T... e)`:给集合对象批量添加元素
4475+ * `public static void shuffle(List<?> list)`:打乱集合顺序
4476+ * `public static <T> void sort(List<T> list)`:将集合中元素按照默认规则排序
4477+ * `public static <T> void sort(List<T> list,Comparator<? super T> )`:集合中元素按照指定规则排序
4478+ * `public static <T> List<T> synchronizedList(List<T> list)`:返回由指定 list 支持的线程安全 list
4479+ * `public static <T> Set<T> singleton(T o)`:返回一个只包含指定对象的不可变组
44794480
44804481```java
44814482public class CollectionsDemo {
@@ -4515,12 +4516,11 @@ public class Student{
45154516
45164517#### 概述
45174518
4518- >Collection是单值集合体系。
4519- >Map集合是一种双列集合,每个元素包含两个值。
4519+ Collection 是单值集合体系,Map集合是一种双列集合,每个元素包含两个值。
45204520
4521- Map集合的每个元素的格式:key=value(键值对元素),Map集合也被称为“键值对集合”
4521+ Map集合的每个元素的格式:key=value(键值对元素),Map集合也被称为键值对集合
45224522
4523- Map集合的完整格式:`{key1=value1 , key2=value2 , key3=value3 , ...}`
4523+ Map集合的完整格式:`{key1=value1, key2=value2, key3=value3, ...}`
45244524
45254525```
45264526Map集合的体系:
@@ -6632,7 +6632,7 @@ Stream 流其实就是一根传送带,元素在上面可以被 Stream 流操
66326632
66336633作用:
66346634
6635- * 可以解决已有集合类库或者数组API的弊端。
6635+ * 可以解决已有集合类库或者数组 API 的弊端
66366636* Stream 流简化集合和数组的操作
66376637* 链式编程
66386638
@@ -6655,7 +6655,7 @@ list.stream().filter(s -> s.startsWith("张"));
66556655
66566656#### 获取流
66576657
6658- 集合获取 Stream 流用:default Stream<E> stream()
6658+ 集合获取 Stream 流用:` default Stream<E> stream()`
66596659
66606660数组:Arrays.stream(数组) / Stream.of(数组);
66616661
@@ -6686,16 +6686,16 @@ Stream<String> arrStream2 = Stream.of(arr);
66866686
66876687#### 常用API
66886688
6689- | 方法名 | 说明 |
6690- | --------------------------------------------------------- | -------------------------------------------------------- |
6691- | void forEach(Consumer<? super T> action) | 逐一处理(遍历) |
6692- | long count | 返回流中的元素数 |
6693- | Stream<T> filterPredicate<? super T> predicate) | 用于对流中的数据进行过滤 |
6694- | Stream<T> limit(long maxSize) | 返回此流中的元素组成的流,截取前指定参数个数的数据 |
6695- | Stream<T> skip(long n) | 跳过指定参数个数的数据,返回由该流的剩余元素组成的流 |
6696- | <R> Stream<R> map(Function<? super T,? extends R> mapper) | 加工方法,将当前流中的T类型数据转换为另一种R类型的流 |
6697- | static <T> Stream<T> concat(Stream a, Stream b) | 合并a和b两个流为一个. 调用: `Stream.concat(s1,s2);` |
6698- | Stream<T> distinct() | 返回由该流的不同元素(根据Object.equals(Object) )组成的流 |
6689+ | 方法名 | 说明 |
6690+ | --------------------------------------------------------- | ---------------------------------------------------- |
6691+ | void forEach(Consumer<? super T> action) | 逐一处理(遍历) |
6692+ | long count | 返回流中的元素数 |
6693+ | Stream<T> filterPredicate<? super T> predicate) | 用于对流中的数据进行过滤 |
6694+ | Stream<T> limit(long maxSize) | 返回此流中的元素组成的流,截取前指定参数个数的数据 |
6695+ | Stream<T> skip(long n) | 跳过指定参数个数的数据,返回由该流的剩余元素组成的流 |
6696+ | <R> Stream<R> map(Function<? super T,? extends R> mapper) | 加工方法,将当前流中的T类型数据转换为另一种R类型的流 |
6697+ | static <T> Stream<T> concat(Stream a, Stream b) | 合并a和b两个流为一个,调用 `Stream.concat(s1,s2)` |
6698+ | Stream<T> distinct() | 返回由该流的不同元素组成的流 |
66996699
67006700```java
67016701public class StreamDemo {
@@ -6745,7 +6745,7 @@ class Student{
67456745
67466746终结方法:Stream 调用了终结方法,流的操作就全部终结,不能继续使用,如 foreach,count 方法等
67476747
6748- 非终结方法:每次调用完成以后返回一个新的流对象,可以继续使用,支持**链式编程**!
6748+ 非终结方法:每次调用完成以后返回一个新的流对象,可以继续使用,支持**链式编程**
67496749
67506750```java
67516751// foreach终结方法
@@ -6766,28 +6766,29 @@ list.stream().filter(s -> s.startsWith("张"))
67666766* Stream流:工具
67676767* 集合:目的
67686768
6769- | 方法名 | 说明 |
6770- | ------------------------------------------------------------ | ---------------------- |
6771- | R collect(Collector collector) | 把结果收集到集合中 |
6772- | public static <T> Collector toList() | 把元素收集到List集合中 |
6773- | public static <T> Collector toSet() | 把元素收集到Set集合中 |
6774- | public static Collector toMap(Function keyMapper,Function valueMapper) | 把元素收集到Map集合中 |
6775- | Object[] toArray() | 把元素收集数组中 |
6769+ Stream 收集方法:`R collect(Collector collector)` 把结果收集到集合中
6770+
6771+ Collectors 方法:
6772+
6773+ * `public static <T> Collector toList()`:把元素收集到 List 集合中
6774+ * `public static <T> Collector toSet()`:把元素收集到 Set 集合中
6775+ * `public static Collector toMap(Function keyMapper,Function valueMapper)`:把元素收集到 Map 集合中
6776+ * `Object[] toArray()`:把元素收集数组中
67766777
67776778```java
67786779public static void main(String[] args) {
67796780 List<String> list = new ArrayList<>();
6780- Stream<String> stream= list.stream().filter(s -> s.startsWith("张"));
6781+ Stream<String> stream = list.stream().filter(s -> s.startsWith("张"));
67816782 //把stream流转换成Set集合。
67826783 Set<String> set = stream.collect(Collectors.toSet());
67836784
67846785 //把stream流转换成List集合。
67856786 //重新定义,因为资源已经被关闭了
6786- Stream<String> stream1= list.stream().filter(s -> s.startsWith("张"));
6787+ Stream<String> stream1 = list.stream().filter(s -> s.startsWith("张"));
67876788 List<String> list = stream.collect(Collectors.toList());
67886789
67896790 //把stream流转换成数组。
6790- Stream<String> stream2 =list.stream().filter(s -> s.startsWith("张"));
6791+ Stream<String> stream2 = list.stream().filter(s -> s.startsWith("张"));
67916792 Object[] arr = stream2.toArray();
67926793 // 可以借用构造器引用申明转换成的数组类型!!!
67936794 String[] arr1 = stream2.toArray(String[]::new);
0 commit comments