Collection的各種子類
java集合框架支持三種主要類型的集合:規(guī)則集(Set),線性集(List)和隊(duì)列(Queue)。Set的實(shí)例用于存儲(chǔ)不重復(fù)的元素,List的實(shí)例用于存儲(chǔ)一個(gè)有元素構(gòu)成的有序集合,Queue的實(shí)例用于存儲(chǔ)先進(jìn)先出方式處理的對(duì)象。
Set具體類是:散列類HashSet,鏈?zhǔn)缴⒘蓄怢inkedHashSet,樹形集TreeSet。HashSet的默認(rèn)初始容量16而客座率為0.75.
List 具體: ArrayList 和 LinkedList 具體區(qū)別就是 linkedList可以在任意位置插入刪除等操作,而arraylist的好處就是效率高
談一下規(guī)則集和線性表的效率性能。
import java.util.*;
public class SetListPerformanceTest
{
public static void main(String[] args)
{
Collection<Integer> set1 = new HashSet<Integer>();//HashSet
System.out.println("Time"+getTestTime(set1,5000)+"milliseconds");
Collection<Integer> set2 = new LinkedHashSet<Integer>();//LinkedHashSet
System.out.println("Time"+getTestTime(set2,5000)+"milliseconds");
Collection<Integer> set3 = new TreeSet<Integer>();//TreeSet
System.out.println("Time"+getTestTime(set3,5000)+"milliseconds");
Collection<Integer> set4 = new ArrayList<Integer>();//ArrayList
System.out.println("Time"+getTestTime(set4,5000)+"milliseconds");
Collection<Integer> set5 = new LinkedList<Integer>();//LinkedList
System.out.println("Time"+getTestTime(set5,5000)+"milliseconds");
}
public static long getTestTime(Collection<Integer> c,int size){
long startTime = System.currentTimeMillis();
//add numbers 1,2,3,......size-1 to the array list
List<Integer> list = new ArrayList<Integer>();
for(int i=0;i<size;i++){
list.add(i);
}
Collections.shuffle(list);//shuffle the array
for(int element : list)
c.add(element);
Collections.shuffle(list);
for(int element:list)
c.remove(element);
long endTime = System.currentTimeMillis();
return endTime-startTime;
}
}
通過上面的列子可以發(fā)現(xiàn) 規(guī)則集比線性表的效率高。但是還是要按照具體需求去選擇。
ArrayList與vector 唯一不同的是訪問和修改向量的同步方法。使用ArrayList效率比vector高。
stack是vector的子類、
java教程http://www.software8.co/wzjs/java/1873.html
隊(duì)列(Queue)和優(yōu)先隊(duì)列(priorityQueue):隊(duì)列是一種先進(jìn)先出的數(shù)據(jù)結(jié)構(gòu)。元素被追加在隊(duì)尾,然后在隊(duì)頭被刪除。
優(yōu)先隊(duì)列中 元素被賦予優(yōu)先級(jí)。最高優(yōu)先級(jí)的元素先被刪除
linkedList 實(shí)現(xiàn)了Deque接口,Deque接口又拓展了Queue接口。 因此可以用LinkedList接口來創(chuàng)建一個(gè)隊(duì)列。
圖(Map):散列圖(HashMap),鏈?zhǔn)缴⒘袌D(LinkedHashMap)和樹形圖(TreeMap)
HashMap中條目的順序是隨機(jī)的。TreeMap的條目是按照升序排列的。LinkedHashMap中的條目是按元素最后一次唄訪問的時(shí)間從早到晚排序的。
import java.util.*;
public class CountWordOfCurrent
{
public static void main(String[] args)
{
//set text in a string
String text = "Good moring. Have a good class."+"Have a good visit. Have fun!";
//create a treemap to hold words as key and count as value
TreeMap<String, Integer> map = new TreeMap<String, Integer>();
String[] words = text.split("[ \n\t\r.,;!:?(){]");
for(int i=0;i<words.length;i++){
String key = words[i].toLowerCase();
if(key.length()>0){
if(map.get(key) == null){
map.put(key,1);
}
else{
int value = map.get(key).intValue();
value++;
map.put(key,value);
}
}
}
//create all entries into a set
Set<Map.Entry<String,Integer>> entrySet = map.entrySet();
//get key and value from each entry
for(Map.Entry<String,Integer> entry : entrySet){
System.out.println(entry.getValue()+"\t"+entry.getKey());
}
}
}
posted on 2012-12-18 09:41 你爸是李剛 閱讀(1507) 評(píng)論(0) 編輯 收藏