Java數(shù)據(jù)結(jié)構(gòu)總結(jié)之Set

HashSet存儲(chǔ)自定義類(lèi)StudentSet接口:是不包括重復(fù)元素的Collection接口

1.首先創(chuàng)建數(shù)據(jù)元素Student,用于存儲(chǔ)到HashSet中,并重寫(xiě)equals方法和hashCode方法。

代碼如下:

常用的方法:

1.size():獲取Set尺寸(即Set包含數(shù)據(jù)元素的總數(shù))

2.add(Object obj):向Set中添加數(shù)據(jù)元素obj

3.remove(Object obj):從Set中刪除數(shù)據(jù)元素obj

4.contains(Object obj):判斷當(dāng)前Set中是否包含數(shù)據(jù)元素obj,如果包含返回true,否則返回false

5.iterator():將Set裝入迭代器

集合運(yùn)算:

1.addAll(Collection c):向Set中添加c包含的全部數(shù)據(jù)元素(并集)

2.removeAll(Collection c):從Set中移除c包含的全部數(shù)據(jù)元素(差集)

3.retainAll(Collection c):求Set和c的交集

4.containsAll(Collection c):判斷Set是否包含c中的全部數(shù)據(jù)元素,如果全部包含返回true,否則返回false

Set常用方法(示例)

Set<String> hSet = new HashSet<String>();

/** 向HashSet中添加數(shù)據(jù)元素 */

hSet.add("AA");

hSet.add("BB");

hSet.add("CC");

/** 注意:當(dāng)向Set中添加重復(fù)數(shù)據(jù)元素時(shí),將添加失敗。

? *例如:下面又向Set中添加了一個(gè)"AA"是無(wú)效的 ?*/

hSet.add("AA");

/** ?獲取當(dāng)前集合是否包含目標(biāo)元素"BB"

? *如果包含"BB"返回true,否則返回false ?*/

boolean bHash = hSet.contains("BB");

System.out.println(bHash);

/** ?刪除集合中的數(shù)據(jù)元素,并返回是否成功

? ?* ?如果要?jiǎng)h除的數(shù)據(jù)元素不在當(dāng)前集合內(nèi),則返回false ?*/

boolean bRemove = hSet.remove("CC");

system.out.println(bRemove);


一、HashSet(散列集):

HashSet通過(guò)Hash算法排布集合內(nèi)的元素,所謂的Hash算法就是把任意長(zhǎng)度的輸入(又叫做預(yù)映射),通過(guò)散列算法,變換成固定長(zhǎng)度的輸出。該輸出就是散列值。這種轉(zhuǎn)換是一種壓縮映射。對(duì)于不同類(lèi)型的信息。其散列值公式亦不完全相同。

當(dāng)我們使用HashSet存儲(chǔ)自定義類(lèi)時(shí),需要在自定義類(lèi)中重寫(xiě)equals和hashCode方法,主要原因是集合內(nèi)不允許有重復(fù)的數(shù)據(jù)元素,在集合校驗(yàn)元素的有效性時(shí)(數(shù)據(jù)元素不可重復(fù)),需要調(diào)用equals和hashCode驗(yàn)證。

注意:HashSet在判斷數(shù)據(jù)元素是否重復(fù)時(shí):

1.檢查hashCode值是否與集合中已有相同。

2.如果hashCode相同再調(diào)用equals方法進(jìn)一步檢查。(equals返回真表示重復(fù))


HashSet(示例)

HashSet存儲(chǔ)自定義類(lèi)Student

1.首先創(chuàng)建數(shù)據(jù)元素Student,用于存儲(chǔ)到HashSet中,并重寫(xiě)equals方法和hashCode方法。

代碼如下:

class ?Student{

? ? ? ? public ?String code;

? ? ? ? public ?String name;

? ? ? ? public ?Student(String code,String name){

? ? ? ? ? ? ? this.code = code;

? ? ? ? ? ? ? this.name = name;

? ? ? ? ?}

? ? ? ? ?public boolean equals(Object o){

? ? ? ? ? ? ? ?if(this == o){

? ? ? ? ? ? ? ? ? ? ? ?return true;

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ?if(o.getClass() == Student.class){

? ? ? ? ? ? ? ? ? ? ? ?Student s = (Student)o;

? ? ? ? ? ? ? ? ? ? ? ? return false;

? ? ? ? ? ? ? ?}

? ? ? ? }

? ? ? ? public int hashCode(){

? ? ? ? ? ? ? ? return this.code.hashCode();

? ? ? ? }

}


2.由于Student類(lèi)重寫(xiě)了equals方法和hashCode方法,定義使用Student的成員變量code來(lái)判斷元素是否重復(fù),所以下面的程序只會(huì)輸出一個(gè)元素,即成員變量name值為T(mén)om的Student。

代碼如下:

package ?com.freelee.set;

import ?java.util.HashSet;

import ?java.util.Iterator;

public ?class HashSetException{

? ? ? ? ?public ?static ?void ?main (String[] ?args) {

? ? ? ? ? ? ? ? HashSet<Student> ?hSet = new ?HashSet<Student>();

? ? ? ? ? ? ? ? Student s1 = new Student("1", "Tom");

? ? ? ? ? ? ? ? hSet.add(s1);

? ? ? ? ? ? ? ?Student s2 = new Student("2", "FreeLee");

? ? ? ? ? ? ? ?hSet.add(s2);

? ? ? ? ? ? ? ?Iterator<Student> i = ?hSet.Iterator();

? ? ? ? ? ? ? ?while (i.hasNext()){

? ? ? ? ? ? ? ? ? ? ? ? Student student ?= ?(Student) i.next();

? ? ? ? ? ? ? ? ? ? ? ? System.out.println(Student);

? ? ? ? ? ? ? ?}

? ? ? ?}

}


二、TreeSet(樹(shù)集)

TreeSet是一個(gè)有序集合,其元素按照升序排序,默認(rèn)是按照自然順序排序,也就是說(shuō)TreeSet中的對(duì)象元素需要實(shí)現(xiàn)Comparable接口。TreeSet類(lèi)中跟HashSet類(lèi)中一樣也沒(méi)有g(shù)et()方法來(lái)獲取指定位置的元素,所以也只能通過(guò)迭代器方法來(lái)獲取。

TreeSet雖然是有序的,但是并沒(méi)有具體的索引,當(dāng)插入一個(gè)新的數(shù)據(jù)元素的時(shí)候,TreeSet中原有的數(shù)據(jù)元素可能需要重新排序,所以TreeSet插入和刪除數(shù)據(jù)元素的效率較低。

當(dāng)我們使用TreeSet存儲(chǔ)自定義類(lèi)時(shí),需要在自定義類(lèi)中重寫(xiě)compareTo方法,以提供比對(duì)形式,否則TreeSet不能對(duì)用戶自定義的類(lèi)型進(jìn)行正確的樹(shù)狀排序。

TreeSet(示例)

1.首先創(chuàng)建數(shù)據(jù)元素類(lèi)Student,用于存儲(chǔ)到TreeSet中,并重寫(xiě)compareTo方法。

class ?Student implements Comparable<Student>{

? ? ? ?String code;

? ? ? ?String name;

? ? ? ?public Student (String code;String name){

? ? ? ? ? ? ? this.code = code;

? ? ? ? ? ? ? this.name = name;

? ? ? }

? ? ? ?/** ?比對(duì)Student轉(zhuǎn)化為比對(duì)Student的code成員變量 ?*/

? ? ? ? public int compareTo(Student s){

? ? ? ? ? ? ? return this.code.compareTo(s.code);

? ? ? ?}

}


2.由于Student類(lèi)重寫(xiě)了compareTo方法,所以比對(duì)兩個(gè)student對(duì)象就轉(zhuǎn)化為比對(duì)兩個(gè)student的code值。

package ?com.freelee.set;

import java.util.Iterator;

import java.util.TreeSet;

public class TestTreeSet{

? ? ? public static void main(String[] arge){\

? ? ? ? ? ? ?TreeSet<student> tSet = new TreeSet<Student>();

? ? ? ? ? ? ?Student s1 = new Student("5", "xiaoming");

? ? ? ? ? ? ?tSet.add(s1);

? ? ? ? ? ? ?Student s2 = new Student("6", "xiaoli");

? ? ? ? ? ? ?tSet.add(s2);

? ? ? ? ? ? ?Iterator<Student> i = tSet.iterator();

? ? ? ? ? ? ?while(i.hasNext()){

? ? ? ? ? ? ? ? ? ?Student student = (Student) i.next();

? ? ? ? ? ? ? ? ? ?System.out.println(Student.code);

? ? ? ? ? ? ? }

? ? ? ? }

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語(yǔ)法,類(lèi)相關(guān)的語(yǔ)法,內(nèi)部類(lèi)的語(yǔ)法,繼承相關(guān)的語(yǔ)法,異常的語(yǔ)法,線程的語(yǔ)...
    子非魚(yú)_t_閱讀 31,765評(píng)論 18 399
  • 上一篇,我們給出了大概35個(gè)題目,都是基礎(chǔ)知識(shí),有童鞋反映題目過(guò)時(shí)了,其實(shí)不然,這些是基礎(chǔ)中的基礎(chǔ),但是也是必不可...
    獨(dú)念白閱讀 255評(píng)論 0 2
  • 面向?qū)ο笾饕槍?duì)面向過(guò)程。 面向過(guò)程的基本單元是函數(shù)。 什么是對(duì)象:EVERYTHING IS OBJECT(萬(wàn)物...
    sinpi閱讀 1,088評(píng)論 0 4
  • 1.import static是Java 5增加的功能,就是將Import類(lèi)中的靜態(tài)方法,可以作為本類(lèi)的靜態(tài)方法來(lái)...
    XLsn0w閱讀 1,262評(píng)論 0 2
  • 世界是個(gè)巨大的夾娃娃機(jī), 你是最珍貴的毛絨玩具, 我沒(méi)有硬幣, 只好隔著玻璃, 眼睜睜地看你。
    百葉行閱讀 134評(píng)論 1 1