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的方法 我 ...
随机推荐
- Visual Studio Tips: How to change project namespace
/* Author: Jiangong SUN */ If you want to modify a project's namespace and its physical container na ...
- SQL Server :理解BCM页
原文:SQL Server :理解BCM页 今天我们来讨论下批量更改映射(Bulk Changed Map:BCM)页,还有大容量日志恢复模式( bulk logged recovery model ...
- Java EE (7) -- Java EE 6 Enterprise Architect Certified Master(1z0-807)
Application Design Concepts and Principles Identify the effects of an object-oriented approach to sy ...
- WPF 3D:简单的Point3D和Vector3D动画创造一个旋转的正方体
原文:WPF 3D:简单的Point3D和Vector3D动画创造一个旋转的正方体 运行结果: 事实上很简单,定义好一个正方体,处理好纹理.关于MeshGeometry3D的正确定义和纹理这里就不多讲 ...
- A Mathematical Curiosity
A Mathematical Curiosity Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/O ...
- UVA 10163 Storage Keepers(两次DP)
UVA 10163 Storage Keepers(两次DP) http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Ite ...
- Windows下效率必备软件
AutoHotKey: 神器!神器!神器!当然也得看使用者咯(^__^) 嘻嘻…… Listary: 本地搜索神器,当然还有别的作用,More&More. Launchy : 快速启动安装的应 ...
- 构建安全的Xml Web Service系列之SSL篇
原文:构建安全的Xml Web Service系列之SSL篇 首先介绍一下SSL, SSL 的英文全称是 "Secure Sockets Layer" ,中文名为 "安全 ...
- HDU 3829 Cat VS Dog
题意: p个人 每一个人有喜欢和讨厌的动物 假设选出的动物中包括这个人喜欢的动物同一时候不包括他讨厌的动物那么这个人会开心 问 最多几个人开心 思路: 二分图最大独立集 利用人与人之间的冲突 ...
- Linux下 高性能、易用、免费的ASP.NET服务器
Linux下 高性能.易用.免费的ASP.NET服务器 http://www.jexus.org/#