数组相关问题以及操作大集合

2021-04-11  本文已影响0人  hbqzbl

1.比较数组和List谁更快?

2.如何将数组转换成ArrayList?

        String[] sArr = new String[]{"aa", "bb", "cc", "dd"};
        ArrayList<String> list = new ArrayList<>(Arrays.asList(sArr));

3.检查数组是否含有某个值

        String[] values = {"AB","BC","CD","AE"};
        boolean contains = Arrays.asList(values).contains("CD");
        String[] fieldsToInclude = { "id", "name", "location" };
        boolean contains = ArrayUtils.contains( fieldsToInclude, "name" );
        String[] values = {"AB","BC","CD","AE"};
        boolean contains = Arrays.stream(values).anyMatch("CD"::equals);
        int[] a = {1,2,3,4};
        boolean contains = IntStream.of(a).anyMatch(x -> x == 5);
        double[] d = {65D,76D};
        boolean contains = DoubleStream.of(d).anyMatch(x -> x == 65D);

4.如何将ArrayList<String>转换成String[]

        String[] arr = new String[list.size()];
        arr = list.toArray(arr);

5.如何将数组转换成列表ArrayList

        Integer[] values = { 1, 3, 7 };
        List<Integer> list = new ArrayList<Integer>(Arrays.asList(values));
        int[] ints = new int[] {1,2,3,4,5};
        List<Integer> list11 =Arrays.stream(ints).boxed().collect(Collectors.toList());

6.打印java数组

        String[] sarr = {"aa", "bb", "cc", "dd"};
        System.out.println(Arrays.toString(sarr));

        double[] darr = {3.4D,5.6D, 7.9D};
        System.out.println(Arrays.toString(darr));

        int[] iarr = {1,2,3,4};
        System.out.println(Arrays.toString(iarr));
        String[][] sArray = new String[][] {{"aa", "bb"}, {"cc", "dd"}};
        System.out.println(Arrays.deepToString(sArray));
        String[] sarr = {"aa", "bb", "cc", "dd"};
        Arrays.stream(sarr).forEach(System.out::println);
        int[][] int2DArray = new int[][] { {11, 12}, { 21, 22}, {31, 32, 33} };
        String[][] str2DArray = new String[][]{ {"aa", "bb"} , {"cc", "dd"}, {"ee", "ff", "gg"} };

        Arrays.stream(int2DArray).flatMapToInt(x -> Arrays.stream(x)).forEach(System.out::println);
        Arrays.stream(str2DArray).flatMap(x -> Arrays.stream(x)).forEach(System.out::println);

7.如何将Java 8 Stream转换为数组?

        Stream<String> stream = Stream.of("aa","bb","cc");

        IntFunction<String[]> intFunction = new IntFunction<String[]>() {
            @Override
            public String[] apply(int value) {
                return new String[value];
            }
        };

        String[] sarr = stream.toArray(intFunction);
        Stream<String> stream = Stream.of("aa","bb","cc");
        String[] sarr = stream.toArray(value -> new String[value]);

8.如何声明一个数组?

        int[] intArr1 = new int[5];//初始化空间
        int[] intArr2 = {1,2,3,4,5};
        int[] intArr3 = new int[]{1,2,3,4,5};

        int[] intArr4 = IntStream.range(0, 5).toArray();//数组长度为5
        int[] intArr5 = IntStream.rangeClosed(0, 5).toArray();//数组长度为6
        int[] intArr6 = IntStream.of(1,5,3,4,2).toArray();
        int[] intArr7 = IntStream.of(1,5,3,4,2).sorted().toArray();//返回排序好的数组


        //二维数组
        int[][] intArrs1 = new int[5][2];
        int[][] intArrs2 = {{1,2},{3,4},{5,6},{7,8},{9,10}};

        int[][] intArrs3 = new int[5][];
        int[][] intArrs4 = {{1},{2,3},{4,5,6},{7},{8}};
        int[][] intArrs5 = new int[][]{{1},{2,3},{4,5,6},{7},{8}};

9.java中将InputStream转换为字节数组byte[]

上一篇 下一篇

猜你喜欢

热点阅读