單列集合--ArryList、LinkedList、Set

在這里插入圖片描述


使用IDEA進入某個類之后,按ctrl+F12,或者alt+數字7,可查看該實現類的大綱。


在這里插入圖片描述


在這里插入圖片描述


在這里插入圖片描述


在這里插入圖片描述

package exercise;import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.function.Consumer;public class Demo3 {public static void main(String[] args) {Set<String> s = new HashSet<>();//添加元素//如果當前元素是第一次添加,那么可以添加成功,返回true//如果當前元素是第二次添加,那么添加失敗,返回falseboolean s1 = s.add("sundhine");boolean s2 = s.add("sundhine");System.out.println(s1 + " " + s2);s.add("jiuselu");s.add("lulushui");//存、取順序不一致System.out.println(s);System.out.println("----------------------------");//迭代器遍歷Iterator<String> it = s.iterator();while (it.hasNext()) {String next = it.next();System.out.println(next);}System.out.println("----------------------------");//增強for遍歷for (String string : s) {System.out.println(string);}System.out.println("----------------------------");//Lambdas.forEach(string -> System.out.println(string));}
}

在這里插入圖片描述


在這里插入圖片描述


在這里插入圖片描述

package exercise;import java.util.Objects;public class Demo4 {public static void main(String[] args) {//1.創建對象Student s1 = new Student("sunshien", 23);Student s2 = new Student("sunshien", 23);//2.如果沒有重寫hashCode方法,不同對象計算出的哈希值是不同的//如果已經重寫hashCode方法,不同的對象只要屬性值相同,計算出的哈希值就是一樣的System.out.println(s1.hashCode());System.out.println(s2.hashCode());//在小部分情況下,不同的屬性或者不同的地址值計算出來的哈希值也有可能一樣}
}class Student {private String name;private int age;public Student() {}public Student(String name, int age) {this.name = name;this.age = age;}/*** 獲取** @return name*/public String getName() {return name;}/*** 設置** @param name*/public void setName(String name) {this.name = name;}/*** 獲取** @return age*/public int getAge() {return age;}/*** 設置** @param age*/public void setAge(int age) {this.age = age;}@Overridepublic boolean equals(Object o) {if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;Student student = (Student) o;return age == student.age && Objects.equals(name, student.name);}@Overridepublic int hashCode() {return Objects.hash(name, age);}public String toString() {return "Student{name = " + name + ", age = " + age + "}";}
}

在這里插入圖片描述


1.加載因子是hashSet的擴容時機,當數組中存了 16*0.75 = 12后(本題為例),原數組就會擴充為原先的兩倍。

在這里插入圖片描述

在這里插入圖片描述


在這里插入圖片描述

package exercise;import java.util.HashSet;
import java.util.Objects;public class Demo5 {public static void main(String[] args) {Student s1 = new Student("sunshine", 23);Student s2 = new Student("sunshine", 23);Student s3 = new Student("jiuselu", 24);Student s4 = new Student("lulushui", 23);HashSet<Student> h = new HashSet<>();h.add(s1);h.add(s2);h.add(s3);h.add(s4);for (Student student : h) {System.out.println(student);}}
}class Student {private String name;private int age;public Student() {}public Student(String name, int age) {this.name = name;this.age = age;}/*** 獲取** @return name*/public String getName() {return name;}/*** 設置** @param name*/public void setName(String name) {this.name = name;}/*** 獲取** @return age*/public int getAge() {return age;}/*** 設置** @param age*/public void setAge(int age) {this.age = age;}@Overridepublic boolean equals(Object o) {if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;Student student = (Student) o;return age == student.age && Objects.equals(name, student.name);}@Overridepublic int hashCode() {return Objects.hash(name, age);}public String toString() {return "Student{name = " + name + ", age = " + age + "}";}
}

在這里插入圖片描述
ctrl+shift+上\下箭頭可實現某行代碼上下移動。

package exercise;import java.util.LinkedHashSet;public class Demo6 {public static void main(String[] args) {Student s1 = new Student("sunshine", 23);Student s2 = new Student("sunshine", 23);Student s3 = new Student("jiuselu", 24);Student s4 = new Student("lulushui", 23);LinkedHashSet<Student> l = new LinkedHashSet<>();l.add(s1);l.add(s2);l.add(s3);l.add(s4);for (Student student : l) {System.out.println(student);}}
}

在這里插入圖片描述


在這里插入圖片描述


在這里插入圖片描述

package exercise;import java.util.TreeSet;public class Demo7 {public static void main(String[] args) {TreeSet<Integer> t = new TreeSet<>();t.add(1);t.add(8);t.add(7);t.add(4);//默認從小到大排序System.out.println(t);}
}

在這里插入圖片描述


在這里插入圖片描述

package exercise;import java.util.Objects;
import java.util.TreeSet;public class Demo7 {public static void main(String[] args) {TreeSet<Student> t = new TreeSet<>();Student s1 = new Student("sunshine", 23);Student s3 = new Student("jiuselu", 24);Student s4 = new Student("lulushui", 25);t.add(s1);t.add(s3);t.add(s4);for (Student student : t) {System.out.println(student);}//hashcode和equals方法:哈希表有關的,所以student不用重寫。//Treeset:底層是黑樹}
}
//泛型要寫明類型
class Student implements Comparable<Student> {private String name;private int age;public Student() {}public Student(String name, int age) {this.name = name;this.age = age;}/*** 獲取** @return name*/public String getName() {return name;}/*** 設置** @param name*/public void setName(String name) {this.name = name;}/*** 獲取** @return age*/public int getAge() {return age;}/*** 設置** @param age*/public void setAge(int age) {this.age = age;}public String toString() {return "Student{name = " + name + ", age = " + age + "}";}@Overridepublic int compareTo(Student o) {//指定排序的規則//只看年齡,按照年齡升序排列return this.getAge() - o.getAge();/*this:表示當前要添加的元素o:表示已經在紅黑樹存在的元素返回值:負數:認為要添加的元素是小的,存左邊正數:認為要添加的元素是大的,存右邊認為要添加的元素已經存在,舍棄*/}
}

上述練習實現了第一種排序方式
在這里插入圖片描述


在這里插入圖片描述

package exercise;import java.util.Comparator;
import java.util.TreeSet;public class Demo8 {public static void main(String[] args) {TreeSet<String> ts = new TreeSet<>(new Comparator<String>() {@Override//o1:表示當前要添加的元素//o2:表示已經在紅黑樹存在的元素//返回值規則跟之前是一樣的public int compare(String o1, String o2) {//按長度排序int i = o1.length() - o2.length();i = i == 0 ? o1.compareTo(o2) : i;return i;}});ts.add("c");ts.add("ab");ts.add("df");ts.add("qwer");System.out.println(ts);}
}

在這里插入圖片描述

package exercise;import java.util.TreeSet;public class Demo9 {public static void main(String[] args) {Student s1 = new Student("sunshine", 23, 23, 34, 45);Student s2 = new Student("jiuselu", 24, 23, 34, 45);Student s3 = new Student("lulushui", 25, 23, 34, 45);Student s4 = new Student("sunshine1", 23, 23, 34, 45);Student s5 = new Student("sunshine2", 23, 23, 34, 45);TreeSet<Student> ts = new TreeSet<>();ts.add(s1);ts.add(s2);ts.add(s3);ts.add(s4);ts.add(s5);for (Student t : ts) {System.out.println(t);}}
}class Student implements Comparable<Student> {private String name;private int age;private int chinese;private int math;private int english;public Student() {}public Student(String name, int age, int chinese, int math, int english) {this.name = name;this.age = age;this.chinese = chinese;this.math = math;this.english = english;}/*** 獲取** @return name*/public String getName() {return name;}/*** 設置** @param name*/public void setName(String name) {this.name = name;}/*** 獲取** @return age*/public int getAge() {return age;}/*** 設置** @param age*/public void setAge(int age) {this.age = age;}/*** 獲取** @return chinese*/public int getChinese() {return chinese;}/*** 設置** @param chinese*/public void setChinese(int chinese) {this.chinese = chinese;}/*** 獲取** @return math*/public int getMath() {return math;}/*** 設置** @param math*/public void setMath(int math) {this.math = math;}/*** 獲取** @return english*/public int getEnglish() {return english;}/*** 設置** @param english*/public void setEnglish(int english) {this.english = english;}public String toString() {return "Student{name = " + name + ", age = " + age + ", chinese = " + chinese + ", math = " + math + ", english = " + english + "sum =" + this.getChinese() + this.getEnglish() + this.getMath()+"}";}//方式一:@Overridepublic int compareTo(Student o) {int sum1 = this.getChinese() + this.getEnglish() + this.getMath();System.out.println(sum1);int sum2 = o.getChinese() + o.getEnglish() + o.getMath();int i = sum1 - sum2;i = i == 0 ? this.chinese - o.chinese : i;i = i == 0 ? this.math - o.math : i;i = i == 0 ? this.english - o.english : i;i = i == 0 ? this.getAge() - o.getAge() : i;i = i == 0 ? this.getName().compareTo(o.getName()) : i;return i;}
}

在這里插入圖片描述


在這里插入圖片描述


Java中toString()方法的作用:它通常只是為了方便輸出,比如System.out.println(xx),括號里面的“xx”如果不是String類型的話,就自動調用xx的toString()方法。


本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/22902.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/22902.shtml
英文地址,請注明出處:http://en.pswp.cn/web/22902.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

開放式耳機哪個牌子好?2024年度熱門機型推薦榜單分享!

隨著音樂技術的不斷革新&#xff0c;開放式耳機已成為音樂發燒友們的首選。從最初的簡單音質&#xff0c;到如今的高清解析&#xff0c;開放式耳機不斷進化。音質純凈&#xff0c;佩戴舒適&#xff0c;無論是街頭漫步還是家中細細靜聽&#xff0c;都能帶給你身臨其境的音樂體驗…

iOS18 新變化提前了解,除了AI還有這些變化

iOS 18即將在不久的將來與廣大iPhone用戶見面&#xff0c;這次更新被普遍認為是蘋果歷史上最重要的軟件更新之一。據多方報道和泄露的消息&#xff0c;iOS 18將帶來一系列全新的功能和改進&#xff0c;包括在人工智能領域的重大突破、全新的設計元素以及增強的性能和安全性。現…

make 中 DESTDIR 和 --prefix 的區別

1.configure + make./configure --prefix=/usr make -j8make install DESTDIR=/home2.meson + ninja meson build --prefix=/usr DESTDIR=/home ninja install 這兩個示例分別展示了如何使用兩種流行的構建系統(configure + make 和 meson + ninja)來編譯和安裝軟件,并…

AI教我變得厲害的思維模式01 - 成長型思維模式

今天和AI一起思考如何培養自己的成長性思維。 一一核對&#xff0c;自己哪里里做到&#xff0c;哪里沒有做到&#xff0c;讓AI來微調訓練我自己。 成長性思維的介紹 成長性思維&#xff08;Growth Mindset&#xff09;是由斯坦福大學心理學教授卡羅爾德韋克&#xff08;Carol…

鋇錸技術BL103助力實現PLC到OPC-UA無縫轉換新高度

在工業4.0的大背景下&#xff0c;信息物理系統和工業物聯網的融合日益加深&#xff0c;推動了工業自動化向更高層次的發展。OPC UA作為一種開放、安全、跨平臺的通信協議&#xff0c;在實現不同設備、系統間數據交換和互操作性方面扮演了核心角色。鋇錸技術公司推出的BL103 PLC…

調用訊飛星火API實現圖像生成

目錄 1. 作者介紹2. 關于理論方面的知識介紹3. 關于實驗過程的介紹&#xff0c;完整實驗代碼&#xff0c;測試結果3.1 API獲取3.2 代碼解析與運行結果3.2.1 完整代碼3.2.2 運行結果 3.3 界面的編寫&#xff08;進階&#xff09; 4. 問題分析5. 參考鏈接 1. 作者介紹 劉來順&am…

Vitis HLS 學習筆記--通道的FIFO/PIPO選擇

目錄 1. 簡介 2. 代碼詳解 2.1 FIFO 通道示例 2.1.1 配置默認通道 2.1.2 kernel 代碼 2.1.3 綜合報告 2.1.4 depth 32 解析 2.1.5 FIFO 通道分類 2.2 PIPO 2.2.1 配置默認通道 2.2.2 kernel 代碼 2.2.3 綜合報告 2.2.4 PIPO 通道分類 3. 綜合對比 3.1 數據類…

docker安裝及常見命令

歷史版本docker下載 https://docs.docker.com/desktop/release-notes/#upgrades-17 docker start 容器id # 啟動容器 docker restart 容器id # 重啟容器 docker stop 容器id # 停止當前運行的容器 docker kill 容器id # 強制停止當前容器…

2024年帶你揭秘FL Studio 21破解版,2024年最新FL21內置漢化破解補丁

截止目前&#xff0c;FL Studio最新版是FL Studio 21.2.3.4004版本&#xff0c;想必很多朋友已經迫不及待了&#xff0c;那么今天這篇文章我將帶大家詳細的介紹FL Studio 21.2.3 Build 4004新特點以及如何下載&#xff0c;安裝和激活。 PS.本次為你帶來的是fl studio21破解版&a…

針對多標簽(Multi-label)任務的經典算法

前言 如果你對這篇文章感興趣&#xff0c;可以點擊「【訪客必讀 - 指引頁】一文囊括主頁內所有高質量博客」&#xff0c;查看完整博客分類與對應鏈接。 多標簽&#xff08;Multi-label&#xff09;任務是分類任務的擴展版&#xff0c;即每個樣本不再僅屬于一個類別&#xff0…

MySQL是怎么保證原子性的(undo log日志相關)

MySQL是怎么保證原子性的&#xff1f; 事務的原子性就是&#xff1a;一個事物要么全部執行成功&#xff0c;要么全部執行失敗。MySQL 主要是利用 undo log&#xff0c;也就是回滾日志來實現原子性。 平常我們在對數據進行增刪改時&#xff0c;InnoDB 除了會記錄 redo log&…

【python】Modulenotfounderror: no module named ‘open_clip’

成功解決“ModuleNotFoundError: No module named ‘open_clip’”錯誤的全面指南 在Python編程中&#xff0c;如果你遇到了“ModuleNotFoundError: No module named ‘open_clip’”這個錯誤&#xff0c;它意味著你的Python環境中沒有安裝名為open_clip的模塊&#xff0c;或者…

grep、sed、awk

grep&#xff1a;文本過濾工具 sed: 文本編輯工具 awk: 格式化文本 grep -n 顯示行號 -i 忽略大小寫 -v 取反 -o 只保留關鍵消息 # 找出文件的空行 grep ^$ test.txt -n # 找出文件非空行內容 grep ^$ test.txt -n -v # 找出文件非空行內容&#xff0c;并且排除注釋&#xff…

8個免費下載音樂的網站,建議收藏!

1、My Free MP3 tools.liumingye.cn/music/ 一個好用且免費的在線音樂播放和下載網站&#xff0c;幾乎收錄了所有國內外大火的歌手和歌曲&#xff0c;可以通過歌手列表找單曲&#xff0c;也可以直接搜索歌手或歌曲名&#xff0c;下面還有一些熱門搜索&#xff0c;可以直接播放…

頻率域,空間域以及頻率域和空間域如何獲取

文章目錄 頻率域頻率域的關鍵概念&#xff1a;頻率域的應用&#xff1a; 空間域空間域特征的含義&#xff1a;空間域操作的常見技術&#xff1a;與頻率域的對比&#xff1a; 如何獲取空間域&#xff0c;頻率域空間域特征&#xff1a;頻率域特征&#xff1a; 頻率域 頻率域&…

每天學習一個Windows命令或Linux命令——seq

今天我們來學習 seq命令&#xff01; seq命令&#xff08;單詞sequence序列的縮寫&#xff09;是Linux系統中用于輸出序列化的一串整數的命令。 一、seq用法 seq用法一共有以下三種&#xff1a; seq [選項]... 尾數 seq [選項]... 首數 尾數 seq [選項]... 首數 增量&#…

數字證書-證書分類

SSL證書類型 數字證書DV證書、OV證書和EV證書三種類型的SSL證書。不同類型證書的安全性、支持的證書品牌和適用的網站類型不同&#xff0c;具體如下表所示 證書類型適用網站類型等級認證強度支持的證書品牌DV&#xff08;域名型&#xff09;個人網站一般CA機構審核個人網站真…

Windows下SVN文件損壞,啟動服務報錯1067

之前碰到過一次&#xff0c;忘記最后怎么解決的了&#xff0c;只記得大概原理和原因&#xff0c;以及解決辦法。 1067錯誤碼&#xff0c;很多地方都會碰到&#xff0c;mysql也會有&#xff0c;看來應該是windows系統的錯誤碼。跟具體程序無關。所以直接百度“SVN”、“1067”…

HarmonyOS App開發造輪子--自定義圓形圖片

思路&#xff1a; 1、對比之前自己在其他程序開發中自定義組件的思路&#xff0c;首先尋找父組件Image和Component相關的Api&#xff0c;看看是否具備OnDraw方法。 2、了解Canvas相關Api操作&#xff0c;特別是涉及到位圖的操作。 通過翻閱大量資料&#xff0c;發現了兩個關…

不是,有了這套IP地址管理開源系統誰還用Excel啊

號主&#xff1a;老楊丨11年資深網絡工程師&#xff0c;更多網工提升干貨&#xff0c;請關注公眾號&#xff1a;網絡工程師俱樂部 中午好&#xff0c;我的網工朋友。 作為網工的我們想必都很清楚IP地址管理的重要性以及其復雜性&#xff0c;傳統的Excel表格雖然在某些情況下能…