HashSet用法

package 容器類;


import java.util.*;
 
class A1{
    private int a;
    public A1(int a){
        this.a = a;
    }
    public int getA1() {
        return a;
    }
}
 
public class set1 {
    public static void main(String[] args) {
        HashSet<Object> hs =  new HashSet<Object>();
        A1 aa = new A1(0);
        hs.add(aa);
        hs.add(aa);
        hs.add(new A1(5));
        hs.add(new A1(5));
        hs.add(4);
        hs.add(4);
        hs.add("111");
        hs.add("111");
        Iterator<Object> it = hs.iterator();
        while(it.hasNext()){
            Object o = (Object)it.next();
            if(o instanceof A1) {
                A1 m = (A1)o;
                System.out.println(m.getA1()+" yes1");
            }
            if(o instanceof String) {
                String s = (String)o;
                System.out.println(s + "yes2");
            }
            if(o instanceof Integer){
                Integer i = (Integer)o;
                System.out.println(i);
            }
        }
        //System.out.println(hs.size());
        //if(hs.contains(5)) System.out.println("yes");
    }
}


 

發佈了161 篇原創文章 · 獲贊 21 · 訪問量 17萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章