DAY08 List接口、Collections接口、Set接口

学习目标

能够说出List集合特点1.有序2.允许存储重复的元素3.有带索引的方法(练习 add,remove,set,get)
能够使用集合工具类Collections:static void sort(List<T> list) 根据元素的自然顺序 对指定列表按升序进行排序。static <T> void sort(List<T> list, Comparator<? super T> c) 根据指定比较器产生的顺序对指定列表进行排序。static void shuffle(List<?> list)  随机打乱集合中元素的顺序
能够使用Comparator比较器进行排序Collections.sort(list01, new Comparator<Integer>() {@Overridepublic int compare(Integer o1, Integer o2) {//降序: o2-o1  升序:o1-o2return o2-o1;}});
能够使用可变参数public static void method(int...arr){}调用method方法,参数是一个可变参数,可以接收任意个同种数据类型的数据method(),method(10),method(10,20)...
能够说出Set集合的特点1.不允许存储重复的元素2.不包含带索引的方法
能够说出哈希表的特点JDK1.8之前:数组+单向链表JDK1.8之后:数组+单向链表|数组+红黑树查询的效率高
使用HashSet集合存储自定义元素(重点)想要保证存储的元素(Person对象,Student对象...)同名和同年龄的人视为同一个人自定义类型(Person对象,Student对象...)必须重写hashCode和equals方法

第一章 List接口

1.List接口的概述

/*java.uil.List<E>接口 extends Collection<E>接口List接口的特点:1.是一个有序的集合:存储元素和取出元素的顺序是一致的  存储:123  取出:1232.允许存储重复的元素  add(10)  add(10)3.包含一些带索引的特有方法List接口特有的带索引的方法:void add(int index, E element)  在指定索引处,添加一个新的元素E get(int index)  获取指定索引处的元素E remove(int index)  移除并返回指定索引处的元素,返回的就是被移除的元素E set(int index, E element)  替换并返回指定索引处的元素,返回的是被替换的元素注意:使用带索引的方法,必须防止索引越界异常(不要超过集合索引的使用范围:0-->集合的长度-1)         */

2.List接口中常用的方法(重点)

package com.itheima.demo01List;import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;/*索引越界异常:IndexOutOfBoundsException:索引越界异常,集合会报ArrayIndexOutOfBoundsException:数组索引越界异常StringIndexOutOfBoundsException:字符串的索引越界异常*/
public class Demo01List {public static void main(String[] args) {//创建List集合对象,给集合添加元素List<String> list = new ArrayList<>();list.add("aaa");list.add("bbb");list.add("ccc");list.add("aaa");list.add("ddd");         //  0    1   2    3     4System.out.println(list);//[aaa, bbb, ccc, aaa, ddd]//void add(int index, E element)  在指定索引处,添加一个新的元素//在索引2处添加一个新的元素"哈哈"list.add(2,"哈哈");System.out.println(list);//[aaa, bbb, 哈哈, ccc, aaa, ddd]//E get(int index)  获取指定索引处的元素String s1 = list.get(0);System.out.println("s1:"+s1);//s1:aaaString s2 = list.get(1);System.out.println("s2:"+s2);//s2:bbb//list.get(10);//IndexOutOfBoundsException: Index: 10, Size: 6//E remove(int index)  移除并返回指定索引处的元素,返回的就是被移除的元素System.out.println(list);//[aaa, bbb, 哈哈, ccc, aaa, ddd]//删除集合中第二个aaaString s3 = list.remove(4);System.out.println("s3:"+s3);//s3:aaaSystem.out.println(list);//[aaa, bbb, 哈哈, ccc, ddd]//E set(int index, E element)  替换并返回指定索引处的元素,返回的是被替换的元素//把索引2处哈哈,替换为呵呵String s4 = list.set(2, "呵呵");System.out.println("s4:"+s4);//s4:哈哈System.out.println(list);//[aaa, bbb, 呵呵, ccc, ddd]System.out.println("------------------------------------------");//使用普通for循环遍历List集合  list.forifor (int i = 0; i < list.size(); i++) {String s = list.get(i);System.out.println(s);}System.out.println("------------------------------------------");//使用增强for循环遍历List集合 list.forfor (String s : list) {System.out.println(s);}System.out.println("------------------------------------------");//使用迭代器遍历List集合Iterator<String> it = list.iterator();while (it.hasNext()){String s = it.next();System.out.println(s);}}
}

3.ArrayList集合(数组)

/*java.util.ArrayList<E>集合 implements List<E>接口List 接口的大小可变数组的实现。ArrayLis集合底层采用的就是数组结构:查询快,增删慢工作中:查询多的时候使用,不对集合的长度进行修改(添加,删除)
*/
public class ArrayList<E>{transient Object[] elementData = {};add方法:list.add("aaa");	public boolean add(E e) {ensureCapacityInternal(size + 1);  // Increments modCount!!elementData[size++] = e;return true;}...//add方法底层:创建一个新的数组,长度是源数组长度+1,把源数组中的元素使用System类中的arraycopy方法复制到新的数组中    public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {@SuppressWarnings("unchecked")T[] copy = ((Object)newType == (Object)Object[].class)? (T[]) new Object[newLength]: (T[]) Array.newInstance(newType.getComponentType(), newLength);System.arraycopy(original, 0, copy, 0,Math.min(original.length, newLength));return copy;}   }

4.LinkedList集合(双向链表)

a.LinkedList集合概述

请添加图片描述

/*java.util.LinkedList<E> implements List<E>接口List 接口的链接列表实现。LinkedList集合底层是一个双向链表:查询慢,增删快双向:是一个有序的集合LinkedList集合中含有一些操作链表首尾元素的方法:public void addFirst(E e) :将指定元素插入此列表的开头。public void push(E e) :将元素推入此列表所表示的堆栈。public void addLast(E e) :将指定元素添加到此列表的结尾。public E getFirst() :返回此列表的第一个元素。public E getLast() :返回此列表的最后一个元素。public boolean isEmpty() :如果列表不包含元素,则返回truepublic E removeFirst() :移除并返回此列表的第一个元素。public E pop() :从此列表所表示的堆栈处弹出一个元素。public E removeLast() :移除并返回此列表的最后一个元素。注意:使用LinkedList集合特有的方法,不能使用多态创建对象List<String> list = new LinkedList<>();弊端:不能是实现类特有的方法LinkedList<String> linked = (LinkedList<String>)list; 向下转型Collection<String> list = new LinkedList<>();弊端:不能是实现类特有的方法LinkedList<String> list = new LinkedList<>();
*/

b.LinkedList集合特有的方法(使用)

package com.itheima.demo01List;import java.util.LinkedList;/*LinkedList集合特有的方法*/
public class Demo02LinkedList {public static void main(String[] args) {show04();}/*public E removeFirst() :移除并返回此列表的第一个元素。public E pop() :从此列表所表示的堆栈处弹出一个元素。 此方法等效于 removeFirst()。public E removeLast() :移除并返回此列表的最后一个元素。*/private static void show04() {LinkedList<String> linked = new LinkedList<>();linked.add("aaa");linked.add("bbb");linked.add("ccc");linked.add("ddd");System.out.println(linked);//[aaa, bbb, ccc, ddd]if(linked.size()!=0){//String first = linked.removeFirst();String first = linked.pop();System.out.println("first:"+first);//first:aaaString last = linked.removeLast();System.out.println("last:"+last);//last:dddSystem.out.println(linked);//[bbb, ccc]}}/*public E getFirst() :返回此列表的第一个元素。public E getLast() :返回此列表的最后一个元素。public boolean isEmpty() :如果列表不包含元素,则返回true*/private static void show03() {LinkedList<String> linked = new LinkedList<>();linked.add("aaa");linked.add("bbb");linked.add("ccc");linked.add("ddd");linked.clear();//清空集合//为了防止NoSuchElementException:没有元素异常,增加一个判断,集合不是空的在获取if(!linked.isEmpty()){//return size() == 0;String first = linked.getFirst();System.out.println("first:"+first);//first:aaaString last = linked.getLast();System.out.println("last:"+last);//last:ddd}if(linked.size()!=0){String first = linked.getFirst();System.out.println("first:"+first);//first:aaaString last = linked.getLast();System.out.println("last:"+last);//last:ddd}}private static void show02() {LinkedList<String> linked = new LinkedList<>();linked.addFirst("1");linked.addFirst("2");linked.addFirst("3");linked.addLast("a");linked.addLast("b");linked.addLast("c");System.out.println(linked);//[3, 2, 1, a, b, c]}/*public void addFirst(E e) :将指定元素插入此列表的开头。public void push(E e) :将元素推入此列表所表示的堆栈。此方法等效于 addFirst(E)。public void addLast(E e) :将指定元素添加到此列表的结尾。此方法等效于add()*/private static void show01() {LinkedList<String> linked = new LinkedList<>();linked.add("aaa");linked.add("bbb");linked.add("ccc");linked.add("ddd");System.out.println(linked);//[aaa, bbb, ccc, ddd]//public void addFirst(E e) :将指定元素插入此列表的开头。//linked.addFirst("www");linked.push("www");System.out.println(linked);//[www, aaa, bbb, ccc, ddd]//public void addLast(E e) :将指定元素添加到此列表的结尾。此方法等效于add()//linked.addLast("com");linked.add("com");System.out.println(linked);//[www, aaa, bbb, ccc, ddd, com]}
}

c.LinkedList源码分析(了解即可)

5.Vector集合(面试)

/*java.util.Vector<E> implements List<E>(jdk1.2之后)Vector是JDK1.0时期存在的单列集合,Collection下边的其他集合(ArrayList,LinkedList...)是JDK1.2之后出现的Vector 类可以实现可增长的对象数组。Vector集合底层和ArrayList集合是一样的,也是一个数组结构(查询快,增删慢)Vector集合1.0时期有一些特有的方法:void addElement(E obj)  往集合中添加元素Enumeration<E> elements() 返回此向量的组件的枚举。 Enumeration<E>接口:向量枚举,是1.0时期的迭代器boolean hasMoreElements()  判断集合中还有没有元素==>hasNextE nextElement()  取出集合的元素==>next与新 collection 实现不同,Vector 是同步的。 同步技术:可以保证多线程的安全同步技术:集合存储数据效率低所以Vector集合被效率更高的ArrayList集合取代了
*/

第二章 Collections类

1.Collections的常用功能(重点)

package com.itheima.demo02Collections;import java.util.ArrayList;
import java.util.Collections;/*java.util.Collections:操作集合的工具类,里边的方法都是静态的通过类名.方法名(参数)可以直接使用常用的方法:static void  sort(List<T> list) 根据元素的自然顺序 对指定集合按升序进行排序。static <T> void sort(List<T> list, Comparator<? super T> c)  根据指定比较器产生的顺序对指定列表进行排序。static void shuffle(List<?> list)  随机打乱集合中元素的顺序注意:以上3个方法,参数都是List,只能传递List接口下的实现类对象(ArrayList,LinkedList),不能使用Set接口下的集合*/
public class Demo01Collections {public static void main(String[] args) {//创建ArrayList集合对象ArrayList<Integer> list01 = new ArrayList<>();list01.add(1);list01.add(3);list01.add(2);list01.add(4);System.out.println(list01);//[1, 3, 2, 4]/*static void  sort(List<T> list) 根据元素的自然顺序 对指定集合按升序进行排序。升序: 小->大降序: 大->小*/Collections.sort(list01);System.out.println(list01);//[1, 2, 3, 4]/*static void shuffle(List<?> list)  随机打乱集合中元素的顺序*/Collections.shuffle(list01);System.out.println(list01);//[4, 1, 3, 2]  [3, 4, 1, 2]  [2, 4, 1, 3]...ArrayList<String> list02 = new ArrayList<>();list02.add("ab");list02.add("aa");list02.add("12");list02.add("AB");list02.add("CC");System.out.println(list02);//[ab, aa, 12, AB, CC]/*static void  sort(List<T> list) 根据元素的自然顺序 对指定集合按升序进行排序。升序: 小->大降序: 大->小自然顺序:编码表的顺序  ASCII  0:48   A:65   a:97*/Collections.sort(list02);System.out.println(list02);//[12, AB, CC, aa, ab]}
}

2.Comparator比较器(重点)

package com.itheima.demo02Collections;import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;/*static <T> void sort(List<T> list, Comparator<? super T> c)  根据指定比较器产生的顺序对指定列表进行排序。参数:List<T> list:要排序的List集合Comparator<? super T> c:对集合进行排序的比较器jav.util.Comparator<T>接口强行对某个对象 collection 进行整体排序 的比较函数。Comparator接口中抽象方法:int compare(T o1, T o2) 比较用来排序的两个参数。参数:T o1, T o2:依次比较的集合中的元素[1,1,3, 2, 4]比较的规则:(重点)升序: o1-o2降序: o2-o1两个元素相等: o1==o2*/
public class Demo02Collections {public static void main(String[] args) {//创建ArrayList集合对象ArrayList<Integer> list01 = new ArrayList<>();list01.add(1);list01.add(3);list01.add(2);list01.add(4);System.out.println(list01);//[1, 3, 2, 4]//使用Collections集合中工具类中的sort方法,对集合中的元素根据比较器产生的规则排序Collections.sort(list01, new Comparator<Integer>() {@Overridepublic int compare(Integer o1, Integer o2) {//降序: o2-o1return o2-o1;}});System.out.println(list01);//[4, 3, 2, 1]//使用Collections集合中工具类中的sort方法,对集合中的元素根据比较器产生的规则排序Collections.sort(list01, new Comparator<Integer>() {@Overridepublic int compare(Integer o1, Integer o2) {//升序: o1-o2return o1-o2;}});System.out.println(list01);//[1, 2, 3, 4]}
}

扩展:

package com.itheima.demo02Collections;import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;public class Demo03Collections {public static void main(String[] args) {//创建ArrayList集合,泛型使用PersonArrayList<Person> list = new ArrayList<>();list.add(new Person("azhansan",18));list.add(new Person("lisi",20));list.add(new Person("wangwu",19));list.add(new Person("bzhaoliu",18));list.add(new Person("tianqi",21));//使用Collections集合工具类中的方法sort,根据比较器产生的规则进行排序(年龄升序排序)Collections.sort(list, new Comparator<Person>() {@Overridepublic int compare(Person o1, Person o2) {//年龄升序排序return o1.getAge()-o2.getAge();}});System.out.println(list);//[Person{name='zhansan', age=18}, Person{name='zhaoliu', age=18}, Person{name='wangwu', age=19}, Person{name='lisi', age=20}, Person{name='tianqi', age=21}]//按照两个人年龄升序排序,如果两个人年龄相同,按照姓名的首字母降序排序Collections.sort(list, new Comparator<Person>() {@Overridepublic int compare(Person o1, Person o2) {//按照两个人年龄升序排序int a = o1.getAge()-o2.getAge();//判断两个人的年龄是否相等if(a==0){//按照姓名的首字母降序排序a = o2.getName().charAt(0)-o1.getName().charAt(0);}return a;}});System.out.println(list);//System.out.println("aaa".charAt(0)-"bbb".charAt(0));//int compareTo(String anotherString) 按字典(编码表)顺序比较两个字符串。//按照两个人年龄升序排序,如果两个人年龄相同,按照姓名进行排序Collections.sort(list, new Comparator<Person>() {@Overridepublic int compare(Person o1, Person o2) {//按照两个人年龄升序排序int a = o1.getAge()-o2.getAge();//判断两个人的年龄是否相等if(a==0){//按照姓名进行排序a = o2.getName().compareTo(o1.getName());}return a;}});System.out.println(list);}
}
package com.itheima.demo02Collections;public class Person {private String name;private int age;public Person() {}public Person(String name, int age) {this.name = name;this.age = age;}@Overridepublic String toString() {return "Person{" +"name='" + name + '\'' +", age=" + age +'}';}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}
}

3.可变参数

package com.itheima.demo03VarArgs;import java.util.ArrayList;/*可变参数是JDK1.5之后出现的新特性作用:当我们定义一个方法的时候,方法的参数类型已经确定了,但是参数的个数不确定,就可以使用可变参数格式:修饰符 返回值类型 方法名(数据类型...变量名){方法体;}数据类型...变量名:叫可变参数,代表形式参数可以接收任意个数据调用参数是可变参数的方法,参数的个数可以传递任意个(不传递,1,2,3,4,5,6....n...)原理:可变参数底层就是一个数组,传递不同个数个参数,就会创建不同长度的数组,来接收这些参数*/
public class Demo01VarArgs {public static void main(String[] args) {//getSum();//getSum(10);//getSum(10,20);int s = getSum(10, 20, 30, 40, 50, 60, 70, 80, 90, 100);System.out.println(s);//550int[] arr = {1,2,3};int s2 = getSum(arr);System.out.println(s2);ArrayList<Integer> list = new ArrayList<>();list.add(1);list.add(2);//int s3 = getSum(list);//System.out.println(s3);method1(10,1.1,"aaa",1,2,3,4,4,5,65,6,7,7,8,8,8);method2(1,2,3,4,4,5,65,6,7,7,8,8,8);method2("a","v","c");}//可变参数的注意事项//1.一个方法的参数只能写一个可变参数//public static void method(int...a,String...s){}//2.方法的参数列表有多个参数,可变参数必须写在末尾//public static void method(int a,double b,int...d,String c){}//Vararg parameter must be the last in the list//public static void method(int a,double b,String c,int...d){}//可变参数终极写法public static void method1(Object...obj){}public static <T> T[] method2(T...t){return t;}/*定一个计算n个(不知道传递多少个整数,0,1,2,3,4,..)int类型整数和的方法已知:参数的数据类型:int类型未知:不知道计算多少个整数的和所以我们就可以使用可变参数,作为方法的参数使用,可以接收任意个同种数据类型的参数getSum();传递0个参数,那么可变参数就会创建一个长度为0的数组,用来接收参数 new int[]{ };getSum(10);传递1个参数,那么可变参数就会创建一个长度为1的数组,用来接收参数 new int[]{10};getSum(10,20);传递2个参数,那么可变参数就会创建一个长度为2的数组,用来接收参数 new int[]{10,20};...getSum(10,20,30,40,50,60,70,80,90,100);传递10个参数,那么可变参数就会创建一个长度为10的数组,用来接收参数 new int[]{10,20,30,40,50,60,70,80,90,100};*/public static int getSum(int...arr){//System.out.println(arr);//[I@4554617c 数组的地址值//System.out.println(arr.length);//0,1,2...数组的长度//对数组中的元素进行求和//定义一个变量,初始值为0,记录累加求和int sum = 0;//遍历数组(可变参数),获取数组中的每一个元素for (int i : arr) {//累加求和sum+=i;}//把和返回return sum;}/*定义计算四个int类型整数和的方法*//*public static int getSum(int a,int b,int c,int d){return a+b+c+d;}*//*定义计算三个int类型整数和的方法*//*public static int getSum(int a,int b,int c){return a+b+c;}*//*定义计算两个int类型整数和的方法*//*public static int getSum(int a,int b){return a+b;}*/
}

重点:记住可变参数可以接收任意个同种数据类型的元素

4.Collections集合工具类中的方法addAll

package com.itheima.demo03VarArgs;import java.util.ArrayList;
import java.util.Collections;/*java.util.Collections:操作集合的工具类,里边的方法都是静态的static <T> boolean  addAll(Collection<T> c, T... elements) 将所有指定元素添加到指定 collection 中。给集合添加多个元素*/
public class Demo02Collections {public static void main(String[] args) {ArrayList<Integer> list = new ArrayList<>();//list.add(1);//list.add(2);//list.add(3);//list.add(4);//list.add(5);Collections.addAll(list,1,2,3,4,5);System.out.println(list);ArrayList<String> list02 = new ArrayList<>();Collections.addAll(list02,"a","b","c","d","e");System.out.println(list02);//可变参数底层就是一个数组,传递可变参数的地方都可以传递数组String[] arr = {"aaa","bbb","ccc","ddd"};Collections.addAll(list02,arr);System.out.println(list02);//[a, b, c, d, e, aaa, bbb, ccc, ddd]}
}

第三章 Set接口

1.Set接口的介绍(记住)

/*java.util.Set<E>接口 extends Collection<E>接口Set接口的特点:1.不允许存储重复的元素  add(1) add(1) ==>集合中只有一个12.不包含带索引的方法,里边的方法和Collection接口是一样
*/

2.HashSet集合的介绍和基本使用(重点)

package com.itheima.demo04Set;import java.util.HashSet;
import java.util.Iterator;/*java.util.HashSet<E>集合 implements Set<E>接口此类实现 Set 接口,由哈希表(实际上是一个 HashMap 实例)支持。它不保证 set 的迭代顺序;特别是它不保证该顺序恒久不变。此类允许使用 null 元素。HashSet集合的特点:1.不允许存储重复的元素2.不包含带索引的方法(不能使用普通的for循环遍历Set集合)3.是一个无序的集合(存储的元素和取出的元素顺序有可能不一致)4.底层是一个哈希表JDK1.8之前:数组+单向链表JDK1.8之后:数组+单向链表|数组+红黑树(可以提高查询的效率)*/
public class Demo01HashSet {public static void main(String[] args) {show02();}private static void show02() {//创建HashSet集合对象HashSet<String> set = new HashSet<>();boolean b1 = set.add("aaa");System.out.println("b1:"+b1);//b1:trueset.add("bbb");set.add("ccc");boolean b2 = set.add("aaa");System.out.println("b2:"+b2);//b2:falseset.add("ddd");//使用增强for循环遍历Set集合for (String s : set) {System.out.println(s);}}private static void show01() {//创建HashSet集合对象HashSet<Integer> set = new HashSet<>();set.add(1);set.add(3);set.add(2);set.add(4);set.add(1);//使用迭代器遍历Set集合Iterator<Integer> it = set.iterator();while (it.hasNext()){Integer s = it.next();System.out.println(s);}}
}

3.哈希值(了解)

package com.itheima.demo04Set;/*哈希值:是一个十进制的整数,由操作系统随机给出,我们打印对象的地址值,使用的就是哈希值(逻辑地址)对象在内存中实际存储的地址,并不是哈希值的地址(物理地址)Object类有获取对象哈希值的方法:int hashCode() 返回该对象的哈希码值。hashCode方法的底层源码:public native int hashCode();native:调用的是操作系统底层的方法,不是由java语言编写的*/
public class Demo02HashCode {public static void main(String[] args) {//Person类默认继承了Object类,所以可以使用Object类中的hashCode方法Person p1 = new Person();int h1 = p1.hashCode();System.out.println(h1);//1163157884==>1Person p2 = new Person();int h2 = p2.hashCode();System.out.println(h2);//1956725890==>1/*Object类的toString方法String toString() 返回该对象的字符串表示。底层源码:public String toString() {return getClass().getName() + "@" + Integer.toHexString(hashCode());}getClass().getName():使用反射技术,获取包名+类名  com.itheima.demo04Set.Person"@":字符串原样输出Integer.toHexString(hashCode()):把hashCode方法返回的十进制的整数,转换为十六进制*/System.out.println(p1.toString());//com.itheima.demo04Set.Person@4554617c==>1System.out.println(p2.toString());//com.itheima.demo04Set.Person@74a14482==>1System.out.println(p1==p2);//比较的是对象的地址值 false}
}
package com.itheima.demo04Set;public class Person extends Object {@Overridepublic int hashCode() {return 1;}
}

4.String类的哈希值(了解)

package com.itheima.demo04Set;/*String类的哈希值(了解)String类重写了Object类的hashCode方法规则:相同的字符串返回的哈希值是一样的不同的字符串,计算出的哈希值也有可能是一样*/
public class Demo03StringHashCode {public static void main(String[] args) {String s1 = new String("abc");String s2 = new String("abc");System.out.println(s1.hashCode());//96354System.out.println(s2.hashCode());//96354System.out.println(s1==s2);//falseSystem.out.println("重地".hashCode());//1179395System.out.println('重'+0);//37325System.out.println('地'+0);//22320System.out.println("通话".hashCode());//1179395}
}

请添加图片描述

5.HashSet集合存储数据的结构(哈希表)

请添加图片描述

6.使用HashSet集合存储String不重复的原理(了解)

package com.itheima.demo04Set;/*String类的哈希值(了解)String类重写了Object类的hashCode方法规则:相同的字符串返回的哈希值是一样的不同的字符串,计算出的哈希值也有可能是一样*/
public class Demo03StringHashCode {public static void main(String[] args) {String s1 = new String("abc");String s2 = new String("abc");System.out.println(s1.hashCode());//96354System.out.println(s2.hashCode());//96354System.out.println(s1==s2);//falseSystem.out.println("重地".hashCode());//1179395System.out.println('重'+0);//37325System.out.println('地'+0);//22320System.out.println("通话".hashCode());//1179395}
}

请添加图片描述

7.HashSet存储自定义类型元素(重点中的重点)

package com.itheima.demo05HashSet;import java.util.HashSet;/*HashSet存储自定义类型元素(重点中的重点)要求:同名同年龄的人,视为同一个人,只能存储一次保证:HashSet集合存储Person类必须重写hashCode和equals方法,来保证元素唯一重点:使用HashSet集合存储自定义类型的元素(Person,Student,Animal...),自定义元素必须重写hashCode和equals方法,来保证元素唯一快捷键: alt+insert  选择 equals and hashCode思考:使用ArrayList集合存储Person,Person需要重写hashCode和equals方法吗? 1需要 2不需要(正确)ArrayList集合元素是允许重复,add方法也不会调用元素hashCode和equals方法*/
public class Demo02HashSetSavePerson {public static void main(String[] args) {//创建HashSet集合对象,泛型使用PersonHashSet<Person> set = new HashSet<>();Person p1 = new Person("a",10);Person p2 = new Person("a",10);System.out.println(p1.hashCode());//1163157884System.out.println(p2.hashCode());//1956725890set.add(p1);set.add(p2);set.add(new Person("b",11));set.add(new Person("c",12));set.add(new Person("d",13));set.add(new Person("e",14));set.add(new Person("f",15));set.add(new Person("b",9));//遍历Set集合for (Person p : set) {System.out.println(p);}}
}
package com.itheima.demo05HashSet;import java.util.Objects;public class Person {private String name;private int age;public Person() {}public Person(String name, int age) {this.name = name;this.age = age;}/*重写Object类的hashCode方法:模拟String类name本身就是一个字符串,可以直接调用String类的hashCode方法,获取字符串的哈希值name.hashCode()+age;set.add(new Person("a",10));  哈希值:97+10=107set.add(new Person("a",10));  哈希值:97+10=107两个人的哈希值都是107,使用equals方法,比较两个人,equals返回true==>认定两个元素相同,只能存储一个----------------------------------------------------name.hashCode()+age;set.add(new Person("a",10));  哈希值:97+10=107set.add(new Person("b",9));   哈希值:98+9=107两个人的哈希值都是107,使用equals方法,比较两个人,equals返回false==>认定两个元素不同,都会存储到集合中----------------------------------------------------降低相同哈希值出现的几率:可以避免两个元素比较equals方法,可以提高程序的效率name.hashCode()*2+age;set.add(new Person("a",10));  哈希值:97*2+10=204set.add(new Person("b",9));   哈希值:98*2+9=205name.hashCode()*31+age;*/@Overridepublic int hashCode() {int result = name != null ? name.hashCode() : 0;result = 31 * result + age;return result;}@Overridepublic boolean equals(Object o) {if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;Person person = (Person) o;if (age != person.age) return false;return name != null ? name.equals(person.name) : person.name == null;}@Overridepublic String toString() {return "Person{" +"name='" + name + '\'' +", age=" + age +'}';}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}
}

2.LinkedHashSet集合(了解)

package com.itheima.demo06Set;import java.util.HashSet;
import java.util.LinkedHashSet;/*java.util.LinkedHashSet<E> extends HashSet<E>具有可预知迭代顺序的 Set 接口的哈希表和链接列表实现。此实现与 HashSet 的不同之外在于,后者维护着一个运行于所有条目的双重链接列表。LinkedHashSet特点:1.不允许存储重复元素2.没有带索引的方法3.底层是哈希表+单向链表JDK1.8之前:数组+单向链表+单向链表JDK1.8之后:数组+单向链表+单向链表|数组+红黑树+单向链表(可以提高查询的效率)结构就是一个双向链表,可以保证迭代的顺序,是一个有序的集合*/
public class Demo01LinkedHashSet {public static void main(String[] args) {HashSet<String> set = new HashSet<>();set.add("aaa");set.add("bbb");set.add("ccc");set.add("ddd");set.add("aaa");System.out.println(set);//[aaa, ccc, bbb, ddd]  不允许存储重复元素,是一个无序集合LinkedHashSet<String> linked = new LinkedHashSet<>();linked.add("aaa");linked.add("bbb");linked.add("ccc");linked.add("ddd");linked.add("aaa");System.out.println(linked);//[aaa, bbb, ccc, ddd] 不允许存储重复元素,是一个有序集合}
}

3.TreeSet集合(使用)

package com.itheima.demo06Set;import java.util.Comparator;
import java.util.TreeSet;/*java.util.TreeSet<E>集合 implements Set<E>接口基于Set接口的红黑树的实现使用元素的自然顺序对元素进行排序(内部会使用Comparator比较器对元素进行默认的升序排序)或者根据创建TreeSet集合时提供的 Comparator 进行排序,具体取决于使用的构造方法。构造方法:TreeSet() 构造一个新的空 set,该 set 根据其元素的自然顺序进行排序。TreeSet(Comparator<? super E> comparator) 构造一个新的空 TreeSet,它根据指定比较器进行排序。TreeSet集合特点:1.不允许存储重复元素2.没有带索引的方法3.底层是一个红黑树结构(元素是有序的)4.可以根据比较器产生的规则对元素进行排序*/
public class Demo02TreeSet {public static void main(String[] args) {//使用空参数构造方法创建TreeSet集合对象TreeSet<Integer> set1 = new TreeSet<>();set1.add(100);set1.add(1);set1.add(-100);set1.add(88);set1.add(66);set1.add(77);set1.add(11);System.out.println(set1);//[-100, 1, 11, 66, 77, 88, 100]//使用带比较器的构造方法创建TreeSet集合对象TreeSet<Integer> set2 = new TreeSet<>(new Comparator<Integer>() {@Overridepublic int compare(Integer o1, Integer o2) {//o1-o2:升序  o2-o1:降序return o2-o1;}});set2.add(100);set2.add(1);set2.add(-100);set2.add(88);set2.add(66);set2.add(77);set2.add(11);System.out.println(set2);//[100, 88, 77, 66, 11, 1, -100]}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/70411.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Zookeeper(58)如何在Zookeeper中实现分布式锁?

在 Zookeeper 中实现分布式锁是一种常见的用例。Zookeeper 提供了强一致性、高可用性的分布式协调服务&#xff0c;使得它非常适合用来实现分布式锁。以下是详细的步骤和代码示例&#xff0c;展示如何在 Zookeeper 中实现分布式锁。 1. Zookeeper 分布式锁的基本原理 Zookeep…

帆软报表FineReport入门:简单报表制作[扩展|左父格|上父格]

FineReport帮助文档 - 全面的报表使用教程和学习资料 数据库连接 点击号>>JDBC 选择要连接的数据库>>填写信息>>点击测试连接 数据库SQLite是帆软的内置数据库, 里面有练习数据 选择此数据库后,点击测试连接即可 数据库查询 方法一: 在左下角的模板数据集…

后台管理系统-项目初始化

认识vue-admin **核心交付:** 为什么要基于现成架子二次开发 什么是二次开发:基于已有的代码(项目工程,脚手架)开进行新功能的开发 所以看懂已有的框架中的既有代码,变得很重要了 1. 背景知识 后台管理系统是一种最常见的应用模式,不同的管理系统之间有很多相似的地方…

DAY07 Collection、Iterator、泛型、数据结构

学习目标 能够说出集合与数组的区别数组:1.是引用数据类型的一种2.可以存储多个元素3.数组的长度是固定的 int[] arr1 new int[10]; int[] arr2 {1,2,3};4.数组即可以存储基本类型的数据,又可以存储引用数据类型的数据int[],double[],String[],Student[]集合:1.是引用数据类…

VLM(视觉语言模型)与DeepSeek R1(奖励机制)如何结合

VLM&#xff08;视觉语言模型&#xff09;与DeepSeek R1&#xff08;奖励机制&#xff09;如何结合 flyfish VLM的传统训练依赖于监督学习&#xff08;直接拟合问答对&#xff09;&#xff0c;而规则奖励函数通常用于强化学习&#xff08;通过试错和奖励反馈优化策略&#xf…

从零开始构建一个语言模型中vocab_size(词汇表大小)的设定规则

从零开始构建一个语言模型就要设计一个模型框架,其中要配置很多参数。在自然语言处理任务中,vocab_size(词汇表大小) 的设定是模型设计的关键参数之一,它直接影响模型的输入输出结构、计算效率和内存消耗。 本文是在我前文的基础上讲解的:从零开始构建一个小型字符级语言…

计算机网络之物理层——基于《计算机网络》谢希仁第八版

(꒪ꇴ꒪ )&#xff0c;Hello我是祐言QAQ我的博客主页&#xff1a;C/C语言&#xff0c;数据结构&#xff0c;Linux基础&#xff0c;ARM开发板&#xff0c;网络编程等领域UP&#x1f30d;快上&#x1f698;&#xff0c;一起学习&#xff0c;让我们成为一个强大的攻城狮&#xff0…

实时股票行情接口与WebSocket行情接口的应用

实时股票行情接口与WebSocket行情接口的应用 实时股票行情接口是量化交易和投资决策的核心工具之一&#xff0c;行情接口的种类和功能也在不断扩展。介绍几种常见的行情接口&#xff0c;包括实时股票行情接口、Level2行情接口、WebSocket行情接口以及量化行情接口&#xff0c;…

图论 之 BFS

文章目录 3243.新增道路查询后的最短距离1311.获取你好友已观看的视频 BFS:广度优先搜索&#xff08;BFS&#xff09; 是一种常用的算法&#xff0c;通常用于解决图或树的遍历问题&#xff0c;尤其是寻找最短路径或层级遍历的场景。BFS 的核心思想是使用队列&#xff08;FIFO 数…

ollama stream“:True django如何返回数据

在使用 Django 框架开发 Web 应用时&#xff0c;如果你想要通过 Ollama 流式返回数据&#xff0c;你可以通过 Django 的 HttpResponse 或者 StreamingHttpResponse 来实现。Ollama 主要用于处理文本生成任务&#xff0c;如聊天机器人、自动完成等&#xff0c;通常这些任务会产生…

为什么要用 const 和 let,而不是 var?

JavaScript 中有三种方式声明变量&#xff1a;var、let 和 const。其中&#xff0c;var 是早期版本的 JavaScript 中的标准&#xff0c;但随着 ECMAScript 6&#xff08;ES6&#xff09;引入了 let 和 const&#xff0c;var 的种种问题也显现出来。今天&#xff0c;我们将探讨为…

从零开始玩转TensorFlow:小明的机器学习故事 2

你好&#xff0c;TensorFlow&#xff01;——从零开始的第一个机器学习程序 1. 为什么要写这个“Hello, TensorFlow!”&#xff1f; 无论学习什么新语言或新框架&#xff0c;“Hello World!”示例都能帮助我们快速确认开发环境是否就绪&#xff0c;并掌握最基本的使用方式。对…

【Java八股文】10-数据结构与算法面试篇

【Java八股文】10-数据结构与算法面试篇 数据结构与算法面试题数据结构红黑树说一下跳表说一下&#xff1f;LRU是什么&#xff1f;如何实现&#xff1f;布隆过滤器怎么设计&#xff1f;时间复杂度&#xff1f; 排序算法排序算法及空间复杂度 数据结构与算法面试题 数据结构 红…

Docker换源加速(更换镜像源)详细教程(2025.2最新可用镜像,全网最详细)

文章目录 前言可用镜像源汇总换源方法1-临时换源换源方法2-永久换源&#xff08;推荐&#xff09;常见问题及对应解决方案1.换源后&#xff0c;可以成功pull&#xff0c;但是search会出错 补充1.如何测试镜像源是否可用2.Docker内的Linux换源教程 换源速通版&#xff08;可以直…

华为云deepseek大模型平台:deepseek满血版

华为云硅基流动使用Chatbox接入DeepSeek-R1满血版671B 1、注册&#xff1a; 华为云deepseek大模型平台注册&#xff1a;https://cloud.siliconflow.cn/i/aDmz6aVN 说明&#xff1a;填写邀请码的话邀请和被邀请的账号都会获得2000 万 Tokens&#xff1b;2个帐号间不会与其他关联…

抓包工具是什么?

抓包工具是一种用于捕获和分析网络数据包的软件或硬件设备。它可以帮助用户监控网络通信过程&#xff0c;查看网络中传输的数据内容、协议类型、源地址、目的地址等信息。以下是关于抓包工具的一些详细解释&#xff1a; 1. 主要功能 捕获数据包&#xff1a;抓包工具能够实时捕…

51c大模型~合集71

我自己的原文哦~ https://blog.51cto.com/whaosoft/12260659 #大模型推理加速技术的学习路线 EfficientQAT 可以在 41 小时内在单个 A100-80GB GPU 上完成对 2-bit Llama-2-70B 模型的量化感知训练。与全精度模型相比&#xff0c;精度仅下降了不到 3%&#xff08;69.48 v…

OpenBMC:BmcWeb实例化App

BmcWeb是OpenBMC的一个核心模块&#xff0c;对外负责响应Redfish请求&#xff0c;并且由于OpenBMC的Web使用的Redfish api&#xff0c;所以BmcWeb也是Web的后台。 1.main函数 //src\webserver_main.cpp #include "webserver_run.hpp"int main(int /*argc*/, char**…

利用AI优化可再生能源管理:Python让绿色能源更高效

利用AI优化可再生能源管理&#xff1a;Python让绿色能源更高效 引言 在全球气候变化和能源危机的背景下&#xff0c;可再生能源的利用变得尤为重要。然而&#xff0c;可再生能源的管理和优化面临诸多挑战&#xff0c;如能源生产的不稳定性和能源需求的波动性。幸运的是&#…

改BUG:Mock测试的时候,when失效

问题再现&#xff1a; 这里我写了一测试用户注册接口的测试类&#xff0c;并通过when模拟下层的服务&#xff0c;但实际上when并没有奏效&#xff0c;还是走了真实的service层的逻辑。 package cn.ac.evo.review.test;import cn.ac.evo.review.user.UserMainApplication; imp…