JAVA--Set接口的两种遍历方法

来源:互联网 发布:域名注册 便宜 编辑:程序博客网 时间:2024/05/17 10:43
import java.util.HashSet;import java.util.Set;import javax.swing.text.html.HTMLDocument.Iterator;public class SetTest {/* * 1.Set 和list很相似,但是又有一些本质上色区别。 * 2。Set是无序的,就是说Set是没有角标的。 * 3.Set中是不能放重复元素的(没角标)。 * 4.Set是不能使用基础的for循环遍历的。 * 5.Set的父接口也是Collection。 * *//* * 看Set的创建过程 * Set是一个接口,和list一样 * 创建Set有两个过程,一个是使用Set接口自己创建Set, * 另一个是使用JAVA提供的方法或类,完成一个Set对象的创建过程 * 而Java提供的Set接口的实现由很多种,我们最常用的是HasSet。 * */Set<NewsArticle>set= new HashSet<>();/* * 遍历1 * */public void bianli(Set<NewsArticle>set){for (NewsArticle newsArticle : set) {System.out.println(newsArticle);}}/* * 遍历 2 * *///public void bianli2(Set<NewsArticle>set){//while(iterator.hasNext()){//}//}
public static void main(String[] args) {
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Monaco; color: #4e9072}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Monaco; min-height: 17.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Monaco}span.s1 {color: #7e504f}span.s2 {color: #931a68}span.s3 {color: #3933ff}span.Apple-tab-span {white-space:pre}

/*

* Set的使用过程

* 

* */

Set<NewsArticle>set= new HashSet<>();

set.add(new NewsArticle("ll","gg","cc"));

set.add(new NewsArticle("ss","aa","dd"));

SetTest setTest=new SetTest();

setTest.bianli(set);

        }
}

原创粉丝点击