用帝國(guó)做的網(wǎng)站只收錄首頁(yè)百度一下百度搜索
集合的使用
前提:棧、堆、二叉樹(shù)、hashcode、toString()、quesalus()的知識(shí)深入和底層理解。
1、什么是集合
集合就是咋們所說(shuō)的容器 ? 前面我們學(xué)習(xí)過(guò)數(shù)組 數(shù)組也是容器 ? 容器:裝東西的 生活中有多少的容器呀? 水杯 教室 酒瓶 水庫(kù) 只要是能裝東西的 都可以看成是容器 ? 我們這個(gè)集合的容器 是用來(lái)裝啥的呢? 裝數(shù)據(jù)? ? 數(shù)據(jù)? 一切可以被計(jì)算機(jī)識(shí)別的 文字 圖片 視頻 音頻都是數(shù)據(jù) ? 說(shuō)白了 我們今天所學(xué)習(xí)的這個(gè)集合 就跟前面的數(shù)組類似 只是底層的實(shí)現(xiàn)不一樣而已
2、集合的分類
主要根據(jù)我們的值的個(gè)數(shù) 可以分成 單例集合 和 雙列集合 ? 單例集合:簡(jiǎn)單的說(shuō) 在這個(gè)容器中存放的的值 就是一個(gè)一個(gè)的 value單列集合的爹:Collection ? 雙列集合:他在容器中存儲(chǔ)的數(shù)據(jù)就是 鍵值對(duì) key---value雙列集合的爹:Map
3、單列集合
3.1、List集合
3.1.1、ArrayList
ArrayList<E> ? 這個(gè)集合中的泛型:表示的意思其實(shí)就是對(duì)這個(gè)容器中能存放數(shù)據(jù)類型的一種約束 比如我們的泛型的數(shù)據(jù)類型是:User 那么這個(gè)容器中只能存放User類型的數(shù)據(jù) ? 這個(gè)ArrayList底層就是數(shù)組 他是有序的 地址空間是連續(xù)的 地址空間連續(xù) 就意味著能通過(guò)開(kāi)始地址+偏移量來(lái)為訪問(wèn) 而且能重復(fù)添加數(shù)據(jù) ? 有序:一定能通過(guò)下標(biāo)訪問(wèn)數(shù)據(jù) ? List中所有的內(nèi)容都是可以重復(fù)的....
3.1.1.1、集合的使用
public class ArrayListTest { ?public static void main(String[] args) {//第一種方式List<String> list = new ArrayList<>();//接下來(lái)就可以向這個(gè)集合中存放數(shù)據(jù)了...list.add("123");list.add("456");list.add("789");list.add("789"); ?List<String> list2 = new ArrayList<>();//接下來(lái)就可以向這個(gè)集合中存放數(shù)據(jù)了...list.add("12322");list.add("45622"); ? ?//將list2集合中的數(shù)據(jù) 添加到 list中來(lái)list.addAll(list2); ? ?//能不能刪除數(shù)據(jù)呢?//這個(gè)就是直接刪除某一個(gè)元素list.remove("789"); ?//還可以通過(guò)下標(biāo)刪除list.remove(0); ? ?//接下來(lái)玩下修改呢?list.set(0,"小波波"); ?//獲取某一個(gè)位置的元素String s = list.get(0); ?//判斷這個(gè)集合中是否存在某一個(gè)元素boolean contains = list.contains("789"); ?//判斷集合是否為空boolean empty = list.isEmpty(); ?//返貨某一個(gè)元素的下標(biāo)位置int i = list.indexOf("789"); ?//截取集合中指定位置的元素 生成一個(gè)新的集合List<String> stringList = list.subList(0, 5); ?System.out.println("list集合的size:"+list.size()); ?List<User> userList=new ArrayList<>();userList.add(new User(1,"小小","123"));userList.add(0,new User(0,"這里是測(cè)試","xxx")); ?System.out.println("userList集合的size:"+userList.size());System.out.println("userList中的數(shù)據(jù)是:"+userList.get(0));} }
public class ArrayListTest1 { ?public static void main(String[] args) {List<String> list = new ArrayList<>();//接下來(lái)就可以向這個(gè)集合中存放數(shù)據(jù)了...list.add("123");list.add("456");list.add("789");list.add("789"); ?//第一種遍歷方式:因?yàn)檫@個(gè)ArrayList本身底層是數(shù)組(Object類型的數(shù)組) 數(shù)組地址空間連續(xù) 所以我們能通過(guò)下標(biāo)來(lái)訪問(wèn)for (int i = 0; i <list.size() ; i++) {System.out.println("集合中的值:"+list.get(i));}System.out.println("------------------------------"); ? ?//第二種遍歷方式 通過(guò)增強(qiáng)的for循環(huán)來(lái)玩for (String val:list){System.out.println("集合中的值:"+val);} ? ?System.out.println("------------------------------"); ? ?//第三種遍歷方式通過(guò)JDK8中的stream流來(lái)遍歷list.stream().forEach(val->{System.out.println("集合中的值:"+val);}); ?System.out.println("------------------------------"); ?//第四種遍歷方式:迭代器 迭代器的游標(biāo)問(wèn)題/*** 這種情況下不允許對(duì)元素進(jìn)行修改和刪除** 其實(shí)不止是這種情況 在遍歷的情況下 邏輯上都允許修改和刪除的產(chǎn)生*/Iterator<String> it = list.iterator();// it.hasNext():判斷下一個(gè)節(jié)點(diǎn)是否有元素while (it.hasNext()){// it.next() :取出當(dāng)前位置的元素String val = it.next();System.out.println("通過(guò)迭代器取出來(lái)的值:"+val);} ?System.out.println("------------------------------"); ?//第五種遍歷方式ListIterator<String> it1 = list.listIterator();while (it1.hasNext()){String next = it1.next();System.out.println("通過(guò)迭代器取出來(lái)的值:"+next);}} }
3.1.2、LinkedList
LinkedList底層是鏈表 ? 鏈表中包含 一個(gè)一個(gè)的鏈條 ? 每一個(gè)鏈條都包含了兩部分 ? 當(dāng)前節(jié)點(diǎn)的值 和 下一個(gè)元素的地址 ? 鏈表中 數(shù)據(jù)存儲(chǔ)的地址空間不連續(xù) 所以不能使用偏移量來(lái)訪問(wèn)
輸出的值是連續(xù)的。
public class LinkedListTest01 { ?public static void main(String[] args) {//申明對(duì)象List<String> linkedList1 = new LinkedList<>();linkedList1.add("001");linkedList1.add("002");linkedList1.add("003");linkedList1.add("004"); ?for (int i = 0; i < linkedList1.size(); i++) {String val = linkedList1.get(i);System.out.println("val:"+val);}for (String val:linkedList1){System.out.println("val:"+val);} ?linkedList1.stream().forEach(val->{System.out.println("val:"+val);});} } ?
3.1.3、Vector的使用(不常用)
這個(gè)底層也是數(shù)組 線程安全的 效率不高 ? 這個(gè)集合基本不使用 不也用記住
public class VectorTest {public static void main(String[] args){List<String> vector=new Vector<>();vector.add("中國(guó)好");vector.add("小日子");for (int i = 0; i <vector.size() ; i++) {System.out.println("數(shù)據(jù)是:"+vector.get(i));} ?} }
3.1.4、Stack(棧)
public class StackTest { ?public static void main(String[] args){//這個(gè)其實(shí)是棧的數(shù)據(jù)結(jié)構(gòu) ?Stack<String> list=new Stack<String>();list.push("1");list.push("2");list.push("3");list.push("4");list.push("5"); ?System.out.println("pop:"+list.pop());System.out.println("pop:"+list.pop());System.out.println("pop:"+list.pop());System.out.println("pop:"+list.pop());System.out.println("pop:"+list.pop()); ?} }
3.2、Set集合
邏輯上 Set集合邏輯上是無(wú)序的 而且Set集合能排重 不能通過(guò)下標(biāo)直接訪問(wèn)Set<E> 的爹 依然是Collection Set這個(gè)接口是所有Set集合的爹Set排重的原則是啥?如果在Set集合中存放的是對(duì)象比如User 那么他就首先會(huì)去調(diào)用這個(gè)User中的 hashCode方法 然后獲取到當(dāng)前的這個(gè)要添加數(shù)據(jù)的hashCode值去和已經(jīng)添加數(shù)據(jù)的HashCode值 做比較 如果是不等 那么說(shuō)明肯定不是一個(gè)元素 那么直接添加元素 如果是HashCode值 遇到了在已經(jīng)添加的數(shù)據(jù)中的HashCode值是相等的話 那么都說(shuō)明有可能這個(gè)值是重復(fù)的 如果是這個(gè)值是重復(fù)的話 那就去調(diào)用當(dāng)前這個(gè)對(duì)象的equals方法 判斷equals方法是不是返回true 如果返回true 那么說(shuō)明值重復(fù) 不添加數(shù)據(jù) 如果返回的是false 那么說(shuō)明值 不重復(fù) 那么就可以添加數(shù)據(jù)
3.2.1、Set集合的排重問(wèn)題(HashSet)
public static void main(String[] args) {Set<String> set = new HashSet<>();set.add("123");set.add("345");set.add("234");set.add("345");System.out.println("數(shù)據(jù)的個(gè)數(shù):" + set.size());System.out.println("-----------------------------");Set<User> setUser = new HashSet<>();setUser.add(new User(1, "小小", "112"));setUser.add(new User(1, "小小", "134"));setUser.add(new User(1, "小小", "165"));setUser.add(new User(1, "小小", "178"));System.out.println("數(shù)據(jù)的個(gè)數(shù):" + setUser.size());}
public class User {private Integer id;private String username;private String password; ?public User(Integer id, String username, String password) {this.id = id;this.username = username;this.password = password;} ?public User() {} ?public Integer getId() {return id;} ?public void setId(Integer id) {this.id = id;} ?public String getUsername() {return username;} ?public void setUsername(String username) {this.username = username;} ?public String getPassword() {return password;}public void setPassword(String password) {this.password = password;} ?@Overridepublic int hashCode() {return this.username.hashCode();} ?/*** 用戶名一樣 那么我們就認(rèn)為這是同一個(gè)數(shù)據(jù)* @param obj* @return*/@Overridepublic boolean equals(Object obj) {if(obj instanceof User){User user= (User) obj;return this.username.equals(user.getUsername());}return false;} }
3.2.2、Set集合的遍歷問(wèn)題(HashSet)
public class HashSetTest1 { ?public static void main(String[] args) {Set<User> set = new HashSet<>();set.add(new User(1,"xiaobobo","123"));set.add(new User(-1,"tiedan","777"));set.add(new User(3,"gousheng","0"));set.add(new User(4,"gouwa","234"));set.add(new User(1,"ergouzi","234")); ?//通過(guò)增強(qiáng)的for循環(huán)能訪問(wèn)for (User val:set){System.out.println("數(shù)據(jù):"+val);} ?System.out.println("--------------------------"); ?Iterator<User> it = set.iterator();while (it.hasNext()){User next = it.next();System.out.println("拿到的數(shù)據(jù)是:"+next);} ?System.out.println("--------------");set.stream().forEach(user -> {System.out.println("讀取到的數(shù)據(jù)是:"+user);});} } ?
3.2.3、LinkedHashSet的使用
這個(gè)集合的底層是鏈表 ? 這個(gè)集合的數(shù)據(jù)保存是有序的
public class LinkedHashSetTest {public static void main(String[] args) {Set<User> set = new LinkedHashSet<>();set.add(new User(1, "xiaobobo", "123"));set.add(new User(-1, "tiedan", "777"));set.add(new User(3, "gousheng", "0"));set.add(new User(4, "gouwa", "234"));set.add(new User(1, "gouwa", "234"));Iterator<User> it = set.iterator();while (it.hasNext()) {User next = it.next();System.out.println(next);}} }
3.2.4、TreeSet(底層實(shí)現(xiàn)是紅黑樹(shù))
TreeSet和其他的Set集合一樣 具有 排重的特性 ? TreeSet的底層是紅黑樹(shù)--->二叉樹(shù)--->數(shù)據(jù)結(jié)構(gòu)--->有大小關(guān)系 ? TreeSet集合自動(dòng)具有排序的功能 ? 這個(gè)排序 就涉及到一個(gè)大小的問(wèn)題 ? 自然數(shù)的大小 ? ? 字符串如何比較大小呢? unicode編碼值
3.2.5、兩個(gè)字符串如何比較大小呢?通過(guò)編碼
// 在String這個(gè)類中為我們提供了這個(gè)比較兩個(gè)字符串大小的方法 public static void main(String[] args) {String str = "Ab";String str2 = "Ab";/*** 返回值 0:表示的是前后相等* 返回值-1:表示的是前面小于后面* 返回值是1:表示的是前面大于后面*/System.out.println(str2.compareTo(str)); }
3.2.6、TreeSet的使用
TreeSet的底層使用的是紅黑樹(shù)來(lái)實(shí)現(xiàn)的
3.2.6.1、TreeSet的基本使用
package com.qfedu.edu.collection.set; ? import java.util.Iterator; import java.util.Set; import java.util.TreeSet; ? ? public class TreeSetTest { ?public static void main(String[] args) {Set<Integer> set = new TreeSet<>();set.add(123);set.add(0);set.add(345);set.add(77);set.add(77);set.add(89); ?//遍歷這些數(shù)據(jù)Iterator<Integer> it = set.iterator(); ?while (it.hasNext()) {Integer next = it.next();System.out.println("數(shù)據(jù)是:" + next);} ?//--------------下面研究字符串的排序------------System.out.println("-------------------"); ?Set<String> setStr = new TreeSet<>();setStr.add("Abc");setStr.add("Bc");setStr.add("Ac"); ?//遍歷這些數(shù)據(jù)Iterator<String> it1 = setStr.iterator(); ?while (it1.hasNext()) {String next = it1.next();System.out.println("數(shù)據(jù)是:" + next);} ?} } ?
3.2.6.2、Comparable接口實(shí)現(xiàn)對(duì)象的比較
1、編寫(xiě)Employee對(duì)象
public class Employee implements Comparable<Employee>{ ?private Integer id;private String name;private Integer salary;private String address; ?public Employee(Integer id, String name, Integer salary, String address) {this.id = id;this.name = name;this.salary = salary;this.address = address;} ?public Employee() {} ?public Integer getId() {return id;} ?public void setId(Integer id) {this.id = id;} ?public String getName() {return name;} ?public void setName(String name) {this.name = name;} ?public Integer getSalary() {return salary;} ?public void setSalary(Integer salary) {this.salary = salary;} ?public String getAddress() {return address;} ?public void setAddress(String address) {this.address = address;} ?/*** 比較的方法* ? 你需要按照誰(shuí)排序 那么下面你就按照什么來(lái)比較** ? ? 我要通過(guò)薪資排序* ? ? ? ? 薪資是int類型 那么下面就直接做減法* @param o the object to be compared.* @return*/ // ? @Override // ? public int compareTo(Employee o) { // ? ? ? return this.salary-o.getSalary(); // ? } ?/*** 下面演示通過(guò)姓名來(lái)排序* 姓名是字符串類型* @param o the object to be compared.* @return*//* ? @Overridepublic int compareTo(Employee o) {return this.getName().compareTo(o.getName());}*/ ?/*** 如果薪資不為空 那么按照薪資排序* ? 如果薪資為空 并且姓名不為空 那么按照姓名排序* ? 如果姓名為空 那么就按照id排序....* @param o the object to be compared.* @return*/@Overridepublic int compareTo(Employee o) {if(o.getSalary()!=null){return this.getSalary()-o.getSalary();}else if(o.getName()!=null&&!("".equals(o.getName()))){return this.getName().compareTo(o.getName());}else{return this.id-o.getId();}} ?@Overridepublic String toString() {return "Employee{" +"id=" + id +", name='" + name + '\'' +", salary=" + salary +", address='" + address + '\'' +'}';} } ?
2、編寫(xiě)測(cè)試類
public class TreeSetTest1 { ?public static void main(String[] args) { ?Set<Employee> set = new TreeSet<>();set.add(new Employee(1,"xiaobobo",3500,"四川成都"));set.add(new Employee(2,"xiaowangzi",1800,"四川巴中"));set.add(new Employee(3,"tiedan",2600,"四川自貢"));set.add(new Employee(4,"gousheng",15000,"四川綿陽(yáng)"));set.add(new Employee(5,"gouwa",2700,"四川德陽(yáng)")); ? ?Iterator<Employee> iterator = set.iterator();while (iterator.hasNext()){Employee next = iterator.next();System.out.println("獲取到的數(shù)據(jù)是:"+next);}} } ?
3.2.6.3、使用Comparator來(lái)實(shí)現(xiàn)對(duì)象的排序
1、對(duì)象的編寫(xiě)
public class Employee1{ ?private Integer id;private String name;private Integer salary;private String address; ?public Employee1(Integer id, String name, Integer salary, String address) {this.id = id;this.name = name;this.salary = salary;this.address = address;} ?public Employee1() {} ?public Integer getId() {return id;} ?public void setId(Integer id) {this.id = id;} ?public String getName() {return name;} ?public void setName(String name) {this.name = name;} ?public Integer getSalary() {return salary;} ?public void setSalary(Integer salary) {this.salary = salary;} ?public String getAddress() {return address;} ?public void setAddress(String address) {this.address = address;} ? ?@Overridepublic String toString() {return "Employee{" +"id=" + id +", name='" + name + '\'' +", salary=" + salary +", address='" + address + '\'' +'}';} } ?
2、測(cè)試的編寫(xiě)
package com.qfedu.edu.collection.set; ? import com.qfedu.edu.pojo.Employee; import com.qfedu.edu.pojo.Employee1; ? import java.util.Comparator; import java.util.Iterator; import java.util.Set; import java.util.TreeSet; ? /*** @author xiaobobo* @title: TreeSetTest* @projectName CD-Java-JY-2401-Simple-Parent* @description: 這個(gè)研究下TreeSet的對(duì)象的排序問(wèn)題* @date 2024/3/19 15:26*/ public class TreeSetTest2 { ?public static void main(String[] args) { ?Set<Employee1> set = new TreeSet<>(new MyComparator());set.add(new Employee1(1,"xiaobobo",3500,"四川成都"));set.add(new Employee1(2,"xiaowangzi",1800,"四川巴中"));set.add(new Employee1(3,"tiedan",2600,"四川自貢"));set.add(new Employee1(4,"gousheng",15000,"四川綿陽(yáng)"));set.add(new Employee1(5,"gouwa",2700,"四川德陽(yáng)")); ?Iterator<Employee1> iterator = set.iterator();while (iterator.hasNext()){Employee1 next = iterator.next();System.out.println("獲取到的數(shù)據(jù)是:"+next);} ? ?} ? ?/*** 自定義了一個(gè)比較器 這個(gè)跟上一個(gè)接口是一樣的*/static class MyComparator implements Comparator<Employee1>{/**** @param o1 新數(shù)據(jù)* @param o2 老數(shù)據(jù)* 按照薪資排序 做減法 按照誰(shuí)排序 就用誰(shuí)來(lái)做比較* @return*/@Overridepublic int compare(Employee1 o1, Employee1 o2) {return o1.getSalary()-o2.getSalary();}} }