1、利用迭代器进行迭代遍历:
Set<Object> <a href="https://www.gaodaima.com/tag/set" title="查看更多关于set的文章" target="_blank">set</a>s = new HashSet<Object>(); Iterator<Object> it = set.iterator(); while (it.hasNext()) { String str = (String)it.next(); System.out.println(str); }
www#gaodaima.com来源gao!%daima.com搞$代*!码网搞代码
2、for循环遍历:
for (String str : sets) { System.out.println(str); }
3、foreach循环遍历:
Set<Object> set = new HashSet<Object>(); for (Object obj: sets) { if(obj instanceof Integer){ int aa= (Integer)obj; }else if(obj instanceof String){ String aa = (String)obj } …….. }
搞代码网,大量的免费python教程,欢迎在线学习!
来源:搞代码网:原文地址:https://www.gaodaima.com