定时任务执行");
}
};
Thread thread = new Thread(runnable);
// invokeDynamic指令实现的(反汇编)
// MethodHandle
Runnable r = () -> System.out.println("定时任务执行");
Thread t = new Thread(r);
}
}
理解Stream
Stream是用于对集合(Collection)进行处理和操作的工具,让我们能够以声明式的方式进行数据的转换、过滤、映射等操作。Stream
可以看作是对集合进行高级迭代和函数式处理的工具,它提供了一种更现代、更简洁的方式来操作数据,使得代码更加清晰、易读、易维护。
Stream API 提供了一系列操作,这些操作分为两类:
- Intermediate(中间操作):这些操作返回一个新的 Stream,用于定义一系列的数据转换和处理操作,但并不立即执行。中间操作包括
filter
、map
、distinct
、sorted
、limit
、skip
等。
- Terminal(终端操作):这些操作触发 Stream 的处理,执行中间操作定义的一系列数据转换和处理操作,生成最终的结果。终端操作包括
forEach
、collect
、count
、reduce
、min
、max
、anyMatch
、allMatch
、noneMatch
等。
!!一般不会将Stream作为参数进行传递,但是如果有Stream作为参数进行传递的话,一定要进行串行与并行的判断,并且根据实际情况进行切换
/**
* Returns an equivalent stream that is sequential. May return
* itself, either because the stream was already sequential, or because
* the underlying stream state was modified to be sequential.
*
* <p>This is an <a href="package-summary.html#StreamOps">intermediate
* operation</a>.
*
* @return a sequential stream
*/
S sequential();
/**
* Returns an equivalent stream that is parallel. May return
* itself, either because the stream was already parallel, or because
* the underlying stream state was modified to be parallel.
*
* <p>This is an <a href="package-summary.html#StreamOps">intermediate
* operation</a>.
*
* @return a parallel stream
*/
S parallel();
示例使用 Stream API:
import java.util.Arrays;
import java.util.List;
public class StreamExample {
public static void main(String[] args) {
List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David", "Eva");
// 使用 Stream API 对集合进行操作
long count = names.stream()
.filter(name -> name.length() <= 4)
.map(String::toUpperCase)
.sorted()
.count();
System.out.println(count); // 输出:4
}
}
在上面的示例中,我们使用 Stream
API 对集合 names
进行一系列的操作:首先用 filter
过滤掉长度大于 4 的字符串,然后用 map
将字符串转换为大写,接着用 sorted
进行排序,最后用 count
终端操作统计符合条件的元素个数。
Stream
API 的优势在于它具有懒加载的特性,即只有当终端操作触发时,中间操作才会执行。这使得 Stream
在处理大量数据时,能够有效地提高性能和资源利用率。
sorted的应用
public class C03_08_Stream {
public static void main(String[] args) {
sorted(Integer::compareTo, Arrays.asList(5, 1, 5, 4, 8, 9, 6, 5, 4, 8, 5, 4, 2, 3, 4)).stream().distinct().forEach(System.out::println);
}
/**
* sorted<Comparator<? super T> comparator>的使用
* @param comparator 比较器
* @param collection 待排序的集合
* @param <T>
* @return 排序后的集合
*/
private static <T> Collection<T> sorted(Comparator<T> comparator, Collection<T> collection) {
return collection.stream().sorted(comparator).collect(Collectors.toList());
}
}
map的应用
map主要是做一个映射,例如这就是将Integer类型转换成Long类型
reduce主要是合并操作,例如将这将多个元素求和
/**
* Stream 的 map<Function>操作
*
* @param integers
*/
private static void count(Integer... integers) {
Stream.of(integers)
.map(Long::valueOf)
.reduce(Long::sum)
.ifPresent(System.out::println);
}
并行parallel
/**
* 并行parallel案例
* @param integers
*/
private static void parallelSort(Integer... integers){
Stream.of(integers).sorted(Integer::compareTo).parallel().forEach(C03_08_Stream::println);
}
public static void println(Object obj){
System.out.printf("[%s]:%s \n",Thread.currentTh