1.java集合类主要有一下几种
(1)list结构的集合
ArrayList类 Linklist类 Vector类 Stack类
(2)Map结构类集合
HashMap类 Hashtable类
(3)set结构的集合类
HashSet类 TreeSet类
(4)Queue结构的集合
Queue接口
arrayList
ArrayList al = new ArrayList();
Clerk clerk1 = new Clerk("宋江",50);//创建一个职员
al.add(clerk1) // 将clerk1加入到al中;
al.remove(1);//删除一个对象
Clerk temp = (Clerk)al.get(0);
System.out.println("第一个名字是:"+temp.getName());
for(int i=0;i<al.size();i++){
Clerk temp = (Clerk)al.get(i);
System.out.println(temp.getName());
}
LinkList
LinkList l1 = new LinkList();
Emp emp1 = new Emp("01","aa",1.2f);
Emp emp2 = new Emp("02","bb",1.2f);
l1.addFirst(emp1);//
l1.addFrist(emp2);//
for(int i=0;i<l1.size();i++){
System.out.println(((emp)l1.get(i)).getName());
}
Vector
Vector v = new Vector();
Emp emp1 = new Emp("1","aa",1.2f);
v.add(emp1);
for(int i=0;i<v.size();i++){
Emp emp = (Emp).v.get(i);
}
HashMap
HashMap hm = new HashMap();
Emp emp1= new Emp("s001","aaa",3.4f);
Emp emp2= new Emp("s002","bbb",3.4f);
hm.put("s001",emp1);
hm.put("s002",emp2);
if(hm.containKey("s002")){//查找编号s002
System.out.println("有该员工");
Emp emp = (Emp)hm.get("S002");
System.out.println(emp.getName());
}else{
System.out.println("没有该员工");
}
Iterator it = hm.keySet().iterator();//遍历HashMap中所有的key和value
while(it.hasNext()){
String key = it.next().toString();//取出key
Emp emp = (Emp)hm.get(key);//通过key取出value
System.out.println("名字:"+emp.getName());
System.out.println("薪水:"+emp.getSal());
}
HashTable
for(Iterator it = ht.keySet().iterator();it.hashNext();){
String key = it.next().toString();
Emp emp = (Emp)ht.get(key);
}
2.HashMap 和Hashtable的区别
Hashtable是同步的,线程安全 执行效率低
HashMap是异步的 不是线程安全
HashMap可以将空值作为一个标的条目key或value
Hashtable不能放入空值null
3.Arraylist 和Vector的区别
Vector同步 Arraylist异步
Vector 缺省情况下自动增长原来一倍的数组长度
Arraylist是原来的50%