转载:https://blog.csdn.net/Fine_Cui/article/details/131913537
![在这里插入图片描述](https://img-blog.csdnimg.cn/19fca73bb6ba41d196d7497f8bf57139.png)
public static void main(String[] args) {
List<String> tempList = Arrays.asList("刘一手", "杜子腾", "林大蛋", "Ekko");
List<String> resList = tempList.stream()
.filter(s -> s.contains("大"))
.collect(Collectors.toList());
System.out.println(resList.toString());
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/4e788b41b81640738085bd464bc9d98f.png)
[林大蛋]
![在这里插入图片描述](https://img-blog.csdnimg.cn/c97b413d475748eea12dc6e800bea6aa.png)
public static void main(String[] args) {
List<String> tempList = Arrays.asList("刘一手", "杜子腾", "林大蛋", "Ekko");
List<String> resList = tempList.stream()
.map(s -> "姓名: " + s)
.collect(Collectors.toList());
System.out.println(resList.toString());
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/9717a1ca990d455882c217074bfad491.png)
@Data
static class Personnel {
private String name;
private List<String> tagList;
public Personnel(String name, List<String> tagList) {
this.name = name;
this.tagList = tagList;
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/e845a94aa97a4649a8d8ab1838f638f6.png)
public static void main(String[] args) {
Personnel personA = new Personnel("张三", Arrays.asList("抽烟", "喝酒", "烫头"));
Personnel personB = new Personnel("李斯", Arrays.asList("编码", "喝酒", "踢足球"));
List<Personnel> personnelList = Arrays.asList(personA, personB);
personnelList.stream()
.flatMap(p -> p.getTagList().stream())
.forEach(s -> System.out.print(s + " "));
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/2102ed19eadc497bbf9ba18dfed1568d.png)
public class SortedTest {
public static void main(String[] args) {
List<Integer> numList = Arrays.asList(10, 20, 18, 300, 30, 2);
List<Integer> orderList = numList.stream()
.sorted()
.collect(Collectors.toList());
System.out.printf("① 默认排序: %s%n", orderList);
List<Integer> orderDescList = numList.stream()
.sorted((x, y) -> {
return y.compareTo(x);
})
.collect(Collectors.toList());
System.out.printf("② 自定义排序: %s%n", orderDescList);
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/f3b8ce64877e428c989c9b93d1573a3f.png)
public class DistinctStreamTest {
public static void main(String[] args) {
List<Integer> numList = Arrays.asList(1,1,1,1,2,3,2,2);
List<Integer> distinctList = numList.stream()
.distinct()
.collect(Collectors.toList());
System.out.println(distinctList);
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/d8c578e06ece458ca8136f544dddb671.png)
public class LimitStreamTest {
public static void main(String[] args) {
List<Integer> numList = Arrays.asList(1,2,3,4,5,6,7,8);
List<Integer> limitList = numList.stream()
.limit(4)
.collect(Collectors.toList());
System.out.println(limitList);
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/4d377882948241c0bf8fc4f145e43344.png)
public class SkipStreamTest {
public static void main(String[] args) {
List<Integer> numList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8);
List<Integer> skipList = numList.stream()
.skip(numList.size() - 2)
.collect(Collectors.toList());
System.out.println(skipList);
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/0c59201fd316448f9ce8fd4363318088.png)
public class PeekStreamTest {
public static void main(String[] args) {
List<Integer> numList = Arrays.asList(5, 6, 7, 8);
List<Integer> resList = numList.stream()
.peek(System.out::println)
.filter(s -> s == 5)
.peek(s -> System.out.printf("过滤后的:%d%n", s))
.collect(Collectors.toList());
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/ccd65748025349cab19f658e30d96c65.png)
public class ForEachStreamTest {
public static void main(String[] args) {
List<Integer> salaryList = Arrays.asList(12000, 20000, 30000, 4000);
salaryList.stream()
.peek(s -> System.out.print("工资普涨前:" + s))
.map(s -> s + 500)
.forEach(s -> {
System.out.println("--工资普涨后:" + s);
});
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/c99cc1f92165439c9bd168ea67b4bee2.png)
public class CollectStreamTest {
public static void main(String[] args) {
List<User> userList = Arrays.asList(new User("张三", 2000.5),
new User("李斯", 11000.5),
new User("王二", 12000.5),
new User("张六", 32000.5),
new User("赵公子", 1000000.0));
Map<String, Double> userSalaryMap = userList.stream()
.collect(Collectors.toMap(User::getName, User::getSalary));
userSalaryMap.forEach((k, v) -> {
System.out.printf("姓名:%s,工资:%.2f%n", k, v);
});
}
@Data
@AllArgsConstructor
static class User {
private String name;
private Double salary;
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/9350b39a436548e99e63b1e887bca685.png)
public class ToArrayStreamTest {
public static void main(String[] args) {
IntStream intStream = IntStream.of(1, 2, 3, 4, 5);
int[] intArray = intStream.toArray();
System.out.println(Arrays.toString(intArray));
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/cf1b6e4569054e74b07870b04dc94056.png)
public class ReduceStreamTest {
public static void main(String[] args) {
IntStream intStream = IntStream.of(1, 2, 3, 4, 5);
OptionalInt sumOptional = intStream.reduce((a, b) -> a + b);
int sum = sumOptional.orElse(0);
System.out.println("总和: " + sum);
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/0ab253220c8d440680ed03f0c68ea85b.png)
Optional<T> min(Comparator<? super T> comparator)
![在这里插入图片描述](https://img-blog.csdnimg.cn/3dc4a9b8477e49bf98d3e403172f34e3.png)
Optional<T> max(Comparator<? super T> comparator)
![在这里插入图片描述](https://img-blog.csdnimg.cn/569de6a5e2dd4439b9d1cd62b873b376.png)
public class MinMaxStreamTest {
public static void main(String[] args) {
Stream<Integer> integerStream = Stream.of(1, 5, 3, 8, 2);
Optional<Integer> minOptional = integerStream.min(Integer::compareTo);
if (minOptional.isPresent()) {
System.out.println("最小值为: " + minOptional.get());
} else {
System.out.println("流为空.");
}
Stream<Integer> newIntegerStream = Stream.of(1, 5, 3, 8, 2);
Optional<Integer> maxOptional = newIntegerStream.max(Integer::compareTo);
if (maxOptional.isPresent()) {
System.out.println("最大值为: " + maxOptional.get());
} else {
System.out.println("流为空.");
}
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/29ad486cfe474d2390187929adf96e0e.png)
public class CountStreamTest {
public static void main(String[] args) {
Stream<Integer> integerStream = Stream.of(1, 5, 3, 8, 2);
long count = integerStream.count();
System.out.println("元素数量: " + count);
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/8555fadb37ef41aaa7cbdcb6e98c5434.png)
boolean anyMatch(Predicate<? super T> predicate)
![在这里插入图片描述](https://img-blog.csdnimg.cn/36fb8f4a984c4f0ba6867f28c020c7d7.png)
boolean allMatch(Predicate<? super T> predicate)
![在这里插入图片描述](https://img-blog.csdnimg.cn/6c94b251cfb14220aac904e962e76547.png)
boolean noneMatch(Predicate<? super T> predicate)
![在这里插入图片描述](https://img-blog.csdnimg.cn/5c9cf4db3a834eeab04932b40bf3c389.png)
public class MatchStreamTest {
public static void main(String[] args) {
Stream<Integer> integerStream = Stream.of(1, 5, 3, 8, 2);
boolean anyGreaterThan5 = integerStream.anyMatch(num -> num > 4);
System.out.println("是否存在元素大于 5 ?" + anyGreaterThan5);
Stream<Integer> newIntegerStream = Stream.of(1, 5, 3, 8, 2);
boolean allLessThan10 = newIntegerStream.allMatch(num -> num < 10);
System.out.println("所有元素都小于10 ? " + allLessThan10);
Stream<Integer> newestIntegerStream = Stream.of(1, 5, 3, 8, 2);
boolean noneEqualTo10 = newestIntegerStream.noneMatch(num -> num == 10);
System.out.println("是否没有元素等于 10 ? " + noneEqualTo10);
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/fecdc01e1bb64604a07de6a424a288bb.png)
Optional<T> findFirst()
![在这里插入图片描述](https://img-blog.csdnimg.cn/c188050914a24dd0b1560b77a34d8178.png)
Optional<T> findAny()
![在这里插入图片描述](https://img-blog.csdnimg.cn/92b7aebbdaf64074a8fa1f7ea89b44cf.png)
public class FindStreamTest {
public static void main(String[] args) {
Stream<Integer> integerStream = Stream.of(1, 5, 3, 8, 2);
Optional<Integer> firstElementOptional = integerStream.findFirst();
if (firstElementOptional.isPresent()) {
System.out.println("发现第一个元素: " + firstElementOptional.get());
} else {
System.out.println("流为空!");
}
Stream<Integer> newIntegerStream = Stream.of(1, 5, 3, 8, 2);
Optional<Integer> anyElementOptional = newIntegerStream.findAny();
if (anyElementOptional.isPresent()) {
System.out.println("找到任意一个元素: " + anyElementOptional.get());
} else {
System.out.println("流为空!");
}
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/4e011da90caa4ccd86d924ecbb358a34.png)