同步操作将从 LiuYan/Java 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
import java.util.*;import java.util.stream.Collectors;import java.util.stream.Stream;/*** @author LiuYan* @date 2023年5月9日* 对于JDK9和JDK10的完善,主要是对于Stream、集合等API的增强、新增ZGC垃圾收集器。*/public class ApiOptimization {public static void main(String[] args) {// 该方法返回满足指定条件的前元素,并在遇到第一个不满足条件的元素时停止处理List<Integer> list = Arrays.asList(1, 3, 5, 7, 9, 2, 4, 6, 8, 10);List<Integer> before = list.stream().takeWhile(n -> n < 5).collect(Collectors.toList());System.out.println(before); // Output: [1, 3]// 该方法返回满足指定条件的后元素,并在遇到第一个不满足条件的元素时停止处理List<Integer> after = list.stream().dropWhile(n -> n < 5).collect(Collectors.toList());System.out.println(after); // Output: [1, 3]// 该方法允许创建一个可能为 null 的单一元素流Stream.ofNullable(null).forEach(System.out::println);// 该方法现在支持提供初始值和限制条件,可以更灵活地使用。Stream.iterate(new int[]{0, 1}, arr -> new int[]{arr[1], arr[0] + arr[1]}).map(arr -> arr[0]).limit(10).forEach(System.out::println);// 如果值为空,则抛出指定的异常// Optional.ofNullable(null).orElseThrow();// ifPresentOrElse() 方法对它们进行判断,并分别执行不同的操作Optional<String> optional1 = Optional.of("Hello");optional1.ifPresentOrElse(value -> System.out.println("Value is present: " + value),() -> System.out.println("Value is absent"));Optional<String> optional2 = Optional.empty();optional2.ifPresentOrElse(value -> System.out.println("Value is present: " + value),() -> System.out.println("Value is absent"));// 允许使用 IntFunction 参数指定数组类型List<String> strings = Arrays.asList("Java", "Python", "Ruby");String[] strArray1 = strings.toArray(new String[0]); // 使用指定类型的数组System.out.println(strArray1);String[] strArray2 = strings.toArray(String[]::new); // 使用 IntFunction 指定类型的数组System.out.println(strArray2);// 可以从不可变集合中创建副本List<String> list1 = List.of("java11");List<String> copyList = List.copyOf(list1);System.out.println(copyList);Set<String> set = Set.of("java11");Set<String> copySet = Set.copyOf(set);System.out.println(copySet);Map<String, Integer> map = Map.of("java", 11);Map<String, Integer> copyMap = Map.copyOf(map);System.out.println(copyMap);}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。