HashSet用法

来源:互联网 发布:机房环境监控软件 编辑:程序博客网 时间:2024/06/05 11:12
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");    }}


 

原创粉丝点击