set与iterator

2018-01-11  本文已影响0人  bluewind1230
import javax.xml.soap.Text;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class Main {
    public static  class Test {
        @Override
        public boolean equals(Object obj) {
            return true;
        }

        @Override
        public int hashCode() {
            return 0x100;
        }
    }

    public static void main(String[] args) {
    Set<String> sets = new HashSet<>();
        sets.add("hello");
        sets.add("hello");//内容重复只显示一个
        sets.add("world");


        Iterator<String> iterator = sets.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());

        }


        //测试
        System.out.println("--------------");
        Set<Test> set = new HashSet<>();


        Test test = new Test();
        set.add(test);
        set.add(test);
        set.add((new Test()));
        set.add((new Test()));
        set.add((new Test()));

        Iterator<Test> iteratorA = set.iterator();
      while (iteratorA.hasNext()){
            System.out.println(iteratorA.next());

        }

    }

}

上一篇下一篇

猜你喜欢

热点阅读