集合架構

<b>構造方法摘要</b>
<b>ArrayList</b>()
構造一個初始容量為 10 的空清單。
<b>ArrayList</b>(Collection<? extends E> c)
構造一個包含指定 collection 的元素的清單,這些元素是按照該 collection 的疊代器傳回它們的順序排列的。
<b>ArrayList</b>(int initialCapacity)
構造一個具有指定初始容量的空清單。
1.建立一個集合容器
2.建立一個容器,且設定它的下限。
3.建立一個容器,并且設定它的初始空間
<b>通路類型</b><b> 傳回值</b><b> 方法</b>
<b>Boolean </b><b>add</b><code>(</code>E<code>o)</code>
将指定的元素追加到此清單的尾部。
E:代表添加元素的類型
<code><b>Void </b></code><b>add</b><code>(int index,</code>E<code>element)</code>
在清單的指定位置插入指定元素
index:代表插入的位置,如果指定的索引小于0則發
生IndexOutOfBoundsException異常,如果指定添加的元素為null 則發
生<code>NullPointerException</code><code>異常</code>
E:代表插入的元素
<code><b>Void </b></code><b>clear</b><code>()</code>
從清單中移除所有元素。
<code><b>Boolean </b></code><b>contains</b><code>(</code>Object<code>o)</code>
如果此 collection 包含指定的元素,則傳回 true。
<code><b>Boolean </b></code><b>equals</b><code>(</code>Object<code>o)</code>
比較此collection 與指定對象是否相等。
<code><b>Int </b></code><b>hashCode</b><code>()</code>
傳回此collection 的哈希碼值。
<code><b>Boolean </b></code><b>isEmpty</b><code>()</code>
如果此collection 不包含元素,則傳回true。
<code><b>Iterator<E></b></code><code></code><b>iterator</b><code>()</code>
傳回在此collection 的元素上進行疊代的疊代器。
<code><b>Boolean </b></code><b>remove</b><code>(</code>Object<code>o)</code>
從此 collection中移除指定元素的單個執行個體,如果存在 的話(可選操作)。
<code><b>E </b></code><b>remove</b><code>(int index)</code>
移除此清單中指定位置上的元素。
<code><b>Int </b></code><b>size</b><code>()</code>
傳回此collection 中的元素數。
<code><b>Object[] </b></code><b>toArray</b><code>()</code>
傳回包含此collection 中所有元素的數組
<code><b>Boolean </b></code><b>retainAll</b><code>(</code>Collection<code><?> c)</code>
取交集,僅保留目前Collection中和指定Collection中 相同的元素
<code><b>Boolean </b></code><b>removeAll</b><code>(</code>Collection<code><?> c)</code>
移除此collection 中那些也包含在指定 collection 中 的所有元素(可選操作)。
<code><b>Booelan </b></code><b>addAll</b><code>(</code>Collection<code><? extends</code>E<code>> c)</code>
一次添加一堆元素,将指定collection 中的所有元素都 添加到此 collection中(可選操作)。
<code><b>Int </b></code><b>lastIndexOf</b><code>(</code>Object<code>elem)</code>
傳回指定的對象在清單中最後一次出現的位置索引。
<code><b>E </b></code><b>set</b><code>(int index,</code>E<code>element)</code>
用指定的元素替代此清單中指定位置上的元素。
<code><b>Void </b></code><b>trimToSize</b><code>()</code>
将此 ArrayList 執行個體的容量調整為清單的目前大小。
<code><b>Object </b></code><b>clone</b><code>()</code>
傳回此 ArrayList 執行個體的淺表複制。
<b>其它集合方法請查閱java se </b><b>幫助文檔</b>
在JDK1.2 Vector才加入集合架構 ,而且此集合是同步的。因為Vector的方法名稱過于長,在1.2之後被ArrayList 取代,在Vector 可以使用Enumeration 來取出元素。獨有方法
Enumeration <E><code>elements</code><code>()</code>
傳回此向量的元件的枚舉。
在實際的開發中比任何合集的查詢速度都要快,但時ArrayList對于,删除,較慢,而且ArrayLIst 是不同步的。在底層ArrayList的資料結構是,按照下标的結構來存儲的,可以随機通路,是以當查詢的時候速度很快,但是在删除的時候,比如目前ArrayList 有1000 個元素,删除索引5中的元素,那麼就意味着索引5之後的所有的元素将全部向前推進,是以删除的速度是相當慢的
對于則增加,删除,修改,删除,首選是LinkedLIst 因為在底層的資料結構是連結清單的形式出現,當删除某一個的時候僅僅隻需要讓前面的元素重新鍊上下一個即可,反之如果LinkeList對于查詢,時很慢的,比如目前LinkedList有1000個元素,查詢索引為100中的元素,此時的LinkeList會從地0個元素一個找,直到找到為止,
Java se幫助文檔的解釋
所有操作都是按照雙重連結清單的需要執行的。在清單中編索引的操作将從開頭或結尾周遊清單(從靠近指定索引的一端), LinkeList 也不是同步的。
HashSet是無序的,底層資料結構是哈希表,該集合是線程不同步的,HashSet 集合是通過hashCode方法和equals方法來完成的唯一性的判斷。
如果hashCode值相同,再繼續判斷元素的equals方法是否為true
如果hashCode值不同,不會判斷equals方法。 這就是為什麼在複寫equals 方法的時候也要複寫hashCode 方法,因為如果不複寫hashCode 方法在HashSet 比較的時候基本上不會相等,測試代碼
public class test3 {建立一個test3的類, 隻包含 name 屬性
String name;
@Override
public int hashCode() {
return 1;
}
public boolean equals(Object obj) {
test3t = (test3)obj;
if(this.name==t.name ){
return true;
return super.equals(obj);
public class test2{
public static void main(String[]args) {
test3 t1 = new test3();
test3 t2 = new test3();
test3 t3 = new test3();
test3 t4 = new test3();
test3 t5 = new test3();
test3 t6 = new test3();
HashSet<test3>h = new HashSet<test3>();
h.add(t1);
h.add(t2);
h.add(t3);
h.add(t4);
h.add(t5);
h.add(t6);
System.out.println(h.size()); 列印的結果大小是1
是按照自然順來排序,底層資料結構是對象的hashCode 值,和<b>Comparable</b>接口聯合使用實作自然順序,來排序。測試代碼
<b>public</b> <b>class</b> test3<b>implements</b> Comparable<Object>{
<b>int</b> age;
<b>public</b> <b>int</b> compareTo(Object o) {
<b>if</b>(!(o <b>instanceof</b> test3))
<b>throw</b> <b>new</b> RuntimeException("轉換異常");
test3t = (test3)o;
<b>if</b>(<b>this</b>.name.hashCode() >t.name.hashCode())
<b>return</b> 1;
<b>if</b>(<b>this</b>.name.hashCode()== t.name.hashCode()){
<b>return</b> 0;// 此代碼也可以調用String中的方法。
//return this.name.compareTo(t.name);//
<b>return</b> -1;
<b>public</b> test3(String name ,<b>int</b> age){
<b>this</b>.name= name;
<b>this</b>.age =age;
<b>public</b> <b>class</b> test2{
<b>public</b> <b>static</b> <b>void</b> main(String[]args) {
test3 t1 = <b>new</b> test3("d",23);
test3 t2 = <b>new</b> test3("a",2);
test3 t3 = <b>new</b> test3("c",43432);
test3 t4 = <b>new</b> test3("v",6754);
test3 t5 = <b>new</b> test3("e",342432);
test3 t6 = <b>new</b> test3("w",234);
TreeSet <test3>h = <b>new</b> TreeSet <test3>();
Iterator<test3>it= h.iterator();
<b>while</b>(it.hasNext()){
System.out.println(it.next().name);