1.比较数组和List谁更快?
- 就添加和遍历来讲,数组运行速度确实比List更快,但是数据量不大的情况下两者几乎无差别。数量级在百万级别以上,数组确实比List要快上不少。
- List在操作上比数组更加灵活。
2.如何将数组转换成ArrayList?
String[] sArr = new String[]{"aa", "bb", "cc", "dd"};
ArrayList<String> list = new ArrayList<>(Arrays.asList(sArr));
3.检查数组是否含有某个值
- 使用ArrayList方法
String[] values = {"AB","BC","CD","AE"};
boolean contains = Arrays.asList(values).contains("CD");
- 使用apache utils相关类
String[] fieldsToInclude = { "id", "name", "location" };
boolean contains = ArrayUtils.contains( fieldsToInclude, "name" );
- java8下可以使用Stream
String[] values = {"AB","BC","CD","AE"};
boolean contains = Arrays.stream(values).anyMatch("CD"::equals);
- 数组类型为int,long,double等基本类型
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
- 使用JDK
Integer[] values = { 1, 3, 7 };
List<Integer> list = new ArrayList<Integer>(Arrays.asList(values));
- JDK1.8下使用流
int[] ints = new int[] {1,2,3,4,5};
List<Integer> list11 =Arrays.stream(ints).boxed().collect(Collectors.toList());
6.打印java数组
- 使用JDK工具类
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));
- JDK1.8使用lambda
String[] sarr = {"aa", "bb", "cc", "dd"};
Arrays.stream(sarr).forEach(System.out::println);
- JDK1.8使用lambda打印多维数组
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转换为数组?
- 使用IntFunction功能接口
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);
- 使用lambda表达式
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[]
-
使用apache工具类
byte[] bytes = IOUtils.toByteArray(inputStream);
-
使用JDK ByteArrayOutputStream先写入在转换
ByteArrayOutputStream buffer = new ByteArrayOutputStream(); byte[] data = new byte[2048]; for (int len; (len = inputStream.read(data, 0, data.length)) > 0; ) { buffer.write(data, 0, len); } byte[] bytes = buffer.toByteArray();
-
使用java9简单操作
byte[] array = inputStream.readAllBytes();
-
使用google guava
byte[] bytes = ByteStreams.toByteArray(inputStream);
-
使用spring框架(spring-core)
byte[] bytes = StreamUtils.copyToByteArray(inputStream);
网友评论