除了Java collection class/interface外,方便的有Google guava的utility class: Lists/Sets/Maps/Queues, 用它们可以方便地创建List等object。

List<String> list = Lists.newArrayList(); or Lists.newArrayList("1", "2");

ArrayList v.s. Vector:

两者都是implement了List interface,

ArrayList: Resizable-array implementation of the List interface.

Vector: The Vector class implements a growable array of objects.

两者的区别是: Vector synchronizes on each individual operation, 所以会很慢,最好用ArrayList。

ArrayList v.s. LinkedList:

LinkedList implements it with a doubly-linked list. ArrayList implements it with a dynamically resizing array.

LinkedList<E> allows for constant-time insertions or removals using iterators, but only sequential access of elements.

ArrayList<E>, on the other hand, allow fast random read access, so you can grab any element in constant time. But adding or removing from anywhere but the end requires shifting all the latter elements over, either to make an opening or fill the gap.

ConcurrentHashMap v.s. HashMap v.s. Hashtable:

三者都是implement了Map interface,

ConcurrentHashMap: thread-safe, Instead of a map wide lock, the collection maintains a list of 16 locks by default, each of which is used to guard (or lock on) a single bucket of the map. This effectively means that 16 threads can modify the collection at a single time (as long as they’re all working on different buckets). http://www.codercorp.com/blog/java/why-concurrenthashmap-is-better-than-hashtable-and-just-as-good-hashmap.html

HashMap: allows null key/value, not thread-safe. LinkedHashMap reserves the inserting order. HashMap methods include: containsKey(), containsValue(), remove(key), put(key, value), isEmpty(), size(). Here put() will update old value.

Iterate HashMap: Set<K> keySet(); Collection<V> values(); Set<Map.Entry<K,V>> entrySet(); You can use for loop on each's result. Or you can also use .iterator().

Hashtable: not allows null key/value, thread-safe, Hashtable’s offer concurrent access to their entries, with a small caveat, the entire map is locked to perform any sort of operation.

Collections.synchronizedHashMap(): use very simple synchronization, which means that only one thread can access the map at the same time.if you need to ensure data consistency, and each thread needs to have an up-to-date view of the map. While, Use the ConcurrentHashMap if performance is critical, and each thread only inserts data to the map, with reads happening less frequently.

复杂度HashMap: 理想情况下是O(1), 但如果出现所有key都产生了同样的hash的情况,那就得iterate所有elements来找到目标O(n).

Parent interface of Collection: Iterable Interface

A class that implements the Iterable can be used with the new for-loop.

The Iterable interface has only one method:

public interface Iterable<T> {
public Iterator<T> iterator();
}

It is possible to use your own collection type classes with the new for-loop. To do so, your class must implement thejava.lang.Iterable<E> interface. Here is a very basic example:

public class MyCollection<E> implements Iterable<E>{

    public Iterator<E> iterator() {
return new MyIterator<E>();
}
}

And here is the corresponding implementation skeleton of the MyIterator class:

public class MyIterator <T> implements Iterator<T> {

    public boolean hasNext() {

        //implement...
} public T next() {
//implement...;
} public void remove() {
//implement... if supported.
}
} 迭代器应用:
 list l = new ArrayList();
 l.add("aa");
 l.add("bb");
 l.add("cc");
 for (Iterator iter = l.iterator(); iter.hasNext();) {
  String str = (String)iter.next();
  System.out.println(str);
 }

 TreeSet & TreeMap

Both are sorted java data structures. Implementation is Red-Black tree. Time complexity for search/insert is O(logn).

HashSet: methods include add(), remove(), contains(), size(), isEmpty(). Here add() will return false if you are trying to add the same element.

												

[Java Basics] Collection的更多相关文章

  1. 模拟java.util.Collection一些简单的用法

    /* 需求:模拟java.util.Collection一些简单的用法! 注意:java虚拟机中并没有泛型类型的对象.泛型是通过编译器执行一个被称为类型擦除的前段转换来实现的. 1)用泛型的原生类型替 ...

  2. Java中Collection和Collections的区别(引用自:http://www.cnblogs.com/dashi/p/3597937.html)

      1.java.util.Collection 是一个集合接口(集合类的一个顶级接口).它提供了对集合对象进行基本操作的通用接口方法.Collection接口在Java 类库中有很多具体的实现.Co ...

  3. java中Collection类及其子类

    1:对象数组(掌握) (1)数组既可以存储基本数据类型,也可以存储引用类型.它存储引用类型的时候的数组就叫对象数组. 2:集合(Collection)(掌握) (1)集合的由来? 我们学习的是Java ...

  4. java 15-2 Collection的高级功能测试

    A:  boolean addAll(Collection c):添加一个集合的元素 ,所有的元素,无论是否重复 B:  boolean removeAll(Collection c):移除一个集合的 ...

  5. java 15-1 Collection集合的概述以及小功能介绍

     集合的由来:  我们学习的是面向对象语言,而面向对象语言对事物的描述是通过对象体现的,为了方便对多个对象进行操作,我们就必须把这多个对象进行存储.  而要想存储多个对象,就不能是一个基本的变量,而应 ...

  6. java集合——Collection接口

    Collection是Set,List接口的父类接口,用于存储集合类型的数据. 2.方法 int size():返回集合的长度 void clear():清除集合里的所有元素,将集合长度变为0 Ite ...

  7. [翻译]Java垃圾收集精粹(Java Garbage Collection Distilled)

    source URL: http://www.infoq.com/articles/Java_Garbage_Collection_Distilled Name: Java Garbage Colle ...

  8. Java API ——Collection集合类 & Iterator接口

    对象数组举例: 学生类: package itcast01; /** * Created by gao on 15-12-9. */ public class Student { private St ...

  9. 源码(03) -- java.util.Collection<E>

     java.util.Collection<E> 源码分析(JDK1.7) -------------------------------------------------------- ...

随机推荐

  1. 使用IOS7原生API进行二维码条形码的扫描

    使用IOS7原生API进行二维码条形码的扫描 IOS7之前,开发者进行扫码编程时,一般会借助第三方库.常用的是ZBarSDK,IOS7之后,系统的AVMetadataObject类中,为我们提供了解析 ...

  2. 统计单词个数及词频(C++实现)

    #include<iostream> #include<fstream> #include<string> using namespace std; struct ...

  3. 《BI那点儿事》数据流转换——OLE DB 命令转换

    OLE DB命令对数据流中的数据行执行一个OLE DB命令.它针对数据表中的每一行进行更新操作,可以事先将要更新的数据存放在表中.或者针对一个有输入参数的存储过程,可以将这些参数存放在一个数据表中,不 ...

  4. phonegap 3.3教程 地理信息api教程

    一 准备工作 phonegap3.3的地理信息教程.从零开始,首先要新建一个项目从命令行启动 可以看到这是默认的生成的www目录,在这个目录里是最原始的html文件,编译的时候在根据这里的文件生成an ...

  5. HDU-4531 吉哥系列故事——乾坤大挪移 模拟

    题意:给定一个九宫格,然后能够选择某行或者是某列滚动,每个小方格分为上下左右四个块,每个块可以涂上4种不同的颜色.问最少使用多少步能够使得所有相同颜色相互联通. 分析:由于九宫格的所有的状态只有9!( ...

  6. hibernate hibernate.cfg.xml component 组件

    1.为什么使用component组件? 当一个表的列数目比较多时,可以根据属性分类,将一个java对象拆分为几个对象. 数据库还是一张表,不过有多个对象与之对应. 2.实例 2.1 Java 对象: ...

  7. 腾讯云TDSQL审计原理揭秘

    版权声明:本文由孙勇福原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/244 来源:腾云阁 https://www.qclo ...

  8. linux 真·随笔

    复制操作 命令行模式下输入 6,9 co 12 复制第6行到第9行之间的内容到第12行后面. vim如何删除文件中所有东西 ggdG :%d 移动光标到指定行的行尾 $:移动光标到行尾 n$:移动到第 ...

  9. Object.create()兼容实现方法

    if (!Object.create) { Object.create = (function(){ function F(){} return function(o){ if (arguments. ...

  10. How do I install Adobe Flash on Debian Wheezy?

    aptitude install flashplugin-nonfreeif fail Check your settings in /etc/apt/sources.list. If this is ...