目录
集合遍历方式可以查看我之前写的文章:
集合遍历:(19条消息) Collection----集合四种遍历方式_Xiayebuliang的博客-CSDN博客_collection 遍历的方式
5.1、Collection接口
常见方法:
三种遍历:
Collection<String> collection = new ArrayList<>();
collection.add("张三");
collection.add("李四");
collection.add("王五");
//第一种:使用迭代器
Iterator<String> iterator = collection.iterator();
while (iterator.hasNext()) {
String s = iterator.next();
System.out.println(s);
}
//第二种:使用增强for,其实是迭代器的升级版
for (String s : collection) {
System.out.println(s);
}
//第三种:使用forEach,其实是增强for的升级版
collection.forEach(s -> {
System.out.println(s);
});
遍历删除:
Collection<String> collection = new ArrayList<>();
collection.add("aaa");
collection.add("bbb");
collection.add("ccc");
collection.add("ddd");
//使用迭代器删除
Iterator<String> iterator = collection.iterator();
while (iterator.hasNext()) {
String next = iterator.next();
if (next.equals("aaa")) {
iterator.remove();
}
}
collection.forEach(System.out::println);
流式操作:
Stream流是由三部分组成的。数据源,零个或一个或多个中间操作,零个或一个终止操作。
在 Java 8 中,集合接口有两个方法来生成流:
- stream() − 为集合创建串行流。
- parallelStream() − 为集合创建并行流。
和以前的Collection操作不同,Stream操作还有两个基础的特征:
- Pipelining :中间操作都会返回流对象本身。这样多个操作可以串联成一个管道,如同流式风格(fluent style)。这样做可以对操作进行优化,比如延迟执行(laziness)和短路( short-circuiting)。
- 内部迭代 :以前对集合遍历都是通过Iterator或者For-Each的方式,显式的在集合外部进行迭代,这叫做外部迭代。Stream提供了内部迭代的方式,通过访问者模式(Visitor)实现。
注意:Stream操作并不会存储元素,而是按需计算。
中间操作:
filter:过滤操作
Collection<String> collection = Arrays.asList("abc", "abd", "abe", "123", "456", "789", "aaa", "bbb", "ccc", "aaa");
collection.stream().filter(s -> s.startsWith("a")).forEach(System.out::println);
distinct:去重操作
Collection<String> collection = Arrays.asList("abc", "abd", "abe", "123", "456", "789", "aaa", "bbb", "ccc", "aaa");
collection.stream().distinct().forEach(System.out::println);
limit:返回前 n 个元素
Collection<String> collection = Arrays.asList("abc", "abd", "abe", "123", "456", "789", "aaa", "bbb", "ccc", "aaa");
collection.stream().limit(5).forEach(System.out::println);
skip:跳过前 n 个元素
Collection<String> collection = Arrays.asList("abc", "abd", "abe", "123", "456", "789", "aaa", "bbb", "ccc", "aaa");
collection.stream().skip(5).forEach(System.out::println);
sorted:对流进行排序
Collection<String> collection = Arrays.asList("abc", "abd", "abe", "123", "456", "789", "aaa", "bbb", "ccc", "aaa");
collection.stream().sorted().forEach(System.out::println);
map:将每个元素映射为新元素
Collection<String> collection = Arrays.asList("abc", "abd", "abe", "123", "456", "789", "aaa", "bbb", "ccc", "aaa");
collection.stream().map(s -> s.toUpperCase()).forEach(System.out::println);
flatMap:将每个流转化为一个流
Collection<String> collection = Arrays.asList("abc", "abd", "abe", "123", "456", "789", "aaa", "bbb", "ccc", "aaa");
collection.stream()
.map(s -> s.toLowerCase())
.peek(s -> System.out.println("toLowerCase value: " + s))
.map(s -> s.toUpperCase())
.peek(s -> System.out.println("toUpperCase value: " + s))
.forEach(System.out::println);
max:常用于获取流中最大值
Collection<Integer> collection = Arrays.asList(3, 2, 1, 4, 5, 6);
Optional<Integer> max = collection.stream().max(Comparator.comparingInt(o -> o));
max.ifPresent(System.out::println);
min:常用于获取流中最小值
Collection<Integer> collection = Arrays.asList(3, 2, 1, 4, 5, 6);
Optional<Integer> max = collection.stream().min(Comparator.comparingInt(o -> o));
max.ifPresent(System.out::println);
终止操作:
forEach:用于串行流保持顺序遍历
Collection<String> collection = Arrays.asList("abc", "abd", "abe", "123", "456", "789", "aaa", "bbb", "ccc", "aaa");
collection.parallelStream().forEach(System.out::println);
forEachOrdered:用于并行流保持顺序遍历
Collection<String> collection = Arrays.asList("abc", "abd", "abe", "123", "456", "789", "aaa", "bbb", "ccc", "aaa");
collection.parallelStream().forEachOrdered(System.out::println);
anyMatch:检查是否至少匹配一个元素
Collection<String> collection = Arrays.asList("abc", "abd", "abe", "123", "456", "789", "aaa", "bbb", "ccc", "aaa");
boolean result = collection.parallelStream().anyMatch(s -> s.equals("aaa"));
System.out.println(result);
noneMatch:检查是否没有匹配所有元素
Collection<String> collection = Arrays.asList("abc", "abd", "abe", "123", "456", "789", "aaa", "bbb", "ccc", "aaa");
boolean result = collection.parallelStream().noneMatch(s -> s.equals("aaa"));
System.out.println(result);
allMatch:检查是否匹配所有元素
Collection<String> collection = Arrays.asList("abc", "abd", "abe", "123", "456", "789", "aaa", "bbb", "ccc", "aaa");
boolean result = collection.parallelStream().allMatch(s -> s.equals("aaa"));
System.out.println(result);
findAny:将返回当前流中的任意元素
Collection<String> collection = Arrays.asList("abc", "abd", "abe", "123", "456", "789", "aaa", "bbb", "ccc", "aaa");
Optional<String> any = collection.parallelStream().findAny();
if (any.isPresent()) {
System.out.println(any.get());
}
findFirst:将返回当前流中的第一元素
Collection<String> collection = Arrays.asList("abc", "abd", "abe", "123", "456", "789", "aaa", "bbb", "ccc", "aaa");
Optional<String> first = collection.parallelStream().findFirst();
if (first.isPresent()) {
System.out.println(first.get());
}
count:返回当前流中元素的总数
Collection<String> collection = Arrays.asList("abc", "abd", "abe", "123", "456", "789", "aaa", "bbb", "ccc", "aaa");
long count = collection.parallelStream().count();
System.out.println(count);
reduce:对流中的数据进行计算
Collection<String> collection = Arrays.asList("abc", "abd", "abe", "123", "456", "789", "aaa", "bbb", "ccc", "aaa");
Optional<String> reduce = collection.parallelStream().reduce((acc, item) -> acc + item);
if (reduce.isPresent()) {
System.out.println(reduce.get());
}
String string = collection.parallelStream().reduce("prefix_", (acc, item) -> acc + item);
System.out.println(string);
collect:对流中的数据进行收集
Collection<String> collection = Arrays.asList("abc", "abd", "abe", "123", "456", "789", "aaa", "bbb", "ccc", "aaa");
ArrayList<String> collect1 = collection.parallelStream().collect(Collectors.toCollection(ArrayList<String>::new));
LinkedList<String> collect2 = collection.parallelStream().collect(Collectors.toCollection(LinkedList<String>::new));
List<String> collect3 = collection.parallelStream().collect(Collectors.toList());
Set<String> collect4 = collection.parallelStream().collect(Collectors.toSet());
Map<String, String> collect5 = collection.parallelStream().collect(Collectors.toMap(k -> k, v -> v, (oldVale, newValue) -> newValue));
String string = collection.parallelStream().collect(Collectors.joining());
5.2、List接口
常见方法:
三种遍历:
List<String> list = Arrays.asList("abc", "abd", "abe", "123", "456", "789", "aaa", "bbb", "ccc", "aaa");
ListIterator<String> listIterator = list.listIterator();
//第一种:使用下标遍历
for (int i = 0; i < list.size(); i++) {
String s = list.get(i);
System.out.println(i + "=" + s);
}
//第二种:从前向后遍历
while (listIterator.hasNext()) {
int nextIndex = listIterator.nextIndex();
String next = listIterator.next();
System.out.println(nextIndex + "=" + next);
}
//第三种:从后向前遍历
while (listIterator.hasPrevious()) {
int previousIndex = listIterator.previousIndex();
String previous = listIterator.previous();
System.out.println(previousIndex + "=" + previous);
}
遍历删除:
List<String> list = new ArrayList<>();
list.add("aaa");
list.add("bbb");
list.add("ccc");
list.add("ddd");
//使用迭代器删除
ListIterator<String> listIterator = list.listIterator();
while (listIterator.hasNext()) {
String next = listIterator.next();
if (next.equals("bbb")) {
listIterator.remove();
}
}
//使用迭代器删除
while (listIterator.hasPrevious()) {
String previous = listIterator.previous();
if (previous.equals("ccc")) {
listIterator.remove();
}
}
list.forEach(System.out::println);
子类对比:
相同点:元素的存取是有序的,可以重复,可以存取null,元素有下标
不同点:
5.3、Set接口
子类对比:
相同点:元素的存取是无序的,不能重复,可以存取null,元素无下标
不同点:
5.4、Map接口
常见方法:
三种遍历:
Map<Integer, String> map = new HashMap<>();
map.put(1, "张三");
map.put(2, "李四");
map.put(3, "王五");
map.put(4, "赵六");
//第一种遍历:遍历所有键值对
Set<Map.Entry<Integer, String>> entries = map.entrySet();
entries.forEach((Map.Entry<Integer, String> entry) -> {
System.out.println(entry.getKey() + "=" + entry.getValue());
});
//第二种遍历:遍历所有键集合
Set<Integer> keySets = map.keySet();
keySets.forEach((key) -> {
System.out.println(key);
});
//第三种遍历:遍历所有值集合
Collection<String> values = map.values();
values.forEach((value) -> {
System.out.println(value);
});
遍历删除:
Map<Integer, String> map = new HashMap<>();
map.put(1, "张三");
map.put(2, "李四");
map.put(3, "王五");
map.put(4, "赵六");
//第一种删除
Set<Map.Entry<Integer, String>> entries = map.entrySet();
Iterator<Map.Entry<Integer, String>> entryIterator = entries.iterator();
while (entryIterator.hasNext()) {
Map.Entry<Integer, String> next = entryIterator.next();
if (next.getKey() == 2) {
entryIterator.remove();
}
}
//第二种删除
Set<Integer> keySets = map.keySet();
Iterator<Integer> keySetIterator = keySets.iterator();
while (keySetIterator.hasNext()) {
Integer next = keySetIterator.next();
if (next == 3) {
keySetIterator.remove();
}
}
子类对比:
相同点:以键值对的形式添加元素,键不能重复,值可以重复
不同点:
5.5、Collections类
常见方法: