1、排序
List<String> names = Arrays.asList("peter", "anna", "mike", "xenia");
names.sort((a, b) -> a.compareTo(b));
将names列表按首字母排序。
2、流式编程
java.util.Stream
表示可以在其上执行一个或多个操作的元素序列。流操作是中间操作或终端操作。当终端操作返回某种类型的结果时,中间操作会返回流本身,因此您可以连续链接多个方法调用。流是在源上创建的,例如java.util.Collection
类似的列表或集(不支持映射)。流操作可以顺序执行,也可以并行执行。
List<String> stringCollection = new ArrayList<>(); stringCollection.add("ddd2"); stringCollection.add("aaa2"); stringCollection.add("bbb1"); stringCollection.add("aaa1"); stringCollection.add("bbb3"); stringCollection.add("ccc"); stringCollection.add("bbb2"); stringCollection.add("ddd1");
过滤(filter)
过滤器接受谓词以过滤流的所有元素。此操作是中间操作,相当于对元素进行筛选。ForEach接受为过滤流中的每个元素执行的使用者。ForEach是一个终端操作。
stringCollection .stream() .filter((s) -> s.startsWith("a")) .forEach(System.out::println);
排序(sorted)
Sorted是一个中间操作,它返回流的排序视图。除非您传递自定义,否则元素按自然顺序排序Comparator
。
stringCollection .stream() .sorted() .filter((s) -> s.startsWith("a")) .forEach(System.out::println);
请记住,sorted
它只会创建流的排序视图,而不会操纵已备份集合的顺序。顺序stringCollection
是不受影响的:
转化(map)
中间操作map
的每个元素转换成经由给定功能的另一个对象。以下示例将每个字符串转换为大写字符串。但您也可以使用map
将每个对象转换为另一种类型。结果流的泛型类型取决于传递给函数的泛型类型。
stringCollection .stream() .map(String::toUpperCase) .sorted((a, b) -> b.compareTo(a)) .forEach(System.out::println);
匹配(match)
可以使用各种匹配操作来检查某个变量是否与流匹配。所有这些操作都是终端并返回一个布尔结果。
boolean anyStartsWithA = stringCollection .stream() .anyMatch((s) -> s.startsWith("a")); System.out.println(anyStartsWithA); // true boolean allStartsWithA = stringCollection .stream() .allMatch((s) -> s.startsWith("a")); System.out.println(allStartsWithA); // false boolean noneStartsWithZ = stringCollection .stream() .noneMatch((s) -> s.startsWith("z")); System.out.println(noneStartsWithZ); // true
计数(count)
Count是一个终端操作,它将流中的元素数作为a返回long
。
long startsWithB = stringCollection .stream() .filter((s) -> s.startsWith("b")) .count(); System.out.println(startsWithB); // 3
操作(reduce)
reduce可以执行各类操作。
stream = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7});
//求和
stream.reduce((i, j) -> i + j).ifPresent(System.out::println);
//求最大值
stream.reduce(Integer::max).ifPresent(System.out::println);
//求最小值
stream.reduce(Integer::min).ifPresent(System.out::println);
//做逻辑
stream.reduce((i, j) -> i > j ? j : i).ifPresent(System.out::println);
Parallel Streams(并行流)
如上所述,流可以是顺序的或并行的。对顺序流的操作在单个线程上执行,而并行流上的操作在多个线程上同时执行。
以下示例演示了使用并行流来提高性能的难易程度。
现在我们测量对此集合的流进行排序所需的时间。
顺序排序
long t0 = System.nanoTime(); long count = values.stream().sorted().count(); System.out.println(count); long t1 = System.nanoTime(); long millis = TimeUnit.NANOSECONDS.toMillis(t1 - t0); System.out.println(String.format("sequential sort took: %d ms", millis)); // sequential sort took: 899 ms
并行排序
long t0 = System.nanoTime(); long count = values.parallelStream().sorted().count(); System.out.println(count); long t1 = System.nanoTime(); long millis = TimeUnit.NANOSECONDS.toMillis(t1 - t0); System.out.println(String.format("parallel sort took: %d ms", millis)); // parallel sort took: 472 ms
正如您所看到的,两个代码片段几乎完全相同,但并行排序大约快了50%。你所要做的就是换stream()
到parallelStream()
。