Hashset解析

由hash表支持,它不保證set的迭代順序,特別是它不保證改順序恆久不變。

//構造一個空的set
		HashSet<Integer> set = new HashSet<Integer>();
		for(int i=0;i<10;i++) {
			set.add(i);
		}
		/*
		 * add() 如果set中不包含指定的元素,則添加指定的元素 
		 */
	Iterator<Integer> ll = set.iterator();
		/*
		 * 返回對set中的元素進行迭代的迭代器。返回元素的順序是排序好的順序(升序排列)
		 */
		while(ll.hasNext()) {
			System.out.println(ll.next());
		}
System.out.println(set.size());
		//返回set中元素的數量
System.out.println(set.isEmpty());
		//如果set中不包含任何元素,就返回true
		System.out.println(set.contains(23));
		//如果set中已經包含這個元素就返回true,如果不包含就返回false
		System.out.println(set.remove(4));
		//如果指定的元素存在set中,我們就把它移除,並返回true,否則的話返回false
		set.clear();
		//pubic void clear() 清除set中的所有元素
HashSet<Integer> set2 = (HashSet<Integer>) set.clone();
		//返回hashset的淺表副本,並沒有複製元素的本身

並沒有實際的把值給複製過來,還是用的原來的地址空間。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章