- Arrays.asList(T....a)
- new ArrayList<>(Arrays.asList());
- Collections.addAll() ***
- Stream流
Arrays.asList(T....a)
String[] strArrays = new String[]{"This","is","a","Arrays"};
System.out.println("strArrays: "+Arrays.toString( strArrays));
List<String> strList = Arrays.asList(strArrays); // Arrays.asList() 返回值就是一个 list
System.out.println("strList: "+ strList);
输出:
strArrays: [This, is, a, Arrays]
strList: [This, is, a, Arrays]
注意:
在查看源码后可发现:
//Arrays.java
private static class ArrayList<E> extends AbstractList<E>
implements RandomAccess, java.io.Serializable
{
private static final long serialVersionUID = -2764017481108945198L;
private final E[] a; // 注意这里,private final 私有内部类,
ArrayList(E[] array) {
a = Objects.requireNonNull(array);
}
....
}
@SafeVarargs
@SuppressWarnings("varargs")
public static <T> List<T> asList(T... a) {
return new ArrayList<>(a);
}
发现:
- 返回值 a 被pricate final修饰
- a直接指向array数组所在位置
思考:
Arrays.asList() 的返回值a被private final进行修饰,意味着什么:
- private:外部无法获取a,即无法对属性进行修改
- final:a的地址不可变。
- a会是一个指向堆区的对象数组(泛型)。指向的地址不可变,即意味着该数组尺寸固定,不可进行扩容。
意义:
- 方法返回的 list 不支持,remove(),add()等修改列表长度的操作
- 方法返回的仅仅只是一个视图,无论是array本身的变化,还是list进行的修改,都会对彼此的产生影响。
- 作为方法的参数,T....a 是个泛型。因此不支持基本数据操作。
验证2
//
strArrays[1] = "not";
strList.set(2,"realArray") ;
System.out.println("After changing:");
System.out.println("strArrays: "+Arrays.toString( strArrays));
System.out.println("strList: "+ strList);
new ArrayList<>(Arrays.asList());
String[] strArrays = new String[]{"This","is","a","static","Arrays"};
System.out.println("strArrays: "+Arrays.toString( strArrays));
List<String> strList = new ArrayList<>(Arrays.asList(strArrays));
System.out.println("strList: "+ strList);
直接调用了 ArrayList() 的构造方法,创建了一个新的 list ,与第一种方法相比较,可以进行任意的扩容,
strList.add(3,"not");
System.out.println("strArrays: "+Arrays.toString( strArrays));
System.out.println("strList: "+ strList);
刚开始好奇,为什么入参还需要使用 Arrays.asList() 方法。看了一下发现,ArrayList 的构造方法,不支持数组类型的参数
Collections.addAll()
String[] strArrays = new String[]{"This","is","a","static","Arrays"};
List<String> strList = new ArrayList<>(strArrays.length);
Collections.addAll(strList, strArrays);
System.out.println("strList: " + strList);
-
Collections.addAll()源码注释
Adds all of the specified elements to the specified collection. Elements to be added may be specified individually or as an array. The behavior of this convenience method is identical to that of c.addAll(Arrays.asList(elements)), but this method is likely to run significantly faster under most implementations.
这种方法要比其他任何方法都要有更快的速度,是最为高效的。代码上没看出什么意思,查了一下貌似是:将数组中的元素转为二进制,然后添加入List中,enmmm,既然都二进制了,快就快把
流
目前java8 仅支持int、long、double类型的数组
List<Integer> intList= Arrays.stream(new int[] { 1, 2, 3, }).boxed().collect(Collectors.toList());
List<Long> longList= Arrays.stream(new long[] { 1, 2, 3 }).boxed().collect(Collectors.toList());
List<Double> doubleList= Arrays.stream(new double[] { 1, 2, 3 }).boxed().collect(Collectors.toList());
不大懂流,还没怎么么学。
/**
* 创建一个长度适合的流?
* Returns a sequential {@link IntStream} with the specified array as its
* source.
*
* @param array the array, assumed to be unmodified during use
* @return an {@code IntStream} for the array
* @since 1.8
*/
public static IntStream stream(int[] array) {
return stream(array, 0, array.length);
}
/**
* 将基本数据类型转化为包装类
* Returns a {@code Stream} consisting of the elements of this stream,
* each boxed to an {@code Integer}.
*
* <p>This is an <a href="package-summary.html#StreamOps">intermediate
* operation</a>.
*
* @return a {@code Stream} consistent of the elements of this stream,
* each boxed to an {@code Integer}
*/
Stream<Integer> boxed();
/**
* Performs a <a href="package-summary.html#MutableReduction">mutable
* reduction</a> operation on the elements of this stream using a
* {@code Collector}. A {@code Collector}
* encapsulates the functions used as arguments to
* {@link #collect(Supplier, BiConsumer, BiConsumer)}, allowing for reuse of
* collection strategies and composition of collect operations such as
* multiple-level grouping or partitioning.
*
* <p>If the stream is parallel, and the {@code Collector}
* is {@link Collector.Characteristics#CONCURRENT concurrent}, and
* either the stream is unordered or the collector is
* {@link Collector.Characteristics#UNORDERED unordered},
* then a concurrent reduction will be performed (see {@link Collector} for
* details on concurrent reduction.)
*
* <p>This is a <a href="package-summary.html#StreamOps">terminal
* operation</a>.
*
* <p>When executed in parallel, multiple intermediate results may be
* instantiated, populated, and merged so as to maintain isolation of
* mutable data structures. Therefore, even when executed in parallel
* with non-thread-safe data structures (such as {@code ArrayList}), no
* additional synchronization is needed for a parallel reduction.
*
* @apiNote
* The following will accumulate strings into an ArrayList:
* <pre>{@code
* List<String> asList = stringStream.collect(Collectors.toList());
* }</pre>
*
* <p>The following will classify {@code Person} objects by city:
* <pre>{@code
* Map<String, List<Person>> peopleByCity
* = personStream.collect(Collectors.groupingBy(Person::getCity));
* }</pre>
*
* <p>The following will classify {@code Person} objects by state and city,
* cascading two {@code Collector}s together:
* <pre>{@code
* Map<String, Map<String, List<Person>>> peopleByStateAndCity
* = personStream.collect(Collectors.groupingBy(Person::getState,
* Collectors.groupingBy(Person::getCity)));
* }</pre>
*
* @param <R> the type of the result
* @param <A> the intermediate accumulation type of the {@code Collector}
* @param collector the {@code Collector} describing the reduction
* @return the result of the reduction
* @see #collect(Supplier, BiConsumer, BiConsumer)
* @see Collectors
*/
没看懂,大体意思应该是将流转化为 list 进行返回吧
<R, A> R collect(Collector<? super T, A, R> collector);
标签:code,数组,Arrays,list,strArrays,strList,array,asList
From: https://www.cnblogs.com/serendipity-igao/p/16819490.html