set与iterator

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 sets = new HashSet<>();
        sets.add("hello");
        sets.add("hello");//内容重复只显示一个
        sets.add("world");


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

        }


        //测试
        System.out.println("--------------");
        Set 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 iteratorA = set.iterator();
      while (iteratorA.hasNext()){
            System.out.println(iteratorA.next());

        }

    }

}

你可能感兴趣的:(set与iterator)