diff --git a/README.md b/README.md index add63ab3..f4fa5df9 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ 整理自《深入理解 Java 虚拟机》,包括内存模型、垃圾回收和类加载机制。 -> [Java 容器](https://github.com/CyC2018/InnterviewNotes/blob/master/notes/Java%20集合.mdd) +> [Java 容器](https://github.com/CyC2018/InnterviewNotes/blob/master/notes/Java%20容器.md) 容器的一些总结,包含容器源码的分析。 diff --git a/notes/Java 容器.md b/notes/Java 容器.md new file mode 100644 index 00000000..9721f2a6 --- /dev/null +++ b/notes/Java 容器.md @@ -0,0 +1,360 @@ + +* [](#) + * [1. List](#1-list) + * [2. Set](#2-set) + * [3. Queue](#3-queue) + * [4. Map](#4-map) + * [5. Java 1.0/1.1 ](#5-java-1011-) +* [еģʽ](#еģʽ) + * [1. ģʽ](#1-ģʽ) + * [2. ģʽ](#2-ģʽ) +* [ɢ](#ɢ) +* [Դ](#Դ) + * [1. ArraList](#1-arralist) + * [2. Vector Stack](#2-vector--stack) + * [3. LinkedList](#3-linkedlist) + * [4. TreeMap](#4-treemap) + * [5. HashMap](#5-hashmap) + * [6. LinkedHashMap](#6-linkedhashmap) + * [7. ConcurrentHashMap](#7-concurrenthashmap) +* [ο](#ο) + + +# + +![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/ebf03f56-f957-4435-9f8f-0f605661484d.jpg) + +Ҫ Collection Map ֣Collection ְ ListSet Լ Queue + +## 1. List + +- ArrayListʹ鷽֧ʣ + +- LinkedListʹʵֻ֣˳ʣǿԿٵмɾԪءˣLinkedList ջк˫˶С + +## 2. Set + +- HashSetʹ Hash ʵֿ֣֧ٲңʧȥԣ + +- TreeSetʹʵ֣򣬵DzЧʲ HashSet + +- LinkedListHashSet HashSet IJЧʣڲʹάԪصIJ˳˾ԡ + +## 3. Queue + +ֻʵ֣LinkedList PriorityQueue LinkedList ֧˫С + +## 4. Map + +- HashMapʹ Hash ʵ + +- LinkedHashMap˳Ϊ˳ʹãLRU˳ + +- TreeMapںʵ + +- ConcurrentHashMap̰߳ȫ Map漰ͬ + +## 5. Java 1.0/1.1 + +ھɵǾӦʹǣֻҪǽ˽⡣ + +- Vector ArrayList ƣ̰߳ȫ + +- HashTable HashMap ƣ̰߳ȫ + +# еģʽ + +## 1. ģʽ + +ӸͼԿÿ඼һ Iterator 󣬿ͨеԪء + +[Java еĵģʽ ](https://github.com/CyC2018/InterviewNotes/blob/master/notes/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F.md#92-java-%E5%86%85%E7%BD%AE%E7%9A%84%E8%BF%AD%E4%BB%A3%E5%99%A8) + +## 2. ģʽ + +java.util.Arrays#asList() ԰תΪ List ͡ + +```java + List list = Arrays.asList(1, 2, 3); + int[] arr = {1, 2, 3}; + list = Arrays.asList(arr); +``` + +# ɢ + +ʹ hasCode() ɢֵʹõǶĵַ + + equals() жǷȵģȵɢֵһҪͬɢֵͬһȡ + +ȱʣ + +1. Է +2. Գ +3. +4. һԣε x.equals(y)䣩 +5. κβ null Ķ x x.equals(nul) Ϊ false + +# Դ + +Ķ [ 㷨 - ](https://github.com/CyC2018/InterviewNotes/blob/master/notes/%E7%AE%97%E6%B3%95.md#%E7%AC%AC%E4%B8%89%E7%AB%A0-%E6%9F%A5%E6%89%BE) ֣ԼԴкܴ + +Դأ[OpenJDK 1.7](http://download.java.net/openjdk/jdk7) + +## 1. ArraList + +[ArraList.java](https://github.com/CyC2018/InterviewNotes/blob/master/notes/src/ArrayList.java) + +ʵ RandomAccess ӿڣ֧ʣȻģΪ ArrayList ǻʵֵġ + +```java +public class ArrayList extends AbstractList implements List, RandomAccess, Cloneable, java.io.Serializable +``` + +ʵ֣Ԫصʹ transient ΣΪ鲻һλöռԪأҲûҪȫлҪд writeObject() readObject() + +```java +private transient Object[] elementData; +``` + +ĬϴСΪ 10 + +```java +public ArrayList(int initialCapacity) { + super(); + if (initialCapacity < 0) + throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity); + this.elementData = new Object[initialCapacity]; +} + +public ArrayList() { + this(10); +} +``` + +ɾԪʱ System.arraycopy() Ԫؽиƣɾɱܸߡ + +```java +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; // Let gc do its work + + return oldValue; +} +``` + +Ԫʱʹ ensureCapacity() ֤㹻ʱҪݣʹΪ 1.5 + +modCount ¼ ArrayList 仯ĴΪÿڽ add() addAll() ʱҪ ensureCapacity()ֱ ensureCapacity() ж modCount ޸ġ + +```java +public void ensureCapacity(int minCapacity) { + if (minCapacity > 0) + ensureCapacityInternal(minCapacity); +} + +private void ensureCapacityInternal(int minCapacity) { + modCount++; + // overflow-conscious code + if (minCapacity - elementData.length > 0) + grow(minCapacity); +} + +private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; + +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); +} + +private static int hugeCapacity(int minCapacity) { + if (minCapacity < 0) // overflow + throw new OutOfMemoryError(); + return (minCapacity > MAX_ARRAY_SIZE) ? + Integer.MAX_VALUE : + MAX_ARRAY_SIZE; +} +``` + +ڽлߵȲʱҪȽϲǰ modCount Ƿı䣬ıҪ׳ ConcurrentModificationException + +```java +private void writeObject(java.io.ObjectOutputStream s) + throws java.io.IOException{ + // Write out element count, and any hidden stuff + int expectedModCount = modCount; + s.defaultWriteObject(); + + // Write out array length + s.writeInt(elementData.length); + + // Write out all elements in the proper order. + for (int i=0; i()); һ̰߳ȫ ArrayListҲʹ concurrent µ CopyOnWriteArrayList ࣻ + +** LinkedList ** + +1. ArrayList ڶ̬ʵ֣LinkedList ˫ѭʵ֣ +2. ArrayList ֧ʣLinkedList ֧֣ +3. LinkedList λɾԪظ졣 + +## 2. Vector Stack + +[Vector.java](https://github.com/CyC2018/InterviewNotes/blob/master/notes/src/Vector.java) + +## 3. LinkedList + +[LinkedList.java](https://github.com/CyC2018/InterviewNotes/blob/master/notes/src/LinkedList.java) + +## 4. TreeMap + +[TreeMap.java](https://github.com/CyC2018/InterviewNotes/blob/master/notes/src/TreeMap.java) + +## 5. HashMap + +[HashMap.java](https://github.com/CyC2018/InterviewNotes/blob/master/notes/src/HashMap.java) + +ʹͻ + +Ĭ capacity Ϊ 16Ҫע뱣֤Ϊ 2 Ĵη Entry[] table ijȣsize ʵʹ + +threshold 涨һ size ٽֵsize С thresholdڵڣͱݲ + +threshold = capacity * load_factor load_factor Ϊ table ܹʹõıload_factor ᵼ¾۴صij֣ӶӰѯͲЧʣ㷨ʼǡ + +```java +static final int DEFAULT_INITIAL_CAPACITY = 16; + +static final int MAXIMUM_CAPACITY = 1 << 30; + +static final float DEFAULT_LOAD_FACTOR = 0.75f; + +transient Entry[] table; + +transient int size; + +int threshold; + +final float loadFactor; + +transient int modCount; +``` + +ԪشпԿҪʱ capacity Ϊԭ + +```java +void addEntry(int hash, K key, V value, int bucketIndex) { + Entry e = table[bucketIndex]; + table[bucketIndex] = new Entry<>(hash, key, value, e); + if (size++ >= threshold) + resize(2 * table.length); +} +``` + +Entry ʾһֵԪأе next ָлʱʹá + +```java +static class Entry implements Map.Entry { + final K key; + V value; + Entry next; + final int hash; +} +``` + +get() Ҫֳkey Ϊ null Ϊ nullпԿ HashMap null Ϊ + +```java +public V get(Object key) { + if (key == null) + return getForNullKey(); + int hash = hash(key.hashCode()); + for (Entry e = table[indexFor(hash, table.length)]; e != null; e = e.next) { + Object k; + if (e.hash == hash && ((k = e.key) == key || key.equals(k))) + return e.value; + } + return null; +} +``` + +put() ҲҪ key ǷΪ null ͬĴҪעû key Ϊ null ļֵԣ²һ key Ϊ null ļֵʱĬǷ 0 λãΪ null ܼ hash ֵҲ޷֪Ӧ÷ĸϡ + +```java +public V put(K key, V value) { + if (key == null) + return putForNullKey(value); + int hash = hash(key.hashCode()); + int i = indexFor(hash, table.length); + for (Entry e = table[i]; e != null; e = e.next) { + Object k; + if (e.hash == hash && ((k = e.key) == key || key.equals(k))) { + V oldValue = e.value; + e.value = value; + e.recordAccess(this); + return oldValue; + } + } + + modCount++; + addEntry(hash, key, value, i); + return null; +} +``` + +```java +private V putForNullKey(V value) { + for (Entry e = table[0]; e != null; e = e.next) { + if (e.key == null) { + V oldValue = e.value; + e.value = value; + e.recordAccess(this); + return oldValue; + } + } + modCount++; + addEntry(0, null, value, 0); + return null; +} +``` + +## 6. LinkedHashMap + +[LinkedHashMap.java](https://github.com/CyC2018/InterviewNotes/blob/master/notes/src/HashMap.java) + +## 7. ConcurrentHashMap + +[ConcurrentHashMap.java](https://github.com/CyC2018/InterviewNotes/blob/master/notes/src/HashMap.java) + +[ ̽ ConcurrentHashMap ߲Եʵֻ ](https://www.ibm.com/developerworks/cn/java/java-lo-concurrenthashmap/) + +# ο + +- Java ˼ diff --git a/notes/Java 集合.md b/notes/Java 集合.md deleted file mode 100644 index b6d0616b..00000000 --- a/notes/Java 集合.md +++ /dev/null @@ -1,152 +0,0 @@ - -* [](#) - * [1. List](#1-list) - * [2. Set](#2-set) - * [3. Queue](#3-queue) - * [4. Map](#4-map) - * [5. Java 1.0/1.1 ](#5-java-1011-) -* [еģʽ](#еģʽ) - * [1. ģʽ](#1-ģʽ) - * [2. ģʽ](#2-ģʽ) -* [ɢ](#ɢ) -* [Դ](#Դ) - * [1. ArraList](#1-arralist) - * [2. LinkedList](#2-linkedlist) - * [3. Vector](#3-vector) - * [4. HashMap](#4-hashmap) - * [5. LinkedHashMap](#5-linkedhashmap) - * [6. ConcurrentHashMap](#6-concurrenthashmap) -* [ο](#ο) - - -# - -![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/ebf03f56-f957-4435-9f8f-0f605661484d.jpg) - -Ҫ Collection Map ֣Collection ְ ListSet Լ Queue - -## 1. List - -- ArrayListʹ鷽֧ʣ - -- LinkedListʹʵֻ֣˳ʣǿԿٵмɾԪءˣLinkedList ջк˫˶С - -## 2. Set - -- HashSetʹ Hash ʵֿ֣֧ٲңʧȥԣ - -- TreeSetʹʵ֣򣬵DzЧʲ HashSet - -- LinkedListHashSet HashSet IJЧʣڲʹάԪصIJ˳˾ԡ - -## 3. Queue - -ֻʵ֣LinkedList PriorityQueue LinkedList ֧˫С - -## 4. Map - -- HashMapʹ Hash ʵ - -- LinkedHashMap˳Ϊ˳ʹãLRU˳ - -- TreeMapںʵ - -- ConcurrentHashMap̰߳ȫ Map漰ͬ - -## 5. Java 1.0/1.1 - -ھɵǾӦʹǣֻҪǽ˽⡣ - -- Vector ArrayList ƣ̰߳ȫ - -- HashTable HashMap ƣ̰߳ȫ - -# еģʽ - -## 1. ģʽ - -ӸͼԿÿ඼һ Iterator 󣬿ͨеԪء - -[Java еĵģʽ](https://github.com/CyC2018/InterviewNotes/blob/master/notes/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F.md#92-java-%E5%86%85%E7%BD%AE%E7%9A%84%E8%BF%AD%E4%BB%A3%E5%99%A8) - -## 2. ģʽ - -java.util.Arrays#asList() ԰תΪ List ͡ - -```java - List list = Arrays.asList(1, 2, 3); - int[] arr = {1, 2, 3}; - list = Arrays.asList(arr); -``` - -# ɢ - -ʹ hasCode() ɢֵʹõǶĵַ - - equals() жǷȵģȵɢֵһҪͬɢֵͬһȡ - -ȱʣ - -1. Է -2. Գ -3. -4. һԣε x.equals(y)䣩 -5. κβ null Ķ x x.equals(nul) Ϊ false - -# Դ - -Ķ [㷨-](https://github.com/CyC2018/InterviewNotes/blob/master/notes/%E7%AE%97%E6%B3%95.md#%E7%AC%AC%E4%B8%89%E7%AB%A0-%E6%9F%A5%E6%89%BE) ֣ԼԴкܴ - - -Դأ[OpenJDK 1.7](http://download.java.net/openjdk/jdk7) - -Ķ[7u40-b43](http://grepcode.com/snapshot/repository.grepcode.com/java/root/jdk/openjdk/7u40-b43/) - -## 1. ArraList - -- ʹʵ - -- ж̬ԣĬΪ 10Ԫʱʹ ensureCapacity() ֤㹻Ϊԭʼ 1.5 times + 1. - -[ArraList.java](https://github.com/CyC2018/InterviewNotes/blob/master/notes/src/ArrayList.java) - - -## 2. LinkedList - -- LinkedList ǻ˫ѭʵ֣ͷ㲻ݡ - -![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/d40c90ad-7943-4574-98a8-8027e5523d53.jpg) - -- ҪLinkedList Entry entry(int index) һŻIJ index ǰ棬ôʹͷںʹӺǰ - -[LinkedList.java](https://github.com/CyC2018/InterviewNotes/blob/master/notes/src/LinkedList.java) - -## 3. Vector - -Vector ĺܶʵַͬ䣬̰߳ȫġ - -[Vector.java](https://github.com/CyC2018/InterviewNotes/blob/master/notes/src/Vector.java) - -## 4. HashMap - -- ʹͻ - -[Vector.java](https://github.com/CyC2018/InterviewNotes/blob/master/notes/src/HashMap.java) - - -## 5. LinkedHashMap - -- ʹ˫Ľڵ㣬Ӷάһ˳ -- עԴеaccessOrder־λfalseʱʾ˫еԪذEntryLinkedHashMapеȺ˳򣬼ÿputLinkedHashMapеEntry˫β˫ʱEntry˳Ͳ˳һ£ҲĬϵ˫Ĵ洢˳򣻵Ϊtrueʱʾ˫еԪذշʵȺ˳УԿȻEntry˳ȻǰputLinkedHashMapе˳򣬵putgetеrecordAccessputkeyͬԭеEntryµrecordAccess÷жaccessOrderǷΪtrueǣ򽫵ǰʵEntryputEntrygetEntryƵ˫βkeyͬʱputEntryʱaddEntrycreatEntry÷ͬ²Ԫط뵽˫βȷϲȺ˳ַϷʵȺ˳ΪʱEntryҲˣʲôҲ˵˵LinkedHashMapʵLRUġȣaccessOrderΪtrueʱŻῪ˳ģʽʵLRU㷨ǿԿputgetᵼĿEntryΪʵEntry˱ѸEntry뵽˫ĩβgetͨrecordAccessʵ֣putڸkey£ҲͨrecordAccessʵ֣ڲµEntryʱͨcreateEntryеaddBeforeʵ֣ʹ˵Entry뵽˫ĺ棬β˫ǰEntryûʹõģڵʱɾǰEntry(headǸEntry)ʹõEntry - -[LinkedHashMap.java](https://github.com/CyC2018/InterviewNotes/blob/master/notes/src/HashMap.java) - -## 6. ConcurrentHashMap - -[̽ ConcurrentHashMap ߲Եʵֻ](https://www.ibm.com/developerworks/cn/java/java-lo-concurrenthashmap/) - -[ConcurrentHashMap.java](https://github.com/CyC2018/InterviewNotes/blob/master/notes/src/HashMap.java) - -# ο - -- Java ˼ diff --git a/notes/src/ConcurrentHashMap.java b/notes/src/ConcurrentHashMap.java index e8214806..338f7d65 100644 --- a/notes/src/ConcurrentHashMap.java +++ b/notes/src/ConcurrentHashMap.java @@ -1,37 +1,3 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - -/* - * This file is available under and governed by the GNU General Public - * License version 2 only, as published by the Free Software Foundation. - * However, the following notice accompanied the original version of this - * file: - * - * Written by Doug Lea with assistance from members of JCP JSR-166 - * Expert Group and released to the public domain, as explained at - * http://creativecommons.org/publicdomain/zero/1.0/ - */ package java.util.concurrent; import java.util.concurrent.locks.*; @@ -41,64 +7,6 @@ import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; -/** - * A hash table supporting full concurrency of retrievals and - * adjustable expected concurrency for updates. This class obeys the - * same functional specification as {@link java.util.Hashtable}, and - * includes versions of methods corresponding to each method of - * Hashtable. However, even though all operations are - * thread-safe, retrieval operations do not entail locking, - * and there is not any support for locking the entire table - * in a way that prevents all access. This class is fully - * interoperable with Hashtable in programs that rely on its - * thread safety but not on its synchronization details. - * - *

Retrieval operations (including get) generally do not - * block, so may overlap with update operations (including - * put and remove). Retrievals reflect the results - * of the most recently completed update operations holding - * upon their onset. For aggregate operations such as putAll - * and clear, concurrent retrievals may reflect insertion or - * removal of only some entries. Similarly, Iterators and - * Enumerations return elements reflecting the state of the hash table - * at some point at or since the creation of the iterator/enumeration. - * They do not throw {@link ConcurrentModificationException}. - * However, iterators are designed to be used by only one thread at a time. - * - *

The allowed concurrency among update operations is guided by - * the optional concurrencyLevel constructor argument - * (default 16), which is used as a hint for internal sizing. The - * table is internally partitioned to try to permit the indicated - * number of concurrent updates without contention. Because placement - * in hash tables is essentially random, the actual concurrency will - * vary. Ideally, you should choose a value to accommodate as many - * threads as will ever concurrently modify the table. Using a - * significantly higher value than you need can waste space and time, - * and a significantly lower value can lead to thread contention. But - * overestimates and underestimates within an order of magnitude do - * not usually have much noticeable impact. A value of one is - * appropriate when it is known that only one thread will modify and - * all others will only read. Also, resizing this or any other kind of - * hash table is a relatively slow operation, so, when possible, it is - * a good idea to provide estimates of expected table sizes in - * constructors. - * - *

This class and its views and iterators implement all of the - * optional methods of the {@link Map} and {@link Iterator} - * interfaces. - * - *

Like {@link Hashtable} but unlike {@link HashMap}, this class - * does not allow null to be used as a key or value. - * - *

This class is a member of the - * - * Java Collections Framework. - * - * @since 1.5 - * @author Doug Lea - * @param the type of keys maintained by this map - * @param the type of mapped values - */ public class ConcurrentHashMap extends AbstractMap implements ConcurrentMap, Serializable { private static final long serialVersionUID = 7249069246763182397L; diff --git a/notes/src/HashMap.java b/notes/src/HashMap.java index 916d6951..5afaa09c 100644 --- a/notes/src/HashMap.java +++ b/notes/src/HashMap.java @@ -1,126 +1,6 @@ -/* - * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - package java.util; import java.io.*; -/** - * Hash table based implementation of the Map interface. This - * implementation provides all of the optional map operations, and permits - * null values and the null key. (The HashMap - * class is roughly equivalent to Hashtable, except that it is - * unsynchronized and permits nulls.) This class makes no guarantees as to - * the order of the map; in particular, it does not guarantee that the order - * will remain constant over time. - * - *

This implementation provides constant-time performance for the basic - * operations (get and put), assuming the hash function - * disperses the elements properly among the buckets. Iteration over - * collection views requires time proportional to the "capacity" of the - * HashMap instance (the number of buckets) plus its size (the number - * of key-value mappings). Thus, it's very important not to set the initial - * capacity too high (or the load factor too low) if iteration performance is - * important. - * - *

An instance of HashMap has two parameters that affect its - * performance: initial capacity and load factor. The - * capacity is the number of buckets in the hash table, and the initial - * capacity is simply the capacity at the time the hash table is created. The - * load factor is a measure of how full the hash table is allowed to - * get before its capacity is automatically increased. When the number of - * entries in the hash table exceeds the product of the load factor and the - * current capacity, the hash table is rehashed (that is, internal data - * structures are rebuilt) so that the hash table has approximately twice the - * number of buckets. - * - *

As a general rule, the default load factor (.75) offers a good tradeoff - * between time and space costs. Higher values decrease the space overhead - * but increase the lookup cost (reflected in most of the operations of the - * HashMap class, including get and put). The - * expected number of entries in the map and its load factor should be taken - * into account when setting its initial capacity, so as to minimize the - * number of rehash operations. If the initial capacity is greater - * than the maximum number of entries divided by the load factor, no - * rehash operations will ever occur. - * - *

If many mappings are to be stored in a HashMap instance, - * creating it with a sufficiently large capacity will allow the mappings to - * be stored more efficiently than letting it perform automatic rehashing as - * needed to grow the table. - * - *

Note that this implementation is not synchronized. - * If multiple threads access a hash map concurrently, and at least one of - * the threads modifies the map structurally, it must be - * synchronized externally. (A structural modification is any operation - * that adds or deletes one or more mappings; merely changing the value - * associated with a key that an instance already contains is not a - * structural modification.) This is typically accomplished by - * synchronizing on some object that naturally encapsulates the map. - * - * If no such object exists, the map should be "wrapped" using the - * {@link Collections#synchronizedMap Collections.synchronizedMap} - * method. This is best done at creation time, to prevent accidental - * unsynchronized access to the map:

- *   Map m = Collections.synchronizedMap(new HashMap(...));
- * - *

The iterators returned by all of this class's "collection view methods" - * are fail-fast: if the map is structurally modified at any time after - * the iterator is created, in any way except through the iterator's own - * remove method, the iterator will throw a - * {@link ConcurrentModificationException}. Thus, in the face of concurrent - * modification, the iterator fails quickly and cleanly, rather than risking - * arbitrary, non-deterministic behavior at an undetermined time in the - * future. - * - *

Note that the fail-fast behavior of an iterator cannot be guaranteed - * as it is, generally speaking, impossible to make any hard guarantees in the - * presence of unsynchronized concurrent modification. Fail-fast iterators - * throw ConcurrentModificationException on a best-effort basis. - * Therefore, it would be wrong to write a program that depended on this - * exception for its correctness: the fail-fast behavior of iterators - * should be used only to detect bugs. - * - *

This class is a member of the - * - * Java Collections Framework. - * - * @param the type of keys maintained by this map - * @param the type of mapped values - * - * @author Doug Lea - * @author Josh Bloch - * @author Arthur van Hoff - * @author Neal Gafter - * @see Object#hashCode() - * @see Collection - * @see Map - * @see TreeMap - * @see Hashtable - * @since 1.2 - */ - public class HashMap extends AbstractMap implements Map, Cloneable, Serializable diff --git a/notes/src/HashSet.java b/notes/src/HashSet.java index 9e53917a..b2fca169 100644 --- a/notes/src/HashSet.java +++ b/notes/src/HashSet.java @@ -1,89 +1,5 @@ -/* - * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - package java.util; -/** - * This class implements the Set interface, backed by a hash table - * (actually a HashMap instance). It makes no guarantees as to the - * iteration order of the set; in particular, it does not guarantee that the - * order will remain constant over time. This class permits the null - * element. - * - *

This class offers constant time performance for the basic operations - * (add, remove, contains and size), - * assuming the hash function disperses the elements properly among the - * buckets. Iterating over this set requires time proportional to the sum of - * the HashSet instance's size (the number of elements) plus the - * "capacity" of the backing HashMap instance (the number of - * buckets). Thus, it's very important not to set the initial capacity too - * high (or the load factor too low) if iteration performance is important. - * - *

Note that this implementation is not synchronized. - * If multiple threads access a hash set concurrently, and at least one of - * the threads modifies the set, it must be synchronized externally. - * This is typically accomplished by synchronizing on some object that - * naturally encapsulates the set. - * - * If no such object exists, the set should be "wrapped" using the - * {@link Collections#synchronizedSet Collections.synchronizedSet} - * method. This is best done at creation time, to prevent accidental - * unsynchronized access to the set:

- *   Set s = Collections.synchronizedSet(new HashSet(...));
- * - *

The iterators returned by this class's iterator method are - * fail-fast: if the set is modified at any time after the iterator is - * created, in any way except through the iterator's own remove - * method, the Iterator throws a {@link ConcurrentModificationException}. - * Thus, in the face of concurrent modification, the iterator fails quickly - * and cleanly, rather than risking arbitrary, non-deterministic behavior at - * an undetermined time in the future. - * - *

Note that the fail-fast behavior of an iterator cannot be guaranteed - * as it is, generally speaking, impossible to make any hard guarantees in the - * presence of unsynchronized concurrent modification. Fail-fast iterators - * throw ConcurrentModificationException on a best-effort basis. - * Therefore, it would be wrong to write a program that depended on this - * exception for its correctness: the fail-fast behavior of iterators - * should be used only to detect bugs. - * - *

This class is a member of the - * - * Java Collections Framework. - * - * @param the type of elements maintained by this set - * - * @author Josh Bloch - * @author Neal Gafter - * @see Collection - * @see Set - * @see TreeSet - * @see HashMap - * @since 1.2 - */ - public class HashSet extends AbstractSet implements Set, Cloneable, java.io.Serializable diff --git a/notes/src/LinkedHashMap.java b/notes/src/LinkedHashMap.java index 4f80ec4f..946ac9a8 100644 --- a/notes/src/LinkedHashMap.java +++ b/notes/src/LinkedHashMap.java @@ -1,149 +1,7 @@ -/* - * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ package java.util; import java.io.*; -/** - *

Hash table and linked list implementation of the Map interface, - * with predictable iteration order. This implementation differs from - * HashMap in that it maintains a doubly-linked list running through - * all of its entries. This linked list defines the iteration ordering, - * which is normally the order in which keys were inserted into the map - * (insertion-order). Note that insertion order is not affected - * if a key is re-inserted into the map. (A key k is - * reinserted into a map m if m.put(k, v) is invoked when - * m.containsKey(k) would return true immediately prior to - * the invocation.) - * - *

This implementation spares its clients from the unspecified, generally - * chaotic ordering provided by {@link HashMap} (and {@link Hashtable}), - * without incurring the increased cost associated with {@link TreeMap}. It - * can be used to produce a copy of a map that has the same order as the - * original, regardless of the original map's implementation: - *

- *     void foo(Map m) {
- *         Map copy = new LinkedHashMap(m);
- *         ...
- *     }
- * 
- * This technique is particularly useful if a module takes a map on input, - * copies it, and later returns results whose order is determined by that of - * the copy. (Clients generally appreciate having things returned in the same - * order they were presented.) - * - *

A special {@link #LinkedHashMap(int,float,boolean) constructor} is - * provided to create a linked hash map whose order of iteration is the order - * in which its entries were last accessed, from least-recently accessed to - * most-recently (access-order). This kind of map is well-suited to - * building LRU caches. Invoking the put or get method - * results in an access to the corresponding entry (assuming it exists after - * the invocation completes). The putAll method generates one entry - * access for each mapping in the specified map, in the order that key-value - * mappings are provided by the specified map's entry set iterator. No - * other methods generate entry accesses. In particular, operations on - * collection-views do not affect the order of iteration of the backing - * map. - * - *

The {@link #removeEldestEntry(Map.Entry)} method may be overridden to - * impose a policy for removing stale mappings automatically when new mappings - * are added to the map. - * - *

This class provides all of the optional Map operations, and - * permits null elements. Like HashMap, it provides constant-time - * performance for the basic operations (add, contains and - * remove), assuming the hash function disperses elements - * properly among the buckets. Performance is likely to be just slightly - * below that of HashMap, due to the added expense of maintaining the - * linked list, with one exception: Iteration over the collection-views - * of a LinkedHashMap requires time proportional to the size - * of the map, regardless of its capacity. Iteration over a HashMap - * is likely to be more expensive, requiring time proportional to its - * capacity. - * - *

A linked hash map has two parameters that affect its performance: - * initial capacity and load factor. They are defined precisely - * as for HashMap. Note, however, that the penalty for choosing an - * excessively high value for initial capacity is less severe for this class - * than for HashMap, as iteration times for this class are unaffected - * by capacity. - * - *

Note that this implementation is not synchronized. - * If multiple threads access a linked hash map concurrently, and at least - * one of the threads modifies the map structurally, it must be - * synchronized externally. This is typically accomplished by - * synchronizing on some object that naturally encapsulates the map. - * - * If no such object exists, the map should be "wrapped" using the - * {@link Collections#synchronizedMap Collections.synchronizedMap} - * method. This is best done at creation time, to prevent accidental - * unsynchronized access to the map:

- *   Map m = Collections.synchronizedMap(new LinkedHashMap(...));
- * - * A structural modification is any operation that adds or deletes one or more - * mappings or, in the case of access-ordered linked hash maps, affects - * iteration order. In insertion-ordered linked hash maps, merely changing - * the value associated with a key that is already contained in the map is not - * a structural modification. In access-ordered linked hash maps, - * merely querying the map with get is a structural - * modification.) - * - *

The iterators returned by the iterator method of the collections - * returned by all of this class's collection view methods are - * fail-fast: if the map is structurally modified at any time after - * the iterator is created, in any way except through the iterator's own - * remove method, the iterator will throw a {@link - * ConcurrentModificationException}. Thus, in the face of concurrent - * modification, the iterator fails quickly and cleanly, rather than risking - * arbitrary, non-deterministic behavior at an undetermined time in the future. - * - *

Note that the fail-fast behavior of an iterator cannot be guaranteed - * as it is, generally speaking, impossible to make any hard guarantees in the - * presence of unsynchronized concurrent modification. Fail-fast iterators - * throw ConcurrentModificationException on a best-effort basis. - * Therefore, it would be wrong to write a program that depended on this - * exception for its correctness: the fail-fast behavior of iterators - * should be used only to detect bugs. - * - *

This class is a member of the - * - * Java Collections Framework. - * - * @param the type of keys maintained by this map - * @param the type of mapped values - * - * @author Josh Bloch - * @see Object#hashCode() - * @see Collection - * @see Map - * @see HashMap - * @see TreeMap - * @see Hashtable - * @since 1.4 - */ - public class LinkedHashMap extends HashMap implements Map diff --git a/notes/src/LinkedHashSet.java b/notes/src/LinkedHashSet.java index 399ba80f..9859c3cf 100644 --- a/notes/src/LinkedHashSet.java +++ b/notes/src/LinkedHashSet.java @@ -1,120 +1,6 @@ -/* - * Copyright (c) 2000, 2006, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ package java.util; -/** - *

Hash table and linked list implementation of the Set interface, - * with predictable iteration order. This implementation differs from - * HashSet in that it maintains a doubly-linked list running through - * all of its entries. This linked list defines the iteration ordering, - * which is the order in which elements were inserted into the set - * (insertion-order). Note that insertion order is not affected - * if an element is re-inserted into the set. (An element e - * is reinserted into a set s if s.add(e) is invoked when - * s.contains(e) would return true immediately prior to - * the invocation.) - * - *

This implementation spares its clients from the unspecified, generally - * chaotic ordering provided by {@link HashSet}, without incurring the - * increased cost associated with {@link TreeSet}. It can be used to - * produce a copy of a set that has the same order as the original, regardless - * of the original set's implementation: - *

- *     void foo(Set s) {
- *         Set copy = new LinkedHashSet(s);
- *         ...
- *     }
- * 
- * This technique is particularly useful if a module takes a set on input, - * copies it, and later returns results whose order is determined by that of - * the copy. (Clients generally appreciate having things returned in the same - * order they were presented.) - * - *

This class provides all of the optional Set operations, and - * permits null elements. Like HashSet, it provides constant-time - * performance for the basic operations (add, contains and - * remove), assuming the hash function disperses elements - * properly among the buckets. Performance is likely to be just slightly - * below that of HashSet, due to the added expense of maintaining the - * linked list, with one exception: Iteration over a LinkedHashSet - * requires time proportional to the size of the set, regardless of - * its capacity. Iteration over a HashSet is likely to be more - * expensive, requiring time proportional to its capacity. - * - *

A linked hash set has two parameters that affect its performance: - * initial capacity and load factor. They are defined precisely - * as for HashSet. Note, however, that the penalty for choosing an - * excessively high value for initial capacity is less severe for this class - * than for HashSet, as iteration times for this class are unaffected - * by capacity. - * - *

Note that this implementation is not synchronized. - * If multiple threads access a linked hash set concurrently, and at least - * one of the threads modifies the set, it must be synchronized - * externally. This is typically accomplished by synchronizing on some - * object that naturally encapsulates the set. - * - * If no such object exists, the set should be "wrapped" using the - * {@link Collections#synchronizedSet Collections.synchronizedSet} - * method. This is best done at creation time, to prevent accidental - * unsynchronized access to the set:

- *   Set s = Collections.synchronizedSet(new LinkedHashSet(...));
- * - *

The iterators returned by this class's iterator method are - * fail-fast: if the set is modified at any time after the iterator - * is created, in any way except through the iterator's own remove - * method, the iterator will throw a {@link ConcurrentModificationException}. - * Thus, in the face of concurrent modification, the iterator fails quickly - * and cleanly, rather than risking arbitrary, non-deterministic behavior at - * an undetermined time in the future. - * - *

Note that the fail-fast behavior of an iterator cannot be guaranteed - * as it is, generally speaking, impossible to make any hard guarantees in the - * presence of unsynchronized concurrent modification. Fail-fast iterators - * throw ConcurrentModificationException on a best-effort basis. - * Therefore, it would be wrong to write a program that depended on this - * exception for its correctness: the fail-fast behavior of iterators - * should be used only to detect bugs. - * - *

This class is a member of the - * - * Java Collections Framework. - * - * @param the type of elements maintained by this set - * - * @author Josh Bloch - * @see Object#hashCode() - * @see Collection - * @see Set - * @see HashSet - * @see TreeSet - * @see Hashtable - * @since 1.4 - */ - public class LinkedHashSet extends HashSet implements Set, Cloneable, java.io.Serializable { diff --git a/notes/src/LinkedList.java b/notes/src/LinkedList.java index 94508da5..10f800de 100644 --- a/notes/src/LinkedList.java +++ b/notes/src/LinkedList.java @@ -1,83 +1,6 @@ -/* - * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ package java.util; -/** - * Doubly-linked list implementation of the {@code List} and {@code Deque} - * interfaces. Implements all optional list operations, and permits all - * elements (including {@code null}). - * - *

All of the operations perform as could be expected for a doubly-linked - * list. Operations that index into the list will traverse the list from - * the beginning or the end, whichever is closer to the specified index. - * - *

Note that this implementation is not synchronized. - * If multiple threads access a linked list concurrently, and at least - * one of the threads modifies the list structurally, it must be - * synchronized externally. (A structural modification is any operation - * that adds or deletes one or more elements; merely setting the value of - * an element is not a structural modification.) This is typically - * accomplished by synchronizing on some object that naturally - * encapsulates the list. - * - * If no such object exists, the list should be "wrapped" using the - * {@link Collections#synchronizedList Collections.synchronizedList} - * method. This is best done at creation time, to prevent accidental - * unsynchronized access to the list:

- *   List list = Collections.synchronizedList(new LinkedList(...));
- * - *

The iterators returned by this class's {@code iterator} and - * {@code listIterator} methods are fail-fast: if the list is - * structurally modified at any time after the iterator is created, in - * any way except through the Iterator's own {@code remove} or - * {@code add} methods, the iterator will throw a {@link - * ConcurrentModificationException}. Thus, in the face of concurrent - * modification, the iterator fails quickly and cleanly, rather than - * risking arbitrary, non-deterministic behavior at an undetermined - * time in the future. - * - *

Note that the fail-fast behavior of an iterator cannot be guaranteed - * as it is, generally speaking, impossible to make any hard guarantees in the - * presence of unsynchronized concurrent modification. Fail-fast iterators - * throw {@code ConcurrentModificationException} on a best-effort basis. - * Therefore, it would be wrong to write a program that depended on this - * exception for its correctness: the fail-fast behavior of iterators - * should be used only to detect bugs. - * - *

This class is a member of the - * - * Java Collections Framework. - * - * @author Josh Bloch - * @see List - * @see ArrayList - * @since 1.2 - * @param the type of elements held in this collection - */ - public class LinkedList extends AbstractSequentialList implements List, Deque, Cloneable, java.io.Serializable diff --git a/notes/src/PriorityQueue.java b/notes/src/PriorityQueue.java index b4416ab2..b76c1d96 100644 --- a/notes/src/PriorityQueue.java +++ b/notes/src/PriorityQueue.java @@ -1,82 +1,5 @@ -/* - * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - package java.util; -/** - * An unbounded priority {@linkplain Queue queue} based on a priority heap. - * The elements of the priority queue are ordered according to their - * {@linkplain Comparable natural ordering}, or by a {@link Comparator} - * provided at queue construction time, depending on which constructor is - * used. A priority queue does not permit {@code null} elements. - * A priority queue relying on natural ordering also does not permit - * insertion of non-comparable objects (doing so may result in - * {@code ClassCastException}). - * - *

The head of this queue is the least element - * with respect to the specified ordering. If multiple elements are - * tied for least value, the head is one of those elements -- ties are - * broken arbitrarily. The queue retrieval operations {@code poll}, - * {@code remove}, {@code peek}, and {@code element} access the - * element at the head of the queue. - * - *

A priority queue is unbounded, but has an internal - * capacity governing the size of an array used to store the - * elements on the queue. It is always at least as large as the queue - * size. As elements are added to a priority queue, its capacity - * grows automatically. The details of the growth policy are not - * specified. - * - *

This class and its iterator implement all of the - * optional methods of the {@link Collection} and {@link - * Iterator} interfaces. The Iterator provided in method {@link - * #iterator()} is not guaranteed to traverse the elements of - * the priority queue in any particular order. If you need ordered - * traversal, consider using {@code Arrays.sort(pq.toArray())}. - * - *

Note that this implementation is not synchronized. - * Multiple threads should not access a {@code PriorityQueue} - * instance concurrently if any of the threads modifies the queue. - * Instead, use the thread-safe {@link - * java.util.concurrent.PriorityBlockingQueue} class. - * - *

Implementation note: this implementation provides - * O(log(n)) time for the enqueing and dequeing methods - * ({@code offer}, {@code poll}, {@code remove()} and {@code add}); - * linear time for the {@code remove(Object)} and {@code contains(Object)} - * methods; and constant time for the retrieval methods - * ({@code peek}, {@code element}, and {@code size}). - * - *

This class is a member of the - * - * Java Collections Framework. - * - * @since 1.5 - * @author Josh Bloch, Doug Lea - * @param the type of elements held in this collection - */ public class PriorityQueue extends AbstractQueue implements java.io.Serializable { diff --git a/notes/src/Queue.java b/notes/src/Queue.java index 124cc449..05088a39 100644 --- a/notes/src/Queue.java +++ b/notes/src/Queue.java @@ -1,146 +1,6 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - -/* - * This file is available under and governed by the GNU General Public - * License version 2 only, as published by the Free Software Foundation. - * However, the following notice accompanied the original version of this - * file: - * - * Written by Doug Lea with assistance from members of JCP JSR-166 - * Expert Group and released to the public domain, as explained at - * http://creativecommons.org/publicdomain/zero/1.0/ - */ package java.util; -/** - * A collection designed for holding elements prior to processing. - * Besides basic {@link java.util.Collection Collection} operations, - * queues provide additional insertion, extraction, and inspection - * operations. Each of these methods exists in two forms: one throws - * an exception if the operation fails, the other returns a special - * value (either null or false, depending on the - * operation). The latter form of the insert operation is designed - * specifically for use with capacity-restricted Queue - * implementations; in most implementations, insert operations cannot - * fail. - * - *

- * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - *
Throws exceptionReturns special value
Insert{@link #add add(e)}{@link #offer offer(e)}
Remove{@link #remove remove()}{@link #poll poll()}
Examine{@link #element element()}{@link #peek peek()}
- * - *

Queues typically, but do not necessarily, order elements in a - * FIFO (first-in-first-out) manner. Among the exceptions are - * priority queues, which order elements according to a supplied - * comparator, or the elements' natural ordering, and LIFO queues (or - * stacks) which order the elements LIFO (last-in-first-out). - * Whatever the ordering used, the head of the queue is that - * element which would be removed by a call to {@link #remove() } or - * {@link #poll()}. In a FIFO queue, all new elements are inserted at - * the tail of the queue. Other kinds of queues may use - * different placement rules. Every Queue implementation - * must specify its ordering properties. - * - *

The {@link #offer offer} method inserts an element if possible, - * otherwise returning false. This differs from the {@link - * java.util.Collection#add Collection.add} method, which can fail to - * add an element only by throwing an unchecked exception. The - * offer method is designed for use when failure is a normal, - * rather than exceptional occurrence, for example, in fixed-capacity - * (or "bounded") queues. - * - *

The {@link #remove()} and {@link #poll()} methods remove and - * return the head of the queue. - * Exactly which element is removed from the queue is a - * function of the queue's ordering policy, which differs from - * implementation to implementation. The remove() and - * poll() methods differ only in their behavior when the - * queue is empty: the remove() method throws an exception, - * while the poll() method returns null. - * - *

The {@link #element()} and {@link #peek()} methods return, but do - * not remove, the head of the queue. - * - *

The Queue interface does not define the blocking queue - * methods, which are common in concurrent programming. These methods, - * which wait for elements to appear or for space to become available, are - * defined in the {@link java.util.concurrent.BlockingQueue} interface, which - * extends this interface. - * - *

Queue implementations generally do not allow insertion - * of null elements, although some implementations, such as - * {@link LinkedList}, do not prohibit insertion of null. - * Even in the implementations that permit it, null should - * not be inserted into a Queue, as null is also - * used as a special return value by the poll method to - * indicate that the queue contains no elements. - * - *

Queue implementations generally do not define - * element-based versions of methods equals and - * hashCode but instead inherit the identity based versions - * from class Object, because element-based equality is not - * always well-defined for queues with the same elements but different - * ordering properties. - * - * - *

This interface is a member of the - * - * Java Collections Framework. - * - * @see java.util.Collection - * @see LinkedList - * @see PriorityQueue - * @see java.util.concurrent.LinkedBlockingQueue - * @see java.util.concurrent.BlockingQueue - * @see java.util.concurrent.ArrayBlockingQueue - * @see java.util.concurrent.LinkedBlockingQueue - * @see java.util.concurrent.PriorityBlockingQueue - * @since 1.5 - * @author Doug Lea - * @param the type of elements held in this collection - */ public interface Queue extends Collection { /** * Inserts the specified element into this queue if it is possible to do so diff --git a/notes/src/Stack.java b/notes/src/Stack.java index ebea11e5..96acf05b 100644 --- a/notes/src/Stack.java +++ b/notes/src/Stack.java @@ -1,50 +1,6 @@ -/* - * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ package java.util; -/** - * The Stack class represents a last-in-first-out - * (LIFO) stack of objects. It extends class Vector with five - * operations that allow a vector to be treated as a stack. The usual - * push and pop operations are provided, as well as a - * method to peek at the top item on the stack, a method to test - * for whether the stack is empty, and a method to search - * the stack for an item and discover how far it is from the top. - *

- * When a stack is first created, it contains no items. - * - *

A more complete and consistent set of LIFO stack operations is - * provided by the {@link Deque} interface and its implementations, which - * should be used in preference to this class. For example: - *

   {@code
- *   Deque stack = new ArrayDeque();}
- * - * @author Jonathan Payne - * @since JDK1.0 - */ public class Stack extends Vector { /** diff --git a/notes/src/TreeMap.java b/notes/src/TreeMap.java index 6e92b8d1..120b46cf 100644 --- a/notes/src/TreeMap.java +++ b/notes/src/TreeMap.java @@ -1,108 +1,6 @@ -/* - * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ package java.util; -/** - * A Red-Black tree based {@link NavigableMap} implementation. - * The map is sorted according to the {@linkplain Comparable natural - * ordering} of its keys, or by a {@link Comparator} provided at map - * creation time, depending on which constructor is used. - * - *

This implementation provides guaranteed log(n) time cost for the - * {@code containsKey}, {@code get}, {@code put} and {@code remove} - * operations. Algorithms are adaptations of those in Cormen, Leiserson, and - * Rivest's Introduction to Algorithms. - * - *

Note that the ordering maintained by a tree map, like any sorted map, and - * whether or not an explicit comparator is provided, must be consistent - * with {@code equals} if this sorted map is to correctly implement the - * {@code Map} interface. (See {@code Comparable} or {@code Comparator} for a - * precise definition of consistent with equals.) This is so because - * the {@code Map} interface is defined in terms of the {@code equals} - * operation, but a sorted map performs all key comparisons using its {@code - * compareTo} (or {@code compare}) method, so two keys that are deemed equal by - * this method are, from the standpoint of the sorted map, equal. The behavior - * of a sorted map is well-defined even if its ordering is - * inconsistent with {@code equals}; it just fails to obey the general contract - * of the {@code Map} interface. - * - *

Note that this implementation is not synchronized. - * If multiple threads access a map concurrently, and at least one of the - * threads modifies the map structurally, it must be synchronized - * externally. (A structural modification is any operation that adds or - * deletes one or more mappings; merely changing the value associated - * with an existing key is not a structural modification.) This is - * typically accomplished by synchronizing on some object that naturally - * encapsulates the map. - * If no such object exists, the map should be "wrapped" using the - * {@link Collections#synchronizedSortedMap Collections.synchronizedSortedMap} - * method. This is best done at creation time, to prevent accidental - * unsynchronized access to the map:

- *   SortedMap m = Collections.synchronizedSortedMap(new TreeMap(...));
- * - *

The iterators returned by the {@code iterator} method of the collections - * returned by all of this class's "collection view methods" are - * fail-fast: if the map is structurally modified at any time after - * the iterator is created, in any way except through the iterator's own - * {@code remove} method, the iterator will throw a {@link - * ConcurrentModificationException}. Thus, in the face of concurrent - * modification, the iterator fails quickly and cleanly, rather than risking - * arbitrary, non-deterministic behavior at an undetermined time in the future. - * - *

Note that the fail-fast behavior of an iterator cannot be guaranteed - * as it is, generally speaking, impossible to make any hard guarantees in the - * presence of unsynchronized concurrent modification. Fail-fast iterators - * throw {@code ConcurrentModificationException} on a best-effort basis. - * Therefore, it would be wrong to write a program that depended on this - * exception for its correctness: the fail-fast behavior of iterators - * should be used only to detect bugs. - * - *

All {@code Map.Entry} pairs returned by methods in this class - * and its views represent snapshots of mappings at the time they were - * produced. They do not support the {@code Entry.setValue} - * method. (Note however that it is possible to change mappings in the - * associated map using {@code put}.) - * - *

This class is a member of the - * - * Java Collections Framework. - * - * @param the type of keys maintained by this map - * @param the type of mapped values - * - * @author Josh Bloch and Doug Lea - * @see Map - * @see HashMap - * @see Hashtable - * @see Comparable - * @see Comparator - * @see Collection - * @since 1.2 - */ - public class TreeMap extends AbstractMap implements NavigableMap, Cloneable, java.io.Serializable diff --git a/notes/src/TreeSet.java b/notes/src/TreeSet.java index 3eee2a8e..1f0d9490 100644 --- a/notes/src/TreeSet.java +++ b/notes/src/TreeSet.java @@ -1,94 +1,6 @@ -/* - * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ package java.util; -/** - * A {@link NavigableSet} implementation based on a {@link TreeMap}. - * The elements are ordered using their {@linkplain Comparable natural - * ordering}, or by a {@link Comparator} provided at set creation - * time, depending on which constructor is used. - * - *

This implementation provides guaranteed log(n) time cost for the basic - * operations ({@code add}, {@code remove} and {@code contains}). - * - *

Note that the ordering maintained by a set (whether or not an explicit - * comparator is provided) must be consistent with equals if it is to - * correctly implement the {@code Set} interface. (See {@code Comparable} - * or {@code Comparator} for a precise definition of consistent with - * equals.) This is so because the {@code Set} interface is defined in - * terms of the {@code equals} operation, but a {@code TreeSet} instance - * performs all element comparisons using its {@code compareTo} (or - * {@code compare}) method, so two elements that are deemed equal by this method - * are, from the standpoint of the set, equal. The behavior of a set - * is well-defined even if its ordering is inconsistent with equals; it - * just fails to obey the general contract of the {@code Set} interface. - * - *

Note that this implementation is not synchronized. - * If multiple threads access a tree set concurrently, and at least one - * of the threads modifies the set, it must be synchronized - * externally. This is typically accomplished by synchronizing on some - * object that naturally encapsulates the set. - * If no such object exists, the set should be "wrapped" using the - * {@link Collections#synchronizedSortedSet Collections.synchronizedSortedSet} - * method. This is best done at creation time, to prevent accidental - * unsynchronized access to the set:

- *   SortedSet s = Collections.synchronizedSortedSet(new TreeSet(...));
- * - *

The iterators returned by this class's {@code iterator} method are - * fail-fast: if the set is modified at any time after the iterator is - * created, in any way except through the iterator's own {@code remove} - * method, the iterator will throw a {@link ConcurrentModificationException}. - * Thus, in the face of concurrent modification, the iterator fails quickly - * and cleanly, rather than risking arbitrary, non-deterministic behavior at - * an undetermined time in the future. - * - *

Note that the fail-fast behavior of an iterator cannot be guaranteed - * as it is, generally speaking, impossible to make any hard guarantees in the - * presence of unsynchronized concurrent modification. Fail-fast iterators - * throw {@code ConcurrentModificationException} on a best-effort basis. - * Therefore, it would be wrong to write a program that depended on this - * exception for its correctness: the fail-fast behavior of iterators - * should be used only to detect bugs. - * - *

This class is a member of the - * - * Java Collections Framework. - * - * @param the type of elements maintained by this set - * - * @author Josh Bloch - * @see Collection - * @see Set - * @see HashSet - * @see Comparable - * @see Comparator - * @see TreeMap - * @since 1.2 - */ - public class TreeSet extends AbstractSet implements NavigableSet, Cloneable, java.io.Serializable { diff --git a/notes/src/Vector.java b/notes/src/Vector.java index 0d69591a..41cc0277 100644 --- a/notes/src/Vector.java +++ b/notes/src/Vector.java @@ -1,81 +1,5 @@ -/* - * Copyright (c) 1994, 2011, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - package java.util; -/** - * The {@code Vector} class implements a growable array of - * objects. Like an array, it contains components that can be - * accessed using an integer index. However, the size of a - * {@code Vector} can grow or shrink as needed to accommodate - * adding and removing items after the {@code Vector} has been created. - * - *

Each vector tries to optimize storage management by maintaining a - * {@code capacity} and a {@code capacityIncrement}. The - * {@code capacity} is always at least as large as the vector - * size; it is usually larger because as components are added to the - * vector, the vector's storage increases in chunks the size of - * {@code capacityIncrement}. An application can increase the - * capacity of a vector before inserting a large number of - * components; this reduces the amount of incremental reallocation. - * - *

- * The iterators returned by this class's {@link #iterator() iterator} and - * {@link #listIterator(int) listIterator} methods are fail-fast: - * if the vector is structurally modified at any time after the iterator is - * created, in any way except through the iterator's own - * {@link ListIterator#remove() remove} or - * {@link ListIterator#add(Object) add} methods, the iterator will throw a - * {@link ConcurrentModificationException}. Thus, in the face of - * concurrent modification, the iterator fails quickly and cleanly, rather - * than risking arbitrary, non-deterministic behavior at an undetermined - * time in the future. The {@link Enumeration Enumerations} returned by - * the {@link #elements() elements} method are not fail-fast. - * - *

Note that the fail-fast behavior of an iterator cannot be guaranteed - * as it is, generally speaking, impossible to make any hard guarantees in the - * presence of unsynchronized concurrent modification. Fail-fast iterators - * throw {@code ConcurrentModificationException} on a best-effort basis. - * Therefore, it would be wrong to write a program that depended on this - * exception for its correctness: the fail-fast behavior of iterators - * should be used only to detect bugs. - * - *

As of the Java 2 platform v1.2, this class was retrofitted to - * implement the {@link List} interface, making it a member of the - * - * Java Collections Framework. Unlike the new collection - * implementations, {@code Vector} is synchronized. If a thread-safe - * implementation is not needed, it is recommended to use {@link - * ArrayList} in place of {@code Vector}. - * - * @author Lee Boynton - * @author Jonathan Payne - * @see Collection - * @see LinkedList - * @since JDK1.0 - */ public class Vector extends AbstractList implements List, RandomAccess, Cloneable, java.io.Serializable