Map已知實現類概述
Attributes
AuthProvider
ConcurrentHashMap
ConcurrentSkipListMap
EnumMap
HashMap
Hashtable
IdentityHashMap
PrinterStateReasons
Properties
AbstractMap
以下類都是他的子類
ConcurrentHashMap / ConcurrentSkipListMap / EnumMap / HashMap / IdentityHashMap / TreeMap / WeakHashMap
Attributes
public static void main(String[] args) {
String s = "e:\\commons-dbcp-1.3.jar";
JarInputStream ji = null;
try {
//jarInputStream與attributes都是在java.util.jar包
ji = new JarInputStream(new FileInputStream(s));
Manifest mf = ji.getManifest();
mf.getMainAttributes().forEach((k, v) -> System.out.println("[ k:" + k + "/" + "v:" + v + " ]"));
} catch (Exception e) {
e.printStackTrace();
} finally {
if (ji != null)
try {
ji.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
[ k:Bundle-License/v:http://www.apache.org/licenses/LICENSE-2.0.txt ]
[ k:Bundle-SymbolicName/v:org.apache.commons.dbcp ]
[ k:Archiver-Version/v:Plexus Archiver ]
[ k:Built-By/v:philsteitz ]
[ k:Bnd-LastModified/v:1265564429985 ]
[ k:Implementation-Vendor-Id/v:org.apache ]
[ k:Specification-Title/v:Commons DBCP ]
[ k:Bundle-DocURL/v:http://commons.apache.org/dbcp/ ]
[ k:Import-Package/v:javax.naming,javax.naming.spi,javax.sql,javax.transaction,javax.transaction.xa,org.apache.commons.dbcp;version="1.3",org.apache.commons.dbcp.cpdsadapter;version="1.3",org.apache.commons.dbcp.datasources;version="1.3",org.apache.commons.dbcp.managed;version="1.3",org.apache.commons.jocl;version="1.3",org.apache.commons.pool,org.apache.commons.pool.impl,org.xml.sax,org.xml.sax.helpers ]
[ k:Export-Package/v:org.apache.commons.jocl;version="1.3",org.apache.commons.dbcp.datasources;version="1.3",org.apache.commons.dbcp.cpdsadapter;version="1.3",org.apache.commons.dbcp.managed;version="1.3",org.apache.commons.dbcp;version="1.3" ]
[ k:Bundle-Name/v:Commons DBCP ]
[ k:Implementation-Title/v:Commons DBCP ]
[ k:Bundle-Description/v:Commons Database Connection Pooling ]
[ k:Implementation-Version/v:1.3 ]
[ k:Specification-Vendor/v:The Apache Software Foundation ]
[ k:Bundle-ManifestVersion/v:2 ]
[ k:Bundle-Vendor/v:The Apache Software Foundation ]
[ k:Tool/v:Bnd-0.0.238 ]
[ k:Manifest-Version/v:1.0 ]
[ k:Implementation-Vendor/v:The Apache Software Foundation ]
[ k:Bundle-Version/v:1.3 ]
[ k:X-Compile-Target-JDK/v:1.4 ]
[ k:X-Compile-Source-JDK/v:1.4 ]
[ k:Created-By/v:1.5.0_19 (Apple Inc.) ]
[ k:Build-Jdk/v:1.5.0_19 ]
[ k:Specification-Version/v:1.3 ]
AuthProvider
概述:JAAS相關內容,提供了3個抽象方法。login logout setCallbackHandler
ConcurrentHashMap
ConcurrentSkipListMap
EnumMap
public static void main(String[] args) {
EnumMap<State,String> e = new EnumMap<State,String>(State.class);
System.out.println(e.put(State.NPE, "11"));
System.out.println(e.put(State.NPE, "22"));
for (Map.Entry<State, String> entry : e.entrySet()) {
System.out.println(entry.getValue() + " " + entry.getKey().str());
}
System.out.println();
}
public enum State {
NOTFOUND("404", "404"), SERVERERROR("500", "500"),
SUCCESS("100", "成功"), NPE("101", "空指針"),
EXCEPTION("102","異常"), IPPERMISSIONS("103", "IP訪問權限不足"),
VERSIONERROR("104", "版本錯誤"),
DOMAINNOTFOUND("105", "域不存在"), ;
private String value;
private String str;
private State(String value, String str) {
this.value = value;
this.str = str;
}
public String value() {
return this.value;
}
public String str() {
return this.str;
}
}
public EnumMap(Class<K> keyType) {
this.keyType = keyType;
keyUniverse = getKeyUniverse(keyType);//將枚舉Key返回爲key[]
vals = new Object[keyUniverse.length]; //初始化一個vals
}
public V put(K key, V value) {
typeCheck(key); //key類型要匹配Class<K>
int index = key.ordinal(); //得到Key的下標 Enum.ordinal();其實就是在你的Enum中的定義順序
Object oldValue = vals[index];
vals[index] = maskNull(value);
//如果值不存在則返回一個EnumMapObject // hasCode return 0 // toString return "java.util.EnumMap.NULL"
if (oldValue == null)
size++; //oldValue不存在計數器+1,代表是新增而不是覆蓋
return unmaskNull(oldValue);//新增返回null 覆蓋返回 舊值
}
public V get(Object key) {
return (isValidKey(key) ?
unmaskNull(vals[((Enum<?>)key).ordinal()]) : null);
//key類型正確則以key下標去vals取值,空則返回
}
HashMap
public HashMap(int initialCapacity, float loadFactor) {
//容量和加載因子,容量不說了,因子越小map利用率越高,衝突概率就高,反之越稀鬆,衝突概率越低
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal initial capacity: " +
initialCapacity);
if (initialCapacity > MAXIMUM_CAPACITY) //初始容量不能超過最大支持容量,最大容量1<<30
initialCapacity = MAXIMUM_CAPACITY;
if (loadFactor <= 0 || Float.isNaN(loadFactor)) //因子判斷
throw new IllegalArgumentException("Illegal load factor:"+ loadFactor);
this.loadFactor = loadFactor; this.threshold = tableSizeFor(initialCapacity); //下一次調整大小的值
}
static class Node<K,V> implements Map.Entry<K,V> {
final int hash;
final K key;
V value;
Node<K,V> next;
Node(int hash, K key, V value, Node<K,V> next) {
this.hash = hash;
this.key = key;
this.value = value;
this.next = next;
}
public final K getKey() { return key; }
public final V getValue() { return value; }
public final String toString() { return key + "=" + value; }
public final int hashCode() {
return Objects.hashCode(key) ^ Objects.hashCode(value);
}
public final V setValue(V newValue) {
V oldValue = value;
value = newValue;
return oldValue;
}
public final boolean equals(Object o) {
if (o == this)
return true;
if (o instanceof Map.Entry) {
Map.Entry<?,?> e = (Map.Entry<?,?>)o;
if (Objects.equals(key, e.getKey()) &&
Objects.equals(value, e.getValue()))
return true;
}
return false;
}
}
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
Hashtable
IdentityHashMap
public static void main(String[] args) {
IdentityHashMap<String, Integer> map=new IdentityHashMap<String, Integer>();
String a=new String("天");
map.put(a, 1);
map.put(new String("天"), 2);
map.put(new String("天"), 3);
map.put(new String("王"), 4);
System.out.println(map.toString());
System.out.println(map.get(a));
}
LinkedHashMap
static class Entry<K,V> extends HashMap.Node<K,V> {
Entry<K,V> before, after; //繼承HashMap.Node並定義了before after來完成雙向循環鏈表
Entry(int hash, K key, V value, Node<K,V> next) {
super(hash, key, value, next);
}
}
其他地方與HashMap基本一致,只是增加了before,after並對相關函數重寫已遍生效有序PrinterStateReasons
標籤: 打印機 非線程安全 HashMap派生類public static void main(String[] args) {
PrintService myPrint = PrintServiceLookup.lookupDefaultPrintService();
PrinterStateReasons printerStateReasons = myPrint.getAttribute(PrinterStateReasons.class);
System.out.println( printerStateReasons);
}
Properties
public static void main(String[] args) throws Exception {
Properties pro = new Properties();
try {
pro.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("conf-test.properties"));
System.out.println(pro.get("username"));
} catch (IOException e) {
}
}