以下代码片都是 jdk1.8 ArrayList中的官方代码

/**
* Constructs a list containing the elements of the specified
* collection, in the order they are returned by the collection's
* iterator.
*
* @param c the collection whose elements are to be placed into this list
* @throws NullPointerException if the specified collection is null
*/
public ArrayList(Collection<? extends E> c) {
elementData = c.toArray();
if ((size = elementData.length) != 0) {
// c.toArray might (incorrectly) not return Object[] (see 6260652)
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, size, Object[].class);
} else {
// replace with empty array.
this.elementData = EMPTY_ELEMENTDATA;
}
}

解读:ArrayList的构造方法,用得比较少,至少我用得比较少。参数是Collection的实现类都行,

     由此我想到了一个好玩的东西,如果要将两个集合想加,那么可以试试这个方法,虽然官方提供了

public boolean addAll(int index, Collection<? extends E> c) 和public boolean addAll(Collection<? extends E> c) 这两个方法。

 /**
* Trims the capacity of this <tt>ArrayList</tt> instance to be the
* list's current size. An application can use this operation to minimize
* the storage of an <tt>ArrayList</tt> instance.
*/
public void trimToSize() {
modCount++;
if (size < elementData.length) {
elementData = (size == 0)
? EMPTY_ELEMENTDATA
: Arrays.copyOf(elementData, size);
}
}

解读:这是个好东西。
    ArrayList所说没有用的值并不是null,而是ArrayList每次容量不够用时申请的存储空间会稍稍多一些,1.5倍+1,
     这样就会出现当size() = 1000的时候,ArrayList已经申请了1200空间的情况  此时trimToSize
     的作用只是去掉预留元素位置,就是删除多余的200,官方指导的是可以用来优化存储

  /**
* Increases the capacity of this <tt>ArrayList</tt> instance, if
* necessary, to ensure that it can hold at least the number of elements
* specified by the minimum capacity argument.
*
* @param minCapacity the desired minimum capacity
*/
public void ensureCapacity(int minCapacity) {
int minExpand = (elementData != DEFAULTCAPACITY_EMPTY_ELEMENTDATA)
// any size if not default element table
? 0
// larger than default for default empty table. It's already
// supposed to be at default size.
: DEFAULT_CAPACITY; if (minCapacity > minExpand) {
ensureExplicitCapacity(minCapacity);
}
}

解读:这更是个好东西。因为官方都建议使用了,

    * <p>An application can increase the capacity of an <tt>ArrayList</tt> instance
    * before adding a large number of elements using the <tt>ensureCapacity</tt>
    * operation.  This may reduce the amount of incremental reallocation.

   简单来说就是业务情况预先设置集合的容量,这样能够大大提高初始化速度。可能有人说直接指定容量呢,实际情况中你哪会知道容量会多大,就算知道,那效率更是不敢看

   下面是测试代码,运行看看就知道了

package sourceCode.ArrayList;

import java.util.ArrayList;

/**
* ArrayList.ensureCapacity(N)性能测试
*
*/
public class entureCapacityTest { @SuppressWarnings({ "unchecked", "rawtypes" })
public static void main(String[] args) { final int N = 100000000;
Object obj = new Object(); // 1.没用调用ensureCapacity()方法初始化ArrayList对象
ArrayList list = new ArrayList();
long startTime = System.currentTimeMillis();
for (int i = 0; i <= N; i++) {
list.add(obj);
}
long endTime = System.currentTimeMillis();
System.out.println("没有调用ensureCapacity()方法所用时间:" + (endTime - startTime) + "ms"); // 2.调用ensureCapacity()方法初始化ArrayList对象
list = new ArrayList();
startTime = System.currentTimeMillis();
// 预先设置list的大小
list.ensureCapacity(N);
for (int i = 0; i <= N; i++) {
list.add(obj);
}
endTime = System.currentTimeMillis();
System.out.println("调用ensureCapacity()方法所用时间:" + (endTime - startTime) + "ms"); // 3.直接指定容量
list = new ArrayList(N);
startTime = System.currentTimeMillis();
for (int i = 0; i <= N; i++) {
list.add(obj);
}
endTime = System.currentTimeMillis();
System.out.println("直接设置容量所用时间:" + (endTime - startTime) + "ms"); }
}
  /**
* Retains only the elements in this list that are contained in the
* specified collection. In other words, removes from this list all
* of its elements that are not contained in the specified collection.
*
* @param c collection containing elements to be retained in this list
* @return {@code true} if this list changed as a result of the call
* @throws ClassCastException if the class of an element of this list
* is incompatible with the specified collection
* (<a href="Collection.html#optional-restrictions">optional</a>)
* @throws NullPointerException if this list contains a null element and the
* specified collection does not permit null elements
* (<a href="Collection.html#optional-restrictions">optional</a>),
* or if the specified collection is null
* @see Collection#contains(Object)
*/
public boolean retainAll(Collection<?> c) {
Objects.requireNonNull(c);
return batchRemove(c, true);
}

解读:取两个集合的交集

ArrayList 冷门方法的更多相关文章

  1. 编写测试类,了解ArrayList的方法

    这篇文章主要介绍了C#中动态数组用法,实例分析了C#中ArrayList实现动态数组的技巧,非常具有实用价值,需要的朋友可以参考下 本文实例讲述了C#中动态数组用法.分享给大家供大家参考.具体分析如下 ...

  2. 将java中数组转换为ArrayList的方法实例(包括ArrayList转数组)

    方法一:使用Arrays.asList()方法   1 2 String[] asset = {"equity", "stocks", "gold&q ...

  3. ArrayList.subList方法使用总结

    ArrayList.subList方法使用总结 示例 List<String> list=new ArrayList<>(); list.add("d"); ...

  4. 遍历Arraylist的方法:

    遍历Arraylist的几种方法: Iterator it1 = list.iterator();        while(it1.hasNext()){            System.out ...

  5. 遍历Arraylist的方法

    package com.test; import java.util.ArrayList; import java.util.Iterator; import java.util.List; publ ...

  6. ArrayList 进阶方法之ListIterator

    同样看的都是jdk1.8 中 ArrayList中的源码,整理测试一下而已ListIterator(int index)方法,返回指定下标(包含该下标)后的值,此时index位置的元素就是新列表迭代器 ...

  7. 集合Arraylist的方法的使用和打印

    package chapter090; import java.util.ArrayList;import java.util.List; public class TestList01 { publ ...

  8. Java ArrayList排序方法详解

    由于其功能性和灵活性,ArrayList是 Java 集合框架中使用最为普遍的集合类之一.ArrayList 是一种 List 实现,它的内部用一个动态数组来存储元素,因此 ArrayList 能够在 ...

  9. 关于ArrayList add()方法 中的引用问题

    ArrayList的add方法每次添加一个对象时,添加 的是一个对象的引用,比如进行循环操作10次  lists.add(a) 每次 a会改变 ,这时候你会发现你在lists里添加了10个相同的对象a ...

随机推荐

  1. 用Jquery做一个时间日期选择器

    今天我们就用Jquery做一个时间日期选择器,当打开网页时,文本框里面显示的是当前的日期,点击文本框可以出现年.月.日的下拉菜单,并且可以选择,会根据年份的选择判断是否是闰年,从而改变二月的天数,闰年 ...

  2. 剑指offer_数组中的逆序对

    题目描述 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数P. 并将P对1000000007取模的结果输出. 即输出P%100 ...

  3. C#与Java区别(一)

    最近学了点java,总结了一些和c#的语法区别,欢迎大家指点和补充,如下: 1.java支持跨平台,当然.net core现在也支持. 2.java中用package,c#中用namespace定义空 ...

  4. http的几种请求的方式(Get、Post、Put、Head、Delete、Options、Trace和Connect)

    http的这几种请求方式各有各的特点,适用于各自的环境.下面我就说说这些方式的各自特点: 1.Get:它的原理就是通过发送一个请求来取得服务器上的某一资源.获取到的资源是通过一组HTTP头和呈现数据来 ...

  5. 连接池 DBCP c3p0以及分页的案例

    1. 连接池 思考: 程序中连接如何管理? 连接资源宝贵:需要对连接管理 连接: a) 操作数据库,创建连接 b) 操作结束,  关闭! 分析: 涉及频繁的连接的打开.关闭,影响程序的运行效率! 连接 ...

  6. HTMLElement

    参考文档:MDN HTMLElement 一.继承关系 所有HTML元素都是由HTMLElement或者其更具体的子类型来表示的. HTMLElement继承自Element,并实现了GlobalEv ...

  7. CDN内容分发网络

    CDN的全称是Content Delivery Network,即内容分发网络,其设计思想是尽可能避开互联网上有可能影响数据传输速度和稳定性的瓶颈和环节,使内容传输的更快.更稳定. CDN系统是在网络 ...

  8. 使用idea2017搭建SSM框架

    搭建个SSM框架居然花费了我好长时间!特此记录! 需要准备的环境: idea 2017.1 jdk1.8 Maven 3.3.9  请提前将idea与Maven.jdk配置好,本次项目用的都是比较新的 ...

  9. jQuery ajax 与服务器交互方法

    1.HTML <table> <tr> <td>用户名:</td> <td><input type="text" ...

  10. C#中如何给PDF添加可见的数字签名

    数字签名广泛用于保护PDF文档,可见数字签名在日常生活中是相当重要的.在这篇文章中我将与大家分享如何给PDF文件添加可见的数字签名. 首先我下载了一个由E-iceblue公司开发的免费版的PDF组件- ...