关于Collection的各种子类

java集合框架支持三种主要类型的集合:规则集(Set),线性集(List)和队列(Queue)。Set的实例用于存储不重复的元素,List的实例用于存储一个有元素构成的有序集合,Queue的实例用于存储先进先出方式处理的对象。

 

Set具体类是:散列类HashSet,链式散列类LinkedHashSet,树形集TreeSet。HashSet的默认初始容量16而客座率为0.75.

 

List 具体: ArrayList 和 LinkedList 具体区别就是 linkedList可以在任意位置插入删除等操作,而arraylist的好处就是效率高

 

谈一下规则集和线性表的效率性能。

 

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;
}
}

 

通过上面的列子可以发现 规则集比线性表的效率高。但是还是要按照具体需求去选择。

 

 

ArrayList与vector 唯一不同的是访问和修改向量的同步方法。使用ArrayList效率比vector高。

stack是vector的子类、

 

队列(Queue)和优先队列(priorityQueue):队列是一种先进先出的数据结构。元素被追加在队尾,然后在队头被删除。

优先队列中 元素被赋予优先级。最高优先级的元素先被删除

linkedList 实现了Deque接口,Deque接口又拓展了Queue接口。 因此可以用LinkedList接口来创建一个队列。

 

 

 

 

图(Map):散列图(HashMap),链式散列图(LinkedHashMap)和树形图(TreeMap)

HashMap中条目的顺序是随机的。TreeMap的条目是按照升序排列的。LinkedHashMap中的条目是按元素最后一次呗访问的时间从早到晚排序的。

 

 

 

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());
}
}
}

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值