package com.test1;
import java.util.*;
public class Demo4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
LinkedList ll=new LinkedList();
Emp emp1=new Emp("sa01", "aa",1.2f);
Emp emp2=new Emp("sa02", "bb",1.2f);
//表示把emp1加在链表的最前面
ll.addFirst(emp1);
ll.addFirst(emp2);
// ll.addLast(emp1);
// ll.addLast(emp2);
for(int i=0;i<ll.size();i++)
{
System.out.println(((Emp)ll.get(i)).getName());
}
//Vector(向量)的用法
Vector vv=new Vector();
Emp emp3=new Emp("1", "c",5);
vv.add(emp3);
for(int i=0;i<vv.size();i++)
{
Emp emp=(Emp)vv.get(i);
}
//Stack(栈)
Stack stack=new Stack();
//创建一个HashMap对象
HashMap hm=new HashMap();
Emp emp4=new Emp("sa001", "dd", 3);
Emp emp5=new Emp("sa002", "ff", 3);
//将emp放入到hm
hm.put("sa001", emp4);
hm.put("sa002", emp5);
//如果要查找编号是sa002
if(hm.containsKey("sa002"))
{
System.out.println("有该员工");
//取出
Emp emp=(Emp)hm.get("sa002");
System.out.println("名字:"+emp.getName());
}else{
System.out.println("没有该员工");
}
//遍历HashMap中所有的key和value
//Iterator迭代
Iterator it=hm.keySet().iterator();
//hasNext()返回一个布尔值
while(it.hasNext())
{
//取出key
String key=it.next().toString();
//通过key取出value
Emp emp=(Emp)hm.get(key);
System.out.println("名字:"+emp.getName());
System.out.println("薪水:"+emp.getSal());
}
//Hashtable
Hashtable ht=new Hashtable();
Map hm2=new HashMap();
hm2.put(null, null);
System.out.println("测试:"+hm2.get(null));
} |
|