Java的集合類型主要有如下幾種:
Set/Queue/List/Set/Tree/Stack等。
我們首先來看一個HashSet的用法:
package com.freesoft.javaadvanced;
import java.util.HashSet;
import com.freesoft.testentity.Kalamata;
import com.freesoft.testentity.Ligurio;
import com.freesoft.testentity.Olive;
import com.freesoft.testentity.Picholine;
public class TestHashSet {
public static void printSize(HashSet<Olive> olives){
System.out.println("There are " + olives.size() + " olives in the set.");
}
public static void main(String[] args) {
HashSet<Olive> olives = new HashSet<>();
Olive k = new Kalamata();
Olive l = new Ligurio();
Olive p = new Picholine();
olives.add(k);
olives.add(l);
printSize(olives);
olives.add(p);
printSize(olives);
// 由于HashSet内部元素需要通過Hash Value識别,是以同一個元素無法儲存兩次
olives.add(p);
printSize(olives);
// HashSet中可以添加null
olives.add(null);
printSize(olives);
// 删除元素
olives.remove(k);
printSize(olives);
}
}
注意每次運作的時候HashSet内元素的順序不是一成不變的。
接下來我們看看如何使用TreeSet。TreeSet是一個自動排序的集合,是以我們需要對泛型對象的類實作Comparable接口:
package com.freesoft.testentity;
public class Olive implements Comparable<Olive>{
public static final long BLACK = 0x000000;
private String name;
private long color = BLACK;
public Olive(){
}
public Olive(String name) {
// 由于字段color已經被設定為預設值為BLACK,故這裡無需再次設定
this.name = name;
}
public Olive(String name, long value) {
super();
this.name = name;
this.color = value;
}
@Override
public String toString() {
return "Olive [name=" + name + ", color=" + color + "]";
}
@Override
public int compareTo(Olive arg0) {
String s1 = this.name;
String s2 = arg0.name;
return s1.compareTo(s2);
}
}
我們再按照如下的規則實作子類:
package com.freesoft.testentity;
public class Kalamata extends Olive {
public Kalamata() {
super("Kalamata");
}
}
最後我們的測試程式是這樣:
package com.freesoft.javaadvanced;
import java.util.HashSet;
import java.util.TreeSet;
import com.freesoft.testentity.Kalamata;
import com.freesoft.testentity.Ligurio;
import com.freesoft.testentity.Olive;
import com.freesoft.testentity.Picholine;
public class TestTreeSet {
public static void printSize(TreeSet<Olive> olives){
System.out.println("There are " + olives.size() + " olives in the set.");
}
public static void main(String[] args) {
Olive l = new Ligurio();
Olive p = new Picholine();
Olive k = new Kalamata();
TreeSet<Olive> olives = new TreeSet<>();
try {
olives.add(k);
olives.add(l);
olives.add(p);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(olives);
}
}
從上面的程式可以看出,随便我們添加的時候的元素順序是什麼,TreeSet在添加的過程中會自動按照排序順序插入元素。
我們在來看看LinkedList這個清單:
package com.freesoft.javaadvanced;
import java.util.LinkedList;
import com.freesoft.testentity.Kalamata;
import com.freesoft.testentity.Ligurio;
import com.freesoft.testentity.Olive;
import com.freesoft.testentity.Picholine;
public class TestLinkedList {
public static void main(String[] args) {
Olive k = new Kalamata();
Olive l = new Ligurio();
Olive p = new Picholine();
LinkedList<Olive> list = new LinkedList<>();
list.add(k);
list.add(l);
list.add(p);
System.out.println(list);
// 這裡的add實際上是執行插入操作,且清單中的元素可以反複添加
list.add(2, k);
System.out.println(list);
// 改變元素屬性,該對象在清單中的引用也會及時更新
k.setColor(0xff0000);
System.out.println(list);
}
}
最後我們看看隊列。隊列的方法比較簡單,就是一個先進先出(FIFO)的操作:
我們先看一下之前的list,我們通過幫助得知LinkedList實作了Queue接口。注意體會peek和poll的差別,一個删除元素一個不删除。
package com.freesoft.javaadvanced;
import java.util.LinkedList;
import com.freesoft.testentity.Kalamata;
import com.freesoft.testentity.Ligurio;
import com.freesoft.testentity.Olive;
import com.freesoft.testentity.Picholine;
public class TestLinkedList {
public static void main(String[] args) {
Olive k = new Kalamata();
Olive l = new Ligurio();
Olive p = new Picholine();
LinkedList<Olive> olives = new LinkedList<>();
olives.add(k);
olives.add(l);
olives.add(p);
System.out.println(olives);
// 這裡的add實際上是執行插入操作,且清單中的元素可以反複添加
olives.add(2, k);
System.out.println(olives);
// 改變元素屬性,該對象在清單中的引用也會及時更新
k.setColor(0xff0000);
System.out.println(olives);
Olive o1 = olives.poll();
System.out.println(o1);
System.out.println(olives);
}
}