除了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. 读取NfcA格式数据

    如何读取数据? Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); NfcA nfcA = NfcA.get(ta ...

  2. Flyer(二分 HDU4768)

    Flyer Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submiss ...

  3. (4) 深入理解Java Class文件格式(三)

    转载:http://blog.csdn.net/zhangjg_blog/article/details/21557357 首先, 让我们回顾一下关于class文件格式的之前两篇博客的主要内容. 在  ...

  4. windows消息和消息队列

    windows消息和消息队列 转自:http://blog.163.com/zhangjie_0303/blog/static/990827062010113062446767/ 与基于MS - DO ...

  5. Windows Server 2008服务器配置FTP站点的方法教程

    1.首先,安装FTP服务   打开服务器管理器,点击角色,添加角色,如果安装过iis,角色摘要里面会有个Web服务器(IIS),点击后面的添加角色,滚动条拉到最后勾选FTP服务器,根据步骤安装. ww ...

  6. IPV6

    (一) iOS 9.0.OS X 10.11 以上的系统 在IPv6的环境下 是支持IP地址访问网络的.所以大家测试机如果是 iOS9.0以上的系统,可以直接通过IP访问.这是因为iOS 9.0之后  ...

  7. GZAPI框架初识

    新建一个MVC项目(GZAPIFramework.Demo): mvc:用于API接口文档查看,Log日志查看 webapi:api调用 新建一个Biz类库并添加nuget引用: 搜索GZAPI.Co ...

  8. XML学习笔记(四)-- 修饰XML文档的CSS

    标签(空格分隔): 学习笔记 XML为存储结构化数据提供了强大的方法,但是它没有提供关于数据如何显示地信息,(数据的结构与数据表示无关).可以使用CSS来控制XML文档中各元素的呈现方式. CSS语法 ...

  9. BW常用事务码Tcode

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  10. java 代理模式一: 静态代理

    代理模式: 代理模式的作用:为其他对象提供一种代理以控制对 特定对象  的访问. 某种情况下,一个客户不想或者直接引用另一个对象,而代理对象可以在客户端和目标对象之间起到中介的作用:通过代理对象引用. ...