java中set接口使用方法詳解
java中的set接口有如下的特點(diǎn):
不允許出現(xiàn)重復(fù)元素;
集合中的元素位置無順序;
有且只有一個(gè)值為null的元素。
因?yàn)閖ava中的set接口模仿了數(shù)學(xué)上的set抽象,所以,對(duì)應(yīng)的數(shù)學(xué)上set的特性為:
互異性:一個(gè)集合中,任何兩個(gè)元素都認(rèn)為是不相同的,即每個(gè)元素只能出現(xiàn)一次。
無序性:一個(gè)集合中,每個(gè)元素的地位都是相同的,元素之間是無序的。集合上可以定義序關(guān)系,定義了序關(guān)系后,元素之間就可以按照序關(guān)系排序。但就集合本身的特性而言,元素之間沒有必然的序。
空集的性質(zhì):空集是一切集合的子集
Set不保存重復(fù)的元素。Set中最常被使用的是測(cè)試歸屬性,你可以很容易的詢問某個(gè)對(duì)象是否在某個(gè)Set中。Set具有與Collection完全一樣的接口,因此沒有任何額外的功能。實(shí)際上Set就是Collection,只是行為不同。
實(shí)現(xiàn)了Set接口的主要有HashSet、TreeSet、LinkedHashSet這幾個(gè)共同點(diǎn)就是每個(gè)相同的項(xiàng)只保存一份。他們也有不同點(diǎn),區(qū)別如下:
1.HashSet:
HashSet使用的是相當(dāng)復(fù)雜的方式來存儲(chǔ)元素的,使用HashSet能夠最快的獲取集合中的元素,效率非常高(以空間換時(shí)間)。會(huì)根據(jù)hashcode和equals來龐端是否是同一個(gè)對(duì)象,如果hashcode一樣,并且equals返回true,則是同一個(gè)對(duì)象,不能重復(fù)存放。
package cn.set;
import java.util.HashSet;
import java.util.Set;
class Student{
int id;
public Student(int id) {
this.id = id;
}
@Override
public String toString() {
return this.id+"";
}
@Override
public int hashCode() {
return this.id;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Student){
Student stu = (Student) obj;
if (stu.id == this.id)
return true;
}
return false;
}
}
public class HashSetTest {
public static void main(String[] args) {
Set<Student> set = new HashSet<Student>();
Student s1 = new Student(1);
Student s2 = new Student(1);
Student s3 = new Student(2);
set.add(s1);
set.add(s2);
set.add(s3);
for (Student s : set) {
System.out.println(s);
}
}
}
正如上例所示,重寫了hashCode()和equals()方法來區(qū)分同意對(duì)象后,就不能存放同以對(duì)象了。如果注釋這兩個(gè)方法,則所有Student對(duì)象視為不同對(duì)象,都可以存放。
2.TreeSet
TreeSet也不能存放重復(fù)對(duì)象,但是TreeSet會(huì)自動(dòng)排序,如果存放的對(duì)象不能排序則會(huì)報(bào)錯(cuò),所以存放的對(duì)象必須指定排序規(guī)則。排序規(guī)則包括自然排序和客戶排序。
?、僮匀慌判颍篢reeSet要添加哪個(gè)對(duì)象就在哪個(gè)對(duì)象類上面實(shí)現(xiàn)java.lang.Comparable接口,并且重寫comparaTo()方法,返回0則表示是同一個(gè)對(duì)象,否則為不同對(duì)象。
?、诳蛻襞判颍航⒁粋€(gè)第三方類并實(shí)現(xiàn)java.util.Comparator接口。并重寫方法。定義集合形式為TreeSet ts = new TreeSet(new 第三方類());
下面一個(gè)例子用TreeSet存放自然排序的對(duì)象:
package cn.set;
import java.util.Set;
import java.util.TreeSet;
class Student1 implements Comparable<Student1>{
int id;
public Student1(int id) {
this.id = id;
}
@Override
public String toString() {
return this.id+"";
}
@Override
public int hashCode() {
return this.id;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Student1){
Student1 stu = (Student1) obj;
if (stu.id == this.id)
return true;
}
return false;
}
public int compareTo(Student1 o) {
return (this.id-o.id);
}
}
public class TreeSetTest {
public static void main(String[] args) {
Set<Student1> set = new TreeSet<Student1>();
Student1 s1 = new Student1(5);
Student1 s2 = new Student1(1);
Student1 s3 = new Student1(2);
Student1 s4 = new Student1(4);
Student1 s5 = new Student1(3);
set.add(s1);
set.add(s2);
set.add(s3);
set.add(s4);
set.add(s5);
for (Student1 s : set) {
System.out.println(s);
}
}
}
輸出結(jié)果為:
下面一個(gè)例子用TreeSet存放客戶排序的對(duì)象:
package com.set;
import java.util.Set;
import java.util.TreeSet;
class Student1 implements Comparable<Student1>{
int id;
public Student1(int id) {
this.id = id;
}
@Override
public String toString() {
return this.id+"";
}
@Override
public int hashCode() {
return this.id;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Student1){
Student1 stu = (Student1) obj;
if (stu.id == this.id)
return true;
}
return false;
}
public int compareTo(Student1 o) {
return (this.id-o.id);
}
}
public class TreeSetTest {
public static void main(String[] args) {
Set<Student1> set = new TreeSet<Student1>();
Student1 s1 = new Student1(5);
Student1 s2 = new Student1(1);
Student1 s3 = new Student1(2);
Student1 s4 = new Student1(4);
Student1 s5 = new Student1(3);
set.add(s1);
set.add(s2);
set.add(s3);
set.add(s4);
set.add(s5);
for (Student1 s : set) {
System.out.println(s);
}
}
}
輸出結(jié)果為:
大家都知道List存放時(shí)按照插入順序排序的,其實(shí)也可以用自然排序和客戶排序?qū)ist集合排序,大家請(qǐng)看:
package cn.set;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
class MySort1 implements java.util.Comparator<Student3>{
public int compare(Student3 o1, Student3 o2) {
return o2.id-o1.id;
}
}
class Student3 implements Comparable<Student3>{
int id;
public Student3(int id) {
this.id = id;
}
@Override
public String toString() {
return this.id+"";
}
public int compareTo(Student3 o) {
return (this.id-o.id);
}
}
public class ListSort {
public static void main(String[] args) {
List<Student3> list = new ArrayList<Student3>();
Student3 s1 = new Student3(5);
Student3 s2 = new Student3(1);
Student3 s3 = new Student3(2);
Student3 s4 = new Student3(4);
Student3 s5 = new Student3(3);
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
list.add(s5);
System.out.println(list);
//自然排序:
Collections.sort(list);
System.out.println(list);
//客戶排序
Collections.sort(list, new MySort1());
System.out.println(list);
}
}
輸出結(jié)果為:
[5, 1, 2, 4, 3]
[1, 2, 3, 4, 5]
[5, 4, 3, 2, 1]
下面為大家介紹Java中的Set集合接口實(shí)現(xiàn)插入對(duì)象不重復(fù)的原理:
在java的集合中,判斷兩個(gè)對(duì)象是否相等的規(guī)則是:
1)、判斷兩個(gè)對(duì)象的hashCode是否相等
如果不相等,認(rèn)為兩個(gè)對(duì)象也不相等,完畢
如果相等,轉(zhuǎn)入2)
(這一點(diǎn)只是為了提高存儲(chǔ)效率而要求的,其實(shí)理論上沒有也可以,但如果沒有,實(shí)際使用時(shí)效率會(huì)大大降低,所以我們這里將其做為必需的。后面會(huì)重點(diǎn)講到這個(gè)問題。)
2)、判斷兩個(gè)對(duì)象用equals運(yùn)算是否相等
如果不相等,認(rèn)為兩個(gè)對(duì)象也不相等
如果相等,認(rèn)為兩個(gè)對(duì)象相等(equals()是判斷兩個(gè)對(duì)象是否相等的關(guān)鍵)
對(duì)于一般類的對(duì)象(除String等封裝類型對(duì)象外):
若普通類沒有重寫hashcode()和equals()方法,,那么其對(duì)象在比較時(shí),是繼承的object類中的hashcode()方法,object類中的hashcode()方法是一個(gè)本地方法,對(duì)該方法的返回值進(jìn)行比較時(shí),比較的是對(duì)象的地址(引用地址),使用new方法創(chuàng)建內(nèi)容相同的對(duì)象,兩次生成的當(dāng)然是不同的對(duì)象。除非重寫hashcode()方法。在object類中定義的equals()方法也是對(duì)對(duì)象地址的比較。一句話總結(jié):若不重寫普通類的hashcode()和equals()方法,在Set集合中對(duì)象引用地址不一樣,對(duì)象即不重復(fù)。
對(duì)于String等對(duì)象(String、Integer、Double····等等):
由于這些封裝類本身已經(jīng)重寫了hashcode()方法,并且重寫的方法的返回值跟對(duì)象的內(nèi)容相關(guān),而不是跟引用地址相關(guān)。這些封裝類中的equals()方法同樣進(jìn)行了重寫,比較的是對(duì)象的內(nèi)容,而非引用地址。一句話總結(jié):String等類的對(duì)象在集合中均比較他們的內(nèi)容,內(nèi)容相同則覆蓋已存在的對(duì)象。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
相關(guān)文章
SpringBoot內(nèi)存數(shù)據(jù)導(dǎo)出成Excel的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于SpringBoot內(nèi)存數(shù)據(jù)導(dǎo)出成Excel的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
JAVA正則表達(dá)式校驗(yàn)qq號(hào)碼的方法
Java作為一種開發(fā)語(yǔ)言,有許多值得推薦的地方,但是它一直以來沒有自帶對(duì)正則表達(dá)式的支持。下面小編給大家?guī)砹薐AVA正則表達(dá)式校驗(yàn)qq號(hào)碼的方法,需要的朋友參考下吧2018-04-04
RabbitMQ的Direct Exchange模式實(shí)現(xiàn)的消息發(fā)布案例(示例代碼)
本文介紹了RabbitMQ的DirectExchange模式下的消息發(fā)布和消費(fèi)的實(shí)現(xiàn),詳細(xì)說明了如何在DirectExchange模式中進(jìn)行消息的發(fā)送和接收,以及消息處理的基本方法,感興趣的朋友跟隨小編一起看看吧2024-09-09
解析SpringBoot整合SpringDataRedis的過程
這篇文章主要介紹了SpringBoot整合SpringDataRedis的過程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06

