artificerpi

Java Collection Framework

Java 集合框架

Java集合框架是指导java的集合类。

 

Collection Framework的特性

  •  高性能。为基本的数据结构提供更高效的实现。
  •  让不同类型的collection以相似的方式工作,具有高度互操作性。
  •  能容易地继承或拓展一个collection

所有的Collections框架包括了基本的Interfaces, Implementations,Algorithms。

 

eg: java.util.Queue

 1 import java.util.LinkedList;
 2 import java.util.Queue;
 3 
 4 /**
 5  * Created by artificerPi on 2016/4/12.
 6  */
 7 
 8 public class Main {
 9     static Queue<String> queue = new LinkedList<>();
10     public static void main(String[] args) {
11         queue.offer("abc");  // add an element
12         queue.offer("Hello"); // add another one
13 
14         System.out.println(queue.size());
15         System.out.println(queue.peek());   // get the first element
16         System.out.println(queue.poll());   // get the first element and remove it
17         for(String str: queue){
18             System.out.println(queue);
19         }
20     }
21 }

 

   Iterator接口提供了很多对集合元素进行迭代的方法。每一个集合类都包含了可以返回迭代器实例的

迭代方法。迭代器可以在迭代的过程中删除底层集合的元素,但是不可以直接调用集合的
remove(Object Obj)删除,可以通过迭代器的remove()方法删除。
 
 
Java中的HashMap的工作原理是什么?
 
  Java中的HashMap是以键值对(key-value)的形式存储元素的。HashMap需要一个hash函数,它使用hashCode()和equals()方法来向集合/从集合添加和检索元素。当调用put()方法的时候,HashMap会计算key的hash值,然后把键值对存储在集合中合适的索引上。如果key已经存在了,value会被更新成新值。HashMap的一些重要的特性是它的容量(capacity),负载因子(load factor)和扩容极限(threshold resizing)。
 
  HashMap和Hashtable都实现了Map接口,因此很多特性非常相似。但是,他们有以下不同点:
    • HashMap允许键和值是null,而HashTable不允许键或者值是null
    • HashTable是同步的,而HashMap不是。因此,HashMap更适合于单线程环境,而HashTable适合于多线程环境。
    • HashMap提供了可供应用迭代的键的集合,因此,HashMap是快速失败的。另一方面,HashTable提供了对键的列举(Enumeration)
    • 由所有类的“collection 视图方法”返回的 collection 的 iterator 方法返回的迭代器都是快速失败 的:在创建 Iterator 之后,如果从结构上对 Hashtable 进行修改,除非通过 Iterator 自身的 remove 方法,否则在任何时间以任何方式对其进行修改,Iterator 都将抛出ConcurrentModificationException。因此,面对并发的修改,Iterator 很快就会完全失败,而不冒在将来某个不确定的时间发生任意不确定行为的风险。由 Hashtable 的键和元素方法返回的 Enumeration 不 是快速失败的。
      View Code

      一般认为HashTable是一个遗留的类。

 

Enum

enum类型

  1. enum 类型不支持 public 和 protected 修饰符的构造方法,因此构造函数一定要是 private 或 friendly 的。也正因为如此,所以枚举对象是无法在程序中通过直接调用其构造方法来初始化的。
  2. 定义 enum 类型时候,如果是简单类型,那么最后一个枚举值后不用跟任何一个符号;但如果有定制方法,那么最后一个枚举值与后面代码要用分号';'隔开,不能用逗号或空格。
  3. 由于 enum 类型的值实际上是通过运行期构造出对象来表示的,所以在 cluster 环境下,每个虚拟机都会构造出一个同义的枚举对象。因而在做比较操作时候就需要注意,如果直接通过使用等号 ( ‘ == ’ ) 操作符,这些看似一样的枚举值一定不相等,因为这不是同一个对象实例。

Enum

public abstract class Enum<E extends Enum<E>>
implements Comparable<E>, Serializable {
/**
 * This is the common base class of all Java language enumeration types.
 *
 * More information about enums, including descriptions of the
 * implicitly declared methods synthesized by the compiler, can be
 * found in section 8.9 of
 * <cite>The Java™ Language Specification</cite>.
 *
 * <p> Note that when using an enumeration type as the type of a set
 * or as the type of the keys in a map, specialized and efficient
 * {@linkplain java.util.EnumSet set} and {@linkplain
 * java.util.EnumMap map} implementations are available.
 *
 * @param <E> The enum type subclass
 * @author  Josh Bloch
 * @author  Neal Gafter
 * @see     Class#getEnumConstants()
 * @see     java.util.EnumSet
 * @see     java.util.EnumMap
 * @since   1.5
 */

  

EnumSet

public abstract class EnumSet<E extends Enum<E>> extends AbstractSet<E>
implements Cloneable, java.io.Serializable

EnumMap

public class EnumMap<K extends Enum<K>, V> extends AbstractMap<K, V>
implements java.io.Serializable, Cloneable

代码:

 1 import java.util.EnumMap;
 2 
 3 public class Demo {
 4     public enum API{
 5         GETNAME,GETIFNO,SETNAME,SETINFO
 6     }
 7     private EnumMap<API,String> urls = new EnumMap<API,String>(API.class);
 8 
 9     public Demo(){
10         urls.put(API.GETNAME,"/getName");
11         urls.put(API.GETIFNO,"/getInfo");
12         urls.put(API.SETNAME,"/setName");
13         urls.put(API.SETINFO,"/setInfo");
14     }
15 
16     public String getURL(API api){
17         return this.urls.get(api);
18     }
19 
20     public static void main(String[] args){
21         Demo demo = new Demo();
22         for(API api : demo.urls.keySet()) {
23             System.out.println(api + demo.getURL(api));
24         }
25     }
26 
27 }
View Code

 

 Concurrent Collections

https://www.ibm.com/developerworks/cn/java/j-5things4.html

 

参考:

https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html

https://www.ibm.com/developerworks/cn/java/j-lo-enum/

posted @ 2016-10-02 14:00  artificerpi  阅读(260)  评论(0编辑  收藏  举报

Copyright ©2017 artificerpi