例如,取得最大值、排序、thread-safe等
所有方法皆為已實作好的static方法,可直接使用,不用再新建物件實體
將欲處理的集合直接傳入即可,需先import library
import java.util.Collections;
import java.util.Arrays;
工作忙了,事情多了,越發越少,備忘錄就到這個段落吧!
import java.util.Collections;
import java.util.Arrays;
Vector v = new Vector();
v.add("data");
for(Object obj:v){
String data = (String) obj;
System.out.println(data);
}
Vector v = new Vector(); // 泛型寫法
v.add("data");
for(Object obj:v){
String data = obj; // 不必轉型
System.out.println(data);
}
void showVector (Vector v){} // 仍然是可加或可不加
void showVector (Vector v){} // ?也可用其他無意義代號來取代
void showVector (Vector v){} // Number包含Interger和Float
import java.util.*;
public class Ex1 {
public static void main(String[] args) {
PriorityQueue<String> pq = new PriorityQueue<String> ();
pq.offer("c");
pq.offer("a");
pq.offer("b");
String s;
while((s = pq.poll()) != null) {
System.out.print(s + ", ");
}
}
}
##ReadMore## public PriorityQueue(int initialCapacity, Comparator<? super E> comparator)
public class Ex2 {
public static void main(String[] args) {
Comparator<String> c = new Comparator<String>(){
public int compare(String a, String b){
return a.compareTo(b) * -1;
}
};
PriorityQueue<String> pq = new PriorityQueue<String> (3, c);
pq.offer("c");
pq.offer("a");
pq.offer("b");
String s;
while((s = pq.poll()) != null) {
System.out.print(s + ", ");
}
}
}
不要使用原始的add()和remove(),在Queue中會丟出exception,
以offer()和poll()來代替
方法 | 傳回值 | 說明 |
offer(E o) | boolean | 加入物件 |
peek() | E | 取得物件,若空傳回null |
element() | E | 取得物件,若空傳回例外 |
poll() | E | 取得物件,並移除該物件,若空傳回null |
remove() | E | 取得物件,並移除該物件,若空傳回例外 |
import java.util.*;
public class Ex {
public static void main(String[] args) {
Queue q = new LinkedList();
q.offer("First");
q.offer("Second");
q.offer("Third");
Object o;
System.out.println(q.toString());
while((o = q.poll()) != null) {
String s = (String)o;
System.out.println(s);
}
System.out.println(q.toString());
}
}
import java.util.*;
public class Ex {
public static void main(String[] args) {
Vector v = new Vector();
v.add("apple");
v.add("banana");
v.add("cookie");
for(Object obj:v){
String data = (String)obj;
System.out.print(data + ", ");
}
System.out.println();
}
}
##ShowAll##
集合介面 | 排序性 | 順序性 | 不予許重複 | 使用鍵值 |
SortedSet | ‧ | ‧ | ||
SortedMap | ‧ | ‧ | ||
HashMap | ‧ | |||
Hashtable | ‧ | |||
TreeMap | ‧ | ‧ | ||
LinkedHashMap | ‧ | ‧ | ||
HashSet | ‧ | |||
TreeSet | ‧ | ‧ | ||
LinkedHashSet | ‧ | ‧ | ||
ArrayList | ‧ | |||
Vector | ‧ | |||
LinkedList | ‧ |
Iterator範例:
import java.util.*;
public class Ex {
public static void main(String[] args) {
HashSet hs = new HashSet();
hs.add("one");
hs.add("two");
hs.add("tree");
hs.add("four");
Iterator it = hs.iterator();
while(it.hasNext()){
String data = (String)it.next();
System.out.print(data + ", ");
}
System.out.println();
}
}
/* equals()覆寫例子 */
public boolean equals(Object obj) {
//若同一物件則true
if (this == obj) return true;
//getClass()會可用來判斷物件是否屬於同一類別
if(obj != null && getClass() == obj.getClass()){
if(obj instanceof Ball) {
Ball ball = (Ball)obj;
//比對屬性內容值
if (tradeMark.equals(ball.tradeMark) &&
kind.equals(ball.kind) &&
color.equals(ball.color)) {
return true;
}
}
}
return false;
}
/* hashCode()覆寫例子 */
import org.apache.commons.lang.builder.HashCodeBuilder;//加入外掛
public int hashCode() {
return new HashCodeBuilder(17, 37). //應放入兩個質數
append(tradeMark). //利用append()加入參與的物件變數
append(kind).
append(color).
toHashCode(); //最後取得hash code數值
}