![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package tet; import java.util.HashSet; import java.util.Iterator; public class Test { public static void main(String[] args) { HashSet<String> hs=new HashSet<String>(); hs.add("jia"); hs.add("hong"); hs.add("ni "); hs.add("shi"); hs.add("zui"); hs.add("bang"); hs.add("de"); Iterator<String> it=hs.iterator(); while(it.hasNext()) System.out.println(it.next()); HashSet<Integer> in=new HashSet<Integer>(); in.add(65); in.add(1); in.add(85); Iterator<Integer> le=in.iterator(); while(le.hasNext()){ System.out.println(le.next()); } } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package list_java270; public class Test { public static void main(String[] args) { MyStack<String> str=new MyStack<String>(); str.push("asdf"); str.push("adf"); str.push("dsfdf"); str.push("aafdssdf"); str.push("dsf"); String element; while((element=str.pop())!=null){ System.out.println(element); } MyStack<Integer> in=new MyStack<Integer>(); in.push(12); in.push(1); in.push(85); Integer le; while((le=in.pop())!=null){ System.out.println(le); } } }
或者用HashSet实现
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package list_java270; import java.util.ArrayList; public class MyStack<T> { private ArrayList<T> al=new ArrayList<T>(); public void push(T e){ al.add(e); } public T pop(){ if(al.size()>0){ return al.remove(al.size()-1); } else return null; } public int size(){ return al.size(); } }