今天学习一下集合包里面的内容,常见的有Collection和Map两个接口的实现类
Collection中常见的又分为两种:

1.List ,支持放入重复的对象,实现类有arraylist,linkedlist,vector,stack
           2.Set ,不支持放入重复对象,hashset,treeset
ArrayList:
    创建arraylist:提供了三种构造方式。

public ArrayList(int initialCapacity) {
if (initialCapacity > 0) {
this.elementData = new Object[initialCapacity];//大于0时就直接创建object数组
} else if (initialCapacity == 0) {
this.elementData = EMPTY_ELEMENTDATA;//为0时构造一个空的
} else {
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
}
}

当插入的数据大于它的容量时就会扩容,过程如下:

  public static void main(String[] args) {
List list = new ArrayList(2);
list.add(1);
list.add(2);
list.add(3);
}

上面定义了长度为2,但存放了三个,调用过程是这样的,在第三次时决断minCapacity-elementData.length>0就调用grow法。源码如下:

//添加
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
} private void ensureCapacityInternal(int minCapacity) {
ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));
} private static int calculateCapacity(Object[] elementData, int minCapacity) {
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
return Math.max(DEFAULT_CAPACITY, minCapacity);
}
return minCapacity;
} private void ensureExplicitCapacity(int minCapacity) {
modCount++; // overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
} private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
// minCapacity is usually close to size, so this is a win:
elementData = Arrays.copyOf(elementData, newCapacity);
}

删除元素:

public E remove(int index) {
rangeCheck(index); modCount++;
E oldValue = elementData(index); int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--size] = null; // clear to let GC do its work return oldValue;
} public boolean remove(Object o) {
if (o == null) {
for (int index = 0; index < size; index++)
if (elementData[index] == null) {
fastRemove(index);
return true;
}
} else {
for (int index = 0; index < size; index++)
if (o.equals(elementData[index])) {
fastRemove(index);
return true;
}
}
return false;
} private void fastRemove(int index) {
modCount++;
int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--size] = null; // clear to let GC do its work
}

遍历时:

public Iterator<E> iterator() {
return new Itr();
} int cursor; // index of next element to return
int lastRet = -1; // index of last element returned; -1 if no such
int expectedModCount = modCount; Itr() {} public boolean hasNext() {
return cursor != size;
} @SuppressWarnings("unchecked")
public E next() {
checkForComodification();
int i = cursor;
if (i >= size)
throw new NoSuchElementException();
Object[] elementData = ArrayList.this.elementData;
if (i >= elementData.length)
throw new ConcurrentModificationException();
cursor = i + 1;
return (E) elementData[lastRet = i];
} public void remove() {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification(); try {
ArrayList.this.remove(lastRet);
cursor = lastRet;
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();//这个异常说明在操作元素时,有其他线程对list进行了改变
}
} @Override
@SuppressWarnings("unchecked")
public void forEachRemaining(Consumer<? super E> consumer) {
Objects.requireNonNull(consumer);
final int size = ArrayList.this.size;
int i = cursor;
if (i >= size) {
return;
}
final Object[] elementData = ArrayList.this.elementData;
if (i >= elementData.length) {
throw new ConcurrentModificationException();
}
while (i != size && modCount == expectedModCount) {
consumer.accept((E) elementData[i++]);
}
// update once at end of iteration to reduce heap write traffic
cursor = i;
lastRet = i - 1;
checkForComodification();
} final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}

总结:

ArrayList基于数组方式实现,无容量限制,在插入元素时可能会扩容,在删除元素时,容量大小并不会改变,但可以通过trimToSize()来修改。在查找元素时要遍历数组,对于非null的元素,采取equals的方式查找,最后一点arrayList是非线程安全的。

LinkedList也是非线程安全的,它基于双向链表机制实现,插入元素时要新建一个Entry对象(1.8好像是Node,没骨找到Entry对象)。

Vector是基于Synchronized实现的线程安全的ArrayList,但在插入元素时容量扩充的机制和ArrayList稍有不同,并可通过capacityIncrement来控制容量的扩充。

Stack继承Vector,实现后进先出(LIFO)的弹出及压入操作,提供了push,pop,peek三个主要方法。

HashSet:

hashset是set接口的实现,set 和list区别就是set不允许有元素重复(不重复是底层用hashmap),

总结:

HashSet基于HashMap,无容量限制,HashSet是非线程安全的。

集合类中的Collection接口实现类的更多相关文章

  1. 『Java』Collection接口 Collections类

    接口Collection public interface Collection<E>定义了所有单列集合中共性的方法,所有的单列集合都可以使用共性方法. Collection的常用子接口有 ...

  2. JavaWeb学习之JDBC API中常用的接口和类

    JDBC API中包含四个常用的接口和一个类分别是: 1.Connection接口 2.Statement接口 3.PreparedStatement接口 4.ResultSet接口 5.Driver ...

  3. Java中的List接口实现类LinkedList

    package collection; import java.util.LinkedList; /* * 1.implement List接口 * 2.底层是一个链表结构:查询慢,增删快 * 注意: ...

  4. 集合框架二(Collection接口实现类常用遍历方法)

    四种常用遍历方式 Collection coll = new ArrayList(); coll.add("123"); coll.add("456"); co ...

  5. JAVA中Collection接口和Map接口的主要实现类

    Collection接口 Collection是最基本的集合接口,一个Collection代表一组Object,即Collection的元素(Elements).一些Collection允许相同的元素 ...

  6. JAVA 中的 Collection 和 Map 以及相关派生类的概念

    JAVA中Collection接口和Map接口的主要实现类   Collection接口 Collection是最基本的集合接口,一个Collection代表一组Object,即Collection的 ...

  7. 牛客网Java刷题知识点之Java 集合框架的构成、集合框架中的迭代器Iterator、集合框架中的集合接口Collection(List和Set)、集合框架中的Map集合

    不多说,直接上干货! 集合框架中包含了大量集合接口.这些接口的实现类和操作它们的算法. 集合容器因为内部的数据结构不同,有多种具体容器. 不断的向上抽取,就形成了集合框架. Map是一次添加一对元素. ...

  8. 16、Collection接口及其子接口Set和List(常用类LinkedList,ArrayList,Vector和Stack)

    16.Collection接口 Collection是最基本的集合接口,一个Collection代表一组Object,即Collection的元素(Elements).一些Collection允许相同 ...

  9. 【集合系列】- 深入浅出分析Collection中的List接口

    一.List简介 List 的数据结构就是一个序列,存储内容时直接在内存中开辟一块连续的空间,然后将空间地址与索引对应. 以下是List集合简易架构图 由图中的继承关系,可以知道,ArrayList. ...

随机推荐

  1. 【HDOJ6611】K Subsequence(费用流)

    题意:给定一个长为n的正整数序列,要求从中取出至多k个不下降序列,使得它们的和最大,求这个和 n<=2e3,k<=10,a[i]<=1e5 思路:极其考验模板,反正我的spfa和zk ...

  2. moment.js 时间库

    一.概念:    https://www.cnblogs.com/Jimc/p/10591580.html    或    http://momentjs.cn/(官网) 1.Moment.js是一个 ...

  3. Redis的高级特性一览

    更多内容,欢迎关注微信公众号:全菜工程师小辉.公众号回复关键词,领取免费学习资料. 应用场景 缓存系统:用于缓解数据库的高并发压力 计数器:使用Redis原子操作,用于社交网络的转发数,评论数,粉丝数 ...

  4. python中模块介绍

    一,模块概念 在计算机程序开发的过程当中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护.为了编码更加容易维护,我们把很多函数分组,分别放到不同的文件里,这样,每个文件包含的代码 ...

  5. datatbales 使用笔记

    实例: var datatable_obj = null; $(document).ready(function(){ datatable_obj = $('#merchant-list').Data ...

  6. Linux系统CPU占用率较高问题排查思路

    作为 Linux 运维工程师,在日常工作中我们会遇到 Linux服务器上出现CPU负载达到100%居高不下的情况,如果CPU 持续跑高,则会影响业务系统的正常运行,带来企业损失. 很多运维的同学遇到这 ...

  7. 18. Jmeter-取样器二

    jmeter-sampler介绍与使用 JMS Point-to-Point JMS Publisher JMS Subscriber JSR223 Sampler JUnit Request Jav ...

  8. git使用记录三:查看日志

    git使用记录三: git log git log 的帮助文档 git log --help 查看最后面的两个日志记录 命令如下: git log -n number 比如: git log -n 2 ...

  9. java File I/O

    File类: 常用方法: boolean exists( ):判断文件或目录是否存在 boolean isFile( ):判断是否是文件 boolean isDirectory( ):判断是否是目录 ...

  10. 一文搞懂--Java中重写equals方法为什么要重写hashcode方法?

    Java中重写equals方法为什么要重写hashcode方法? 直接看下面的例子: 首先我们只重写equals()方法 public class Test { public static void ...