代码:
public class Test11 { public static void main(String[] args) { //有如下7个元素黄药师,冯蘅,郭靖,黄蓉,郭芙,郭襄,郭破虏,使用Stream将以郭字开头的元素存入新数组 String[] arr = {"黄药师","冯蘅","郭靖","黄蓉","郭芙","郭襄","郭破虏"}; Object[] array = Arrays.stream(arr).filter(a -> a.startsWith("郭")).toArray(); for (Object obj : array) { System.out.println(obj); } } }
代码版本2:
public class Test11 { public static void main(String[] args) { //有如下7个元素黄药师,冯蘅,郭靖,黄蓉,郭芙,郭襄,郭破虏,使用Stream将以郭字开头的元素存入新数组 Object[] array1 = Stream.of("黄药师", "冯蘅", "郭靖", "黄蓉", "郭芙", "郭襄", "郭破虏") .filter(a -> a.startsWith("郭")).toArray(); for (Object obj : array1) { System.out.println(obj); } } }
控制台打印效果: