Arrays.asList的那点事
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List; public class ArraysAsListDemo {
public static void main(String[] args) {
List<String> source1 = new ArrayList<String>(Arrays.asList("1", "2", "3"));
ArrayList<String> source2 = new ArrayList<String>(Arrays.asList("5", "62", "3"));
source2.removeAll(source1);
System.out.println(source2);
// Arrays.asList("1", "2", "3").remove("1");//会报错。因为Arrays$ArrayList中没有实现List接口中的remove方法 System.out.println(source1.containsAll(source2));
HashSet<String> total = new HashSet<String>();
total.addAll(source1);
total.addAll(source2);
System.out.println(total.size() > (source1.size() + source2.size()));
System.out.println(total.size() < (source1.size() + source2.size()));
} }
// Misc /**
* Returns a fixed-size list backed by the specified array. (Changes to
* the returned list "write through" to the array.) This method acts
* as bridge between array-based and collection-based APIs, in
* combination with {@link Collection#toArray}. The returned list is
* serializable and implements {@link RandomAccess}.
*
* <p>This method also provides a convenient way to create a fixed-size
* list initialized to contain several elements:
* <pre>
* List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");
* </pre>
*
* @param a the array by which the list will be backed
* @return a list view of the specified array
*/
public static <T> List<T> asList(T... a) {
return new ArrayList<T>(a);
} /**
* @serial include
*/
private static class ArrayList<E> extends AbstractList<E>
implements RandomAccess, java.io.Serializable
{
private static final long serialVersionUID = -2764017481108945198L;
private final E[] a; ArrayList(E[] array) {
if (array==null)
throw new NullPointerException();
a = array;
}
说明:
在jdk文档中对RandomAccess接口的定义如下:
public interface RandomAccess
List 实现所使用的标记接口,用来表明其支持快速(通常是固定时间)随机访问。
此接口的主要目的是允许一般的算法更改其行为,从而在将其应用到随机或连续访问列表时能提供良好的性能。
将操作随机访问列表的最佳算法(如 ArrayList )应用到连续访问列表(如 LinkedList )时,可产生二次项的行为。
如果将某个算法应用到连续访问列表,那么在应用可能提供较差性能的算法前,鼓励使用一般的列表算法检查给定列表是否为此接口的一个 instanceof ,
如果需要保证可接受的性能,还可以更改其行为。现在已经认识到,随机和连续访问之间的区别通常是模糊的。
例如,如果列表很大时,某些 List 实现提供渐进的线性访问时间,但实际上是固定的访问时间。这样的 List 实现通常应该实现此接口。
强调:
JDK中推荐的是对List集合尽量要实现RandomAccess接口
如果集合类是RandomAccess的实现,则尽量用for(int i = 0; i < size; i++) 来遍历而不要用Iterator迭代器来遍历,在效率上要差一些。
反过来,如果List是Sequence List,则最好用迭代器来进行迭代。
package java.util;
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{
private static final long serialVersionUID = 8683452581122892189L; /**
* The array buffer into which the elements of the ArrayList are stored.
* The capacity of the ArrayList is the length of this array buffer.
*/
private transient Object[] elementData; /**
* The size of the ArrayList (the number of elements it contains).
*
* @serial
*/
private int size;
public interface List<E> extends Collection<E> {
// Query Operations /**
* Returns the number of elements in this list. If this list contains
* more than <tt>Integer.MAX_VALUE</tt> elements, returns
* <tt>Integer.MAX_VALUE</tt>.
*
* @return the number of elements in this list
*/
int size(); /**
* Returns <tt>true</tt> if this list contains no elements.
*
* @return <tt>true</tt> if this list contains no elements
*/
boolean isEmpty();
* @author Josh Bloch
* @author Neal Gafter
* @version 1.55, 04/21/06
* @see Set
* @see List
* @see Map
* @see SortedSet
* @see SortedMap
* @see HashSet
* @see TreeSet
* @see ArrayList
* @see LinkedList
* @see Vector
* @see Collections
* @see Arrays
* @see AbstractCollection
* @since 1.2
*/ public interface Collection<E> extends Iterable<E> {
// Query Operations /**
* Returns the number of elements in this collection. If this collection
* contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
* <tt>Integer.MAX_VALUE</tt>.
*
* @return the number of elements in this collection
*/
int size(); /**
* Returns <tt>true</tt> if this collection contains no elements.
*
* @return <tt>true</tt> if this collection contains no elements
*/
boolean isEmpty();
Arrays.asList的那点事的更多相关文章
- 【转】java.util.Arrays.asList 的用法
DK 1.4对java.util.Arrays.asList的定义,函数参数是Object[].所以,在1.4中asList()并不支持基本类型的数组作参数. JDK 1.5中,java.util.A ...
- Arrays.asList()注意
api: public static <T> List<T> asList(T... a) 返回一个受指定数组支持的固定大小的列表.(对返回列表的更改会“直接写”到数组.)此方 ...
- Arrays.toString Arrays.asList
import java.util.Arrays; public class TestCalc{ public static void main(String[] args) { ,,,,,,,}; / ...
- Arrays.asList()使用注意点
今天看代码时, 发现书上使用了Arrays.asList()方法, 将一个数组转成了List, 然后说到得到的List不能调用add(), remove()方法添加元素或者删除,带着疑问看了下内部实现 ...
- Arrays.asList(数组) 解说
最近在用Arrays的asList()生成的List时,List元素的个数时而不正确. Java代码 一:Arrays.asList(数组)该方法是将数组转化为集合(该方法主要用于Object对象数组 ...
- Arrays.asList方法总结
import java.util.Arrays; import java.util.List; /** * * 本类演示了Arrays类中的asList方法 * 通过四个段落来演示,体现出了该方法的相 ...
- Arrays.asList的使用及异常问题
将数组转成List问题,通常我们习惯这样写成:List<String> list = Arrays.asList("1","2"); 于是我们这样就 ...
- Arrays.asList引起的惨案
最近代码中需要对两个数组求交,想当然便用到了List中的retainAll函数,但要将将数组转换成list.代码如下: String[] abc = new String[] { "abc& ...
- Arrays.asList的源码分析
以前一直很奇怪为什么Arrays.asList的数组不能插入新的数据,后来看了源码发现是因为内部是一个final的数组支持起来的Arraylist,下面贴入源码与分析. 1.先看Arrays的方法 我 ...
随机推荐
- MySQL在大数据Limit使用
它已被用于Oracle一世.但今天,很惊讶,MySQL在对数量级的性能,甚至差距如此之大不同的顺序相同的功能. 看看表ibmng(id,title,info) 只要 id key 指数title ...
- pthread_t定义结构
linux下被定义为: 在linux履行pthread_t它被定义为 "unsigned long int",参考这里 Windows下这样定义: /* * Generic han ...
- javascript于boolean类型转换,运营商&&和|| 返回值
javascript它是弱类型语言,不管是什么类型的数据可以被转换成boolean种类.转换规则如下面的: 数据类型 转换为boolean后的值 NAN ...
- Java Metrics
Java Metrics Java Metrics是一个功能比較强大的java统计库,它的输出组件也非常强大,帮我们做好了: 输出到Ganglia 输出到控制台 输出到JMX 输出Json 具体见:d ...
- 插入排序java
插入排序简述 插入排序的基本操作就是将一个数据插入到已经排好序的有序数据中,从而得到一个新的.个数加一的有序数据. 第一个元素是有序队列,从第二个元素开始向有序队列中插入,插入完成后将第三个元素向 ...
- Java栈的实例-数组和链表两种方法(转)
一.栈 栈的定义 栈(Stack)是限制仅在表的一端进行插入和删除运算的线性表. (1)通常称插入.删除的这一端为栈顶 (Top),另一端称为栈底 (Bottom). (2)当表中没有元素时称为空栈. ...
- 关与 Visual.Assist.X.V10.7.1912的Crack破解补丁(vs 番茄插件的key破解方法)
在win7系统下, 我用的是vs2012版本号. Visual Assist沿用了快10年的界面,最终有了更新,变得更加适合Win8 以及 VS2012的主题风格了 ,这也是以后软件的发展趋势,仅仅是 ...
- iis10 HTTP 错误 500.19 - Internal Server Error
HTTP 错误 500.19 - Internal Server Error 无法访问请求的页面,因为该页的相关配置数据无效. 详细错误信息: 模块 IIS Web Core 通知 未知 ...
- gif动图快速制作方法(附工具)(转)
现在写博客或是wiki的过程中,会经常引用到图片,特别是客户端经常与页面相关所以截图不可避.但是越来越多的效果仅仅一张图片是无法清楚的描述.并且博客或是wiki也是支持gif图的.gif图的制作方法有 ...
- android对app代码混淆
接到新任务.现有项目的代码混淆.在此之前混淆了一些理解,但还不够具体和全面,我知道有些东西混起来相当棘手. 但幸运的是,现在这个项目是不是太复杂(对于这有些混乱).提前完成--这是总结. 第一部分 介 ...