HashSet内的元素遍历

import java.util.Set;
import java.util.HashSet;
import java.util.Iterator;
 
public class SetTest {
    public static void main(String[] args) {
        Set<String> setExample = new HashSet<String>();
        
        setExample.add("aaa");
        setExample.add("bbb");
        setExample.add("ccc");
 
        //利用collection的Iterator遍历
        Iterator iterator = setExample.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());            
        }    
        
        //或者这样
        for (String str:setExample ) {
            System.out.println(str);
        }
    }
}
————————————————
转自CSDN博客
原文链接:https://blog.csdn.net/u011402596/java/article/details/38523443

 

posted @ 2020-06-08 18:34  zeroPatrick  阅读(2202)  评论(0编辑  收藏  举报