现在的位置: 首页 > 综合 > 正文

HashSet用法

2013年08月18日 ⁄ 综合 ⁄ 共 672字 ⁄ 字号 评论关闭
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");
    }
}

 

抱歉!评论已关闭.