常用方法
List partition
《集合 List 分片的 5 种方法》
Array to List
方法一:转两次
1
| List<String> list = new ArrayList<>(Arrays.asList("a", "b", "c"))
|
方法二:Java 8
1 2 3 4 5 6
| Integer [] myArray = { 1, 2, 3 }; List<Integer> myList = Arrays.stream(myArray).collect(Collectors.toList());
int [] myArray2 = { 1, 2, 3 }; List<Integer> myList2 = Arrays.stream(myArray2).boxed().collect(Collectors.toList());
|
方法三:Guava
1 2 3 4 5 6 7 8 9
| List<String> il = ImmutableList.of("string", "elements"); List<String> i2 = ImmutableList.copyOf(new String[]{"string", "elements"});
List<String> l0 = Lists.newLinkedList(Arrays.asList("a", "b", "c")); List<String> l1 = Lists.newArrayList(Arrays.asList("a", "b", "c")); List<String> l2 = Lists.newArrayList(new String[]{"string", "elements"}); List<String> l3 = Lists.newArrayList("or", "string", "elements");
|
参考
《Arrays.asList() 原来是这样用的》
《千万不要这样使用 Arrays.asList !》