以下內容來自marschen的java4android視頻教程
類集架構就是Jdk提供的類與介面.
本集主要內容.
1. collection和iterator介面
2. set與hashSet的使用方法
collection介面
boolean add 向集合當中加入一個對象
clear 刪除集合當中的所有對象
isEmpty 判斷集合是否為空白
remove 從集合中刪除一個對象的引用
int size(); 返回集合中元素的數目.
collection的一個子介面就是set.
set介面有一個實作類別,就是hastSet.
import java.util.Set;import java.util.HashSet;public class Test{public static void main(String args[]){HashSet<String> hashSet = new HashSet<String>();Set<String> set = hashSet;//Set<Stinrg> set = new HashSet<String>();boolean b1 = set.isEmpty();System.out.println(b1);set.add("a");set.add("b");set.add("c");set.add("d");boolean b2 = set.isEmpty();System.out.println(b2);int i = set.size();System.out.println("remove之前set對象的長度是" + i);//set.clear();set.remove("a");int j = set.size();System.out.println("remove之後set對象的長度是" + j);}}
import java.util.Set;import java.util.HashSet;import java.util.Iterator;public class Test1{public static void main(String args[]){//HashSet<String> hashSet = new HashSet<String>();//Set<String> set = hashSet;//Iterator<--Collection<---Set <-----HashSet/// <---List<----ArrayList//hasNext() next() Set<String> set = new HashSet<String>();set.add("a");set.add("b");set.add("c");set.add("d");//調用Set對象的iterator方法會產生一個迭代器對象,該對象用於便溺整個對set.Iterator<String> it = set.iterator();//產生一個迭代器的對象./*boolean b1 = it.hasNext();if(b1){String s = it.next();System.out.println(s);}boolean b2 = it.hasNext();if(b2){String s = it.next();System.out.println(s);}*/while(it.hasNext()){String s = it.next();System.out.println(s);}}}