🌸集合体系总复习 整理导图 我整理的导图:
🌸Collection集合体系 Collection接口
代码
package 基础知识.集合.Collection.Collection接口;import java.util.ArrayList;import java.util.Collection;public class Collection_Lei { public static void main (String[] args) { Collection<String> arrayList = new ArrayList <String>(); arrayList.add("问君能有几多愁," ); arrayList.add("恰似一江春水向东流。" ); System.out.println(arrayList); System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" ); boolean boo = arrayList.add("流呀流流到外婆桥" ); System.out.println(boo+" " +arrayList); System.out.println("==============================================" ); boolean boo1 = arrayList.remove(1 ); boolean boo2 = arrayList.remove("流呀流流到外婆桥" ); System.out.println(boo1 + " " + boo2 + arrayList); System.out.println("==============================================" ); arrayList.clear(); System.out.println(arrayList); System.out.println("==============================================" ); arrayList.add("问君能有几多愁," ); arrayList.add("恰似一江春水向东流。" ); boolean boole = arrayList.contains("恰似一江春水向东流。" ); System.out.println(boole+" " +arrayList); System.out.println("==============================================" ); boolean booE = arrayList.isEmpty(); System.out.println(booE + " " + arrayList); System.out.println("==============================================" ); int size = arrayList.size(); System.out.println(size + " " + arrayList); System.out.println("==============================================" ); } }
package 基础知识.集合.Collection.Collection接口;import java.util.ArrayList;import java.util.Collection;import java.util.Iterator;public class Iterator_Method { public static void main (String[] args) { Collection<String> list = new ArrayList <>(); list.add("问世间情为何物," ); list.add("直教人生死相许。" ); Iterator<String> it = list.iterator(); System.out.println(it.next()); System.out.println(it.next()); System.out.println("===================================" ); list.add("本是两情相愿" ); list.add("又其在朝朝暮暮" ); Iterator<String> ite = list.iterator(); if (ite.hasNext()){ System.out.println(1 +": " +ite.next()); } if (ite.hasNext()){ System.out.println(2 +": " +ite.next()); } if (ite.hasNext()){ System.out.println(3 +": " +ite.next()); } if (ite.hasNext()){ System.out.println(4 +": " +ite.next()); } if (ite.hasNext()){ System.out.println(5 +": " +ite.next()); } if (ite.hasNext()){ System.out.println(6 +ite.next()); } System.out.println("=====使用while循环=====" ); Iterator<String> itr = list.iterator(); int i = 1 ; while (itr.hasNext()){ String str = itr.next(); System.out.println((i++)+": " +str); } } }
案例
package 基础知识.集合.Collection.Collection接口.Collection案例;import java.util.ArrayList;import java.util.Collection;import java.util.Iterator;public class Run_Collection { public static void main (String[] args) { Student stu1 = new Student ("何雪冲" ,16 ); Student stu2 = new Student ("何雪虫" ,18 ); Student stu3 = new Student ("何学匆" ,20 ); Collection<Student> list = new ArrayList <>(); list.add(stu1); list.add(stu2); list.add(stu3); Iterator<Student> itr = list.iterator(); while (itr.hasNext()){ Student stu = itr.next(); System.out.println("我叫: " + stu.getName() + ", 今年" + stu.getAge() + "岁了。" ); } } }
package 基础知识.集合.Collection.Collection接口.Collection案例;public class Student { private String name; private int age; public Student () { } public Student (String name, int age) { this .name = name; this .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; } }
List集合 2.1List集合概述和特点 List集合概述
有序集合(也称为序列),用户可以精确控制列表中每个元素的插入位置。用户可以通过整数索引访问元素, 并搜索列表中的元素
与Set集合不同,列表通常允许重复的元素
List集合特点
有序:存储和取出的元素顺序一致
可重复:存储的元素可以重复
代码
package 基础知识.集合.Collection.List.List集合;import java.util.ArrayList;import java.util.Iterator;import java.util.List;public class List_interface { public static void main (String[] args) { List<String> list = new ArrayList <>(); list.add("天青日照树下溪" ); list.add("孩童赤脚溪水欢" ); list.add("惊蛙扑通跳水去" ); list.add("溅水飞入湛蓝天" ); list.add("恰是夏日微风起" ); list.add("===========" ); list.add("天青日照树下溪" ); list.add("孩童赤脚溪水欢" ); list.add("惊蛙扑通跳水去" ); list.add("溅水飞入湛蓝天" ); list.add("恰是夏日微风起" ); list.add("===========" ); System.out.println(list); System.out.println("==============================================================================" ); System.out.println("有序集合,可以通过索引值调用相应位置元素" ); System.out.println(list.get(0 )); System.out.println(list.get(1 )); System.out.println("==============================================================================" ); for (int i = 0 ; i < list.size(); i++) { System.out.println(list.get(i)); } System.out.println("通过iterator迭代器遍历" ); Iterator<String> itr = list.iterator(); while (itr.hasNext()){ System.out.println(itr.next()); } } }
Method代码
package 基础知识.集合.Collection.List.List集合;import java.util.ArrayList;import java.util.List;public class List_interface_Method { public static void main (String[] args) { System.out.print("1: " ); List<Double> list1 = new ArrayList <>(); list1.add(0.0 ); list1.add(1.0 ); list1.add(2.0 ); list1.add(3.0 ); list1.add(2 ,1.5 ); System.out.println(list1); System.out.println("list1[2] = " +list1.get(2 )); System.out.println("==========================================================================" ); System.out.print("2: " ); List<Integer> list2 = new ArrayList <>(); list2.add(0 ); list2.add(1 ); list2.add(2 ); list2.add(3 ); int LIST_2 = list2.remove(0 ); System.out.println(list2); System.out.println("被删除的元素是: " + LIST_2); System.out.println("==========================================================================" ); System.out.print("3: " ); List<Integer> list3 = new ArrayList <>(); list3.add(0 ); list3.add(1 ); list3.add(2 ); list3.add(3 ); int LIST_3 = list3.set(0 , 666 ); System.out.println(list3); System.out.println("被修改的原元素是: " +LIST_3); System.out.println("==========================================================================" ); System.out.print("4: " ); List<Integer> list4 = new ArrayList <>(); list4.add(0 ); list4.add(1 ); list4.add(2 ); list4.add(3 ); System.out.print("[" ); for (int i = 0 ; i < list4.size(); i++) { if (i != (list4.size()-1 )){ System.out.print(list4.get(i)+"," ); } else { System.out.print(list4.get(i)); } } System.out.println("]" ); System.out.println("==========================================================================" ); } }
增强for循环 增强for:简化数组和Collection集合的遍历
实现Iterable接口的类允许其对象成为增强型for语句的目标
它是JDK5之后出现的,其内部原理是一个lterator迭代器
增强for的格式
格式:
for (元素数据类型变量名:数组或者Collection集合){ }
范例:
int0[] ar r= {1 ,2 ,3 ,4 ,5 }; for (int i:arr){ System.out.printIn(i); }
演示
package 基础知识.集合.Collection.List.List集合.增强for 循环;public class Foreach { public static void main (String[] args) { int [] array = {0 ,1 ,3 ,4 ,5 ,6 }; for (int num : array) { System.out.println(num); } System.out.println("====" ); for (int num : array) { System.out.println(num); } } }
Iterator迭代器 Listlterator
Listlterator:列表迭代器
通过List集合的llistlterator0方法得到,所以说它是List集合特有的迭代器
用于允许程序员沿任一方向遍历列表的列表迭代器,在迭代期间修改列表,并获取列表中迭代器的当前位置
Listlterator中的常用方法
E next():返回迭代中的下一个元素
boolean hasNext():如果迭代具有更多元素,则返回true
E previous():返回列表中的上一个元素
boolean hasPrevious():如果此列表迭代器在相反方向遍历列表时具有更多元素,则返回true
void add(E e):将指定的元素插入列表
演示
package 基础知识.集合.Collection.List.List集合.ListIterator迭代器;import java.util.ArrayList;import java.util.List;import java.util.ListIterator;public class ListIterator_Method { public static void main (String[] args) { List<String> list = new ArrayList <>(); list.add("亚索" ); list.add("永恩" ); list.add("万豪" ); ListIterator<String> listItr = list.listIterator(); while (listItr.hasNext()){ String str = listItr.next(); System.out.println(str); } System.out.println("========" ); while (listItr.hasPrevious()){ String str = listItr.previous(); System.out.println(str); } System.out.println("========" ); System.out.println("===========================================================================================" ); ListIterator<String> listIte = list.listIterator(); while (listIte.hasPrevious()){ String str = listIte.previous(); System.out.println(str); } System.out.println("输出结果: " ); System.out.println( listIte.next() ); System.out.println( listIte.previous() ); System.out.print("重点: " ); ListIterator<String> listIterator = list.listIterator(); while (listIterator.hasNext()){ String str = listIterator.next(); if (str.equals("亚索" )){ listIterator.add("牛批,卢本伟没有开挂!" ); } } System.out.println(list); } }
源码分析 (List) 代码
public interface List <E>{ Iterator<E> iterator () ; ListIterator<E> listIterator () ; } public abstract class AbstractList <E>{ protected transient int modCount = 0 ; } public class ArrayList <E> extends AbstractList <E> implements List <E>{ private class Itr implements Iterator <E> { } public ListIterator<E> listIterator () { return new ListItr (0 ); } private class ListItr extends Itr implements ListIterator <E> { public void add (E e) { checkForComodification(); try { int i = cursor; ArrayList.this .add(i, e); cursor = i + 1 ; lastRet = -1 ; expectedModCount = modCount; } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException (); } } } final void checkForComodification () { if (modCount != expectedModCount) throw new ConcurrentModificationException (); } }
不同点分析:
ListItr
中的add方法:
private class ListItr extends Itr implements ListIterator <E> {public void add (E e) { checkForComodification(); try { int i = cursor; ArrayList.this .add(i, e); cursor = i + 1 ; lastRet = -1 ; expectedModCount = modCount; } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException (); } } }
其中add方法里面多了一步: expectedModCount = modCount;
重新就导致两数相等 则modCount != expectedModCount
为false
就不会抛出异常。
List并发修改异常 并发修改异常
ConcurrentModificationException
产生原因
迭代器遍历的过程中,通过集合对象修改了集合中元素的长度,造成了迭代器获取元素中判断预期修改值和实际修改值不一致
解决方案
案例代码
package 基础知识.集合.Collection.List.List集合.List并发修改异常;import java.util.ArrayList;import java.util.Iterator;import java.util.List;public class List_Yi { public static void main (String[] args) { List<String> list = new ArrayList <>(); list.add("亚索" ); list.add("永恩" ); list.add("万豪" ); Iterator<String> itr = list.iterator(); for (int i = 0 ; i < list.size(); i++) { String str = list.get(i); if (str.equals("亚索" )){ list.add("牛批,卢本伟没有开挂!" ); } } System.out.println(list); } }
分析
并发修改异常:
程序原码(简略):
public interface List <E>{ Iterator<E> iterator () ; boolean add (E e) ; } public abstract class AbstractList <E>{ protected transient int modCount = 0 ; } public class ArrayList <E> extends AbstractList <E> implements List <E>{ public E get (int index) { Objects.checkIndex(index, size); return elementData(index); } public boolean add (E e) { modCount++; add(e, elementData, size); return true ; } public Iterator<E> iterator () { return new Itr (); } private class Itr implements Iterator <E> { int expectedModCount = modCount; public boolean hasNext () { return cursor != size; } public E next () { checkForComodification(); int i = cursor; if (i >= size) throw new NoSuchElementException (); Object[] elementData = ArrayList.this .elementData; if (i >= elementData.length) throw new ConcurrentModificationException (); cursor = i + 1 ; return (E) elementData[lastRet = i]; } final void checkForComodification () { if (modCount != expectedModCount) throw new ConcurrentModificationException (); } } }
分析:
24行出现的问题:String str = itr.next();
向上是ArrayList的内部类Itr的next方法问题。
next方法先调用了checkForComodification();
方法:
>class ArrayList <E>{ >final void checkForComodification () { if (modCount != expectedModCount) throw new ConcurrentModificationException (); >} >}
当modCount != expectedModCount
时, 就抛出了ConcurrentModificationException
异常
原因:
modCount
是实际修改集合的次数,expectedModCount
是预期修改集合的次数
预期修改集合的次数在内部类Itr中
:int expectedModCount = modCount;
在Itr当中,是将实际修改集合次
数赋值给了预期修改集合次数
,所以说一开始是一样的,不会报错,但是当进行某些操作时,可能会发生变化
变化:
modCount是实际修改集合的次数
的来源在ArrayList<E>
的父类AbstractList<E>
当中
protected transient int modCount = 0;
protected修饰子类ArrayList<E>
继承父类
所以一开始,modCount
是实际修改集合的次数,expectedModCount
是预期修改集合的次数 两个值都是相等的0,每次Itr.next()
都要先执行checkForComodification();
>class ArrayList <E>{ >public E next () { checkForComodification(); int i = cursor; if (i >= size) throw new NoSuchElementException (); Object[] elementData = ArrayList.this .elementData; if (i >= elementData.length) throw new ConcurrentModificationException (); cursor = i + 1 ; return (E) elementData[lastRet = i]; } >}
当进行:
>if (str.equals("亚索")){ list.add("牛批,卢本伟没有开挂!"); >}
操作时,调用了add方法,跟进add方法:
>class ArrayList <E>{ >public boolean add (E e) { modCount++; add(e, elementData, size); return true ; >} >}
add方法当中,出现了modCount++;
也就是,实际修改集合次数加1, 而expectedModCount
是预期修改集合的次数没有加1,
导致modCount != expectedModCount
执行抛出异常
解决方案
使用 get方法 和 for循环 进行解决
>class ArrayList <E>{ >public E get (int index) { Objects.checkIndex(index, size); return elementData(index); >} >}
get方法没有像next方法一样,get没有checkForComodification();
判断是否相等,更不会抛出ConcurrentModificationException
异常
for循环y y d s
增强for循环 yyds
List子集合 List集合子类特点
List集合常用子类:ArrayList,LinkedList
ArrayList:底层数据结构是数组,查询快,增删慢
LinkedList:底层数据结构是链表,查询慢,增删快
📌ArrayList 前面已经讲过了
📌LinkedList
代码
package 基础知识.集合.Collection.List.List子集合.LinkedList;import java.util.Iterator;import java.util.LinkedList;public class LinkedList_Impl { public static void main (String[] args) { LinkedList<String> linkedList = new LinkedList <>(); linkedList.add("亚索" ); linkedList.add("哥哥" ); linkedList.add("你好浪呀" ); linkedList.add(1 ,"呦哟呦" ); System.out.println(linkedList); System.out.println("========================" ); linkedList.addFirst("哇!" ); linkedList.addLast("不过我稀饭" ); System.out.println(linkedList); System.out.println("========================" ); String strFirst = linkedList.removeFirst(); String strLast = linkedList.removeLast(); System.out.println("移除第一个元素:" + strFirst); System.out.println("移除最后一个元素" + strLast); System.out.println(linkedList); System.out.println("========================" ); String strGetFirst = linkedList.getFirst(); String strGetLast = linkedList.getLast(); System.out.println("获取第一个元素:" + strGetFirst); System.out.println("获取最后一个元素:" + strGetLast); System.out.println(linkedList); System.out.println("========================" ); System.out.println("三种方式遍历LinkedList集合" ); Iterator<String> itr = linkedList.iterator(); while (itr.hasNext()){ String str = itr.next(); System.out.println(str); } System.out.println("========================" ); for (int i = 0 ; i < linkedList.size(); i++) { System.out.println(linkedList.get(i)); } System.out.println("========================" ); for (String name: linkedList) { System.out.println(name); } System.out.println("========================" ); } }
Set集合 哈希值
概述:
哈希值:是JDK根据对象的地址
或者字符串
或者数字
算出来的int类型的数值
Object
当中有一个hashCode()
方法,可以调用某处的哈希值
public int Object()
返回该对象的哈希码值。
[注意]:
数字是用不了hashCode()方法的。
在默认情况下, hashCode()方法没有被覆盖重写的情况下:
不同对象的哈希值是不同的。
对于字符串来说,stu1对象中的字符串[何雪聪]和str字符串的[何雪聪]哈希值是相同的。
不同的字符串的哈希值也有可能是相同的!
代码
package 基础知识.集合.Collection.Set.Set集合.哈希值;public class Run_HashCode { public static void main (String[] args) { Student stu1 = new Student ("何雪聪" , 20 ); int iCode1 = stu1.hashCode(); int iCode2 = stu1.hashCode(); if (iCode1 == iCode2) { System.out.println("同一个对象多次调用哈希值相同。" ); } Student stu2 = new Student ("何雪聪" , 20 ); int jCode1 = stu2.hashCode(); int jCode2 = stu2.hashCode(); if (jCode1 == jCode2) { System.out.println("同一个对象多次调用哈希值相同。" ); } System.out.println("========================" ); if (iCode1 == jCode1) { System.out.println("重写了hashCode()方法" ); } else if (iCode1 != jCode1) { System.out.println("不同对象的哈希值是不同的" ); } System.out.println("========================" ); System.out.println("字符串的哈希值" ); System.out.println("亚索: " + "亚索" .hashCode()); System.out.println("========================" ); System.out.println("重地: " +"重地" .hashCode()); System.out.println("通话: " +"通话" .hashCode()); if ("重地" .hashCode() == "通话" .hashCode()){ System.out.println("不同的字符串的哈希值可能相同!" ); } System.out.println("========================" ); String str = "何雪聪" ; System.out.println("何雪聪: " + str.hashCode()); System.out.println("stu1对象的name: " + stu1.getName().hashCode()); if (stu1.getName().hashCode() == "何雪聪" .hashCode()){ System.out.println("stu对象中的字符串[何雪聪]和str字符串的[何雪聪]哈希值相同。" ); } int i = 1 ; } }
package 基础知识.集合.Collection.Set.Set集合.哈希值;public class Student { private String name; private int age; public Student () { } public Student (String name, int age) { this .name = name; this .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; } }
Set集合概述和特点
Set集合特点
不包含重复元素的集合
没有带索引的方法,所以不能使用普通f和循环遍历
代码
package 基础知识.集合.Collection.Set.Set集合;import java.util.HashSet;import java.util.Iterator;import java.util.Set;public class Set_Interface { public static void main (String[] args) { Set<String> setSet = new HashSet <>(); setSet.add("这就尴尬了" ); setSet.add("问君能有几多愁" ); setSet.add("恰似一江春水向东流" ); setSet.add("问君能有几多愁" ); System.out.println(setSet); System.out.println("================================================" ); System.out.println("================================================" ); Iterator<String> itr = setSet.iterator(); while (itr.hasNext()){ System.out.println(itr.next()); } System.out.println("================================================" ); for (String name : setSet) { System.out.println(name); } System.out.println("================================================" ); System.out.println(setSet.hashCode()); } }
Set子集合 📌HashSet HashSet集合概述和特点
底层数据结构是哈希表
对集合的迭代顺序不作任何保证,也就是说不保证存储和取出的元素顺序致
没有带索引的方法,所以不能使用普通for循环遍历
由于是Set集合,所以是不包含重复元素的集合
代码
package 基础知识.集合.Collection.Set.Set子集合.HashSet;import java.util.HashSet;import java.util.Iterator;public class HashSet_Impl { public static void main (String[] args) { HashSet<String> hashSet = new HashSet <>(); hashSet.add("亚索" ); hashSet.add("哥哥" ); hashSet.add("你好浪" ); hashSet.add("亚索" ); hashSet.add("哥哥" ); System.out.println("================" ); System.out.println(hashSet.add("亚索" )); Iterator<String> itr = hashSet.iterator(); while (itr.hasNext()){ System.out.println(itr.next()); } System.out.println("=======" ); for (String name : hashSet) { System.out.println(name); } } }
源码分析
案例练习
package 基础知识.集合.Collection.Set.Set子集合.HashSet.案例练习;import java.util.HashSet;import java.util.Iterator;public class HashSet_Stu { public static void main (String[] args) { HashSet<Student> hashSet = new HashSet <Student>(); Student stu1 = new Student ("尴尬" ,18 ); Student stu2 = new Student ("尴尬酱" ,19 ); Student stu3 = new Student ("尴尬帝" ,20 ); Student stu4 = new Student ("尴尬" ,18 ); hashSet.add(stu1); hashSet.add(stu2); hashSet.add(stu3); hashSet.add(stu4); for (Student stu: hashSet){ System.out.println("我叫:" +stu.getName()+",年龄:" +stu.getAge()); } System.out.println("==============================================" ); Iterator<Student> ite = hashSet.iterator(); while (ite.hasNext()){ Student stu = ite.next(); System.out.println("我叫:" +stu.getName()+",年龄:" +stu.getAge()); } } }
package 基础知识.集合.Collection.Set.Set子集合.HashSet.案例练习;public class Student { private String name; private int age; public Student () { } public Student (String name, int age) { this .name = name; this .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; } @Override public boolean equals (Object o) { if (this == o) return true ; if (o == null || getClass() != o.getClass()) return false ; Student student = (Student) o; if (age != student.age) return false ; return name != null ? name.equals(student.name) : student.name == null ; } @Override public int hashCode () { int result = name != null ? name.hashCode() : 0 ; result = 31 * result + age; return result; } }
📌LinkedHashSet LinkedHashSet集合概述和特点
哈希表和链表实现的St接口,具有可预测的迭代次序
由链表保证元素有序,也就是说元素的存储和取出顺序是一致的
由哈希表保证元素唯一,也就是说没有重复的元素
代码
package 基础知识.集合.Collection.Set.Set子集合.LinkedHashSet;import java.util.LinkedHashSet;public class LinkedHashSet_Lei { public static void main (String[] args) { LinkedHashSet<String> linkedHashSet = new LinkedHashSet <String>(); linkedHashSet.add("尴尬" ); linkedHashSet.add("问君能有几多愁" ); linkedHashSet.add("恰似一江春水向东流" ); linkedHashSet.add("尴尬" ); for (String str : linkedHashSet) { System.out.println(str); } } }
📌TreeSet TreeSet集合概述和特点
元素有序,这里的顺序不是指存储和取出的顺序,而是按照一定的规则进行排序,具体排序方式取决于构造方法
TreeSet():根据其元素的自然排序进行排序
TreeSet(Comparator comparator):根据指定的比较器进行排序
没有带索引的方法,所以不能使用普通for循环遍历
由于是Set集合,所以不包含重复元素的集合
代码
package 基础知识.集合.Collection.Set.Set子集合.TreeSet;import java.util.TreeSet;public class TreeSet_Lei { public static void main (String[] args) { TreeSet<Integer> treeSet = new TreeSet <Integer>(); treeSet.add(30 ); treeSet.add(50 ); treeSet.add(10 ); treeSet.add(20 ); treeSet.add(40 ); treeSet.add(30 ); treeSet.add(40 ); for (int num : treeSet) { System.out.println(num); } } }
📌TreeSet && 比较器类 自然排序 结论:
用TreeSet集合存储自定义对象,无参构造方法使用的是自然排序对元素进行排序的
自然排序,就是让元素所属的类实现Comparable接口,重写compareTo(To)方法
重写方法时,一定要注意排序规则必须按照要求的庄要条件和次要条件来写
代码
package 基础知识.集合.Collection.Set.Set子集合.TreeSet.排序类.Comparable;import java.util.TreeSet;public class TreeSet_Lei { public static void main (String[] args) { try { Student_1 stu1 = new Student_1 ("GanGa" , 18 ); Student_1 stu2 = new Student_1 ("GanGaJiang" , 9 ); Student_1 stu3 = new Student_1 ("GanGaDi" , 20 ); Student_1 stu4 = new Student_1 ("GanGaLe" , 120 ); TreeSet<Student_1> tree = new TreeSet <>(); tree.add(stu1); tree.add(stu2); tree.add(stu3); tree.add(stu4); for (Student_1 stu : tree) { System.out.println(stu.getName() + "," + stu.getAge()); } }catch (ClassCastException e){ System.out.println("ClassCastException 报错" ); } System.out.println("===========================================================================================" ); Student_2 stu1 = new Student_2 ("GanGa" , 18 ); Student_2 stu2 = new Student_2 ("GanGaJiang" , 9 ); Student_2 stu3 = new Student_2 ("GanGaDi" , 20 ); Student_2 stu4 = new Student_2 ("GanGaLe" , 120 ); TreeSet<Student_2> tree = new TreeSet <>(); tree.add(stu1); tree.add(stu2); tree.add(stu3); tree.add(stu4); for (Student_2 stu : tree) { System.out.println(stu.getName() + "," + stu.getAge()); } System.out.println("==============================" ); Student_3 stu01 = new Student_3 ("GanGa" , 18 ); Student_3 stu02 = new Student_3 ("GanGaJiang" , 9 ); Student_3 stu03 = new Student_3 ("GanGaDi" , 20 ); Student_3 stu04 = new Student_3 ("GanGaLe" , 120 ); Student_3 stu05 = new Student_3 ("GanGaBaoBao" ,9 ); Student_3 stu06 = new Student_3 ("GanGaBaoBao" ,9 ); TreeSet<Student_3> treeSet = new TreeSet <>(); treeSet.add(stu01); treeSet.add(stu02); treeSet.add(stu03); treeSet.add(stu04); treeSet.add(stu05); treeSet.add(stu06); for (Student_3 stu : treeSet) { System.out.println(stu.getName() + "," + stu.getAge()); } System.out.println("==================================================================================" ); System.out.println("==================================================================================" ); TreeSet<Student_回顾复习> treeSet00 = new TreeSet <>(); Student_回顾复习 student1 = new Student_ 回顾复习("GanGa" , 18 ); Student_回顾复习 student2 = new Student_ 回顾复习("GanGaJiang" , 9 ); Student_回顾复习 student3 = new Student_ 回顾复习("GanGaDi" , 20 ); Student_回顾复习 student4 = new Student_ 回顾复习("GanGaLe" , 120 ); Student_回顾复习 student5 = new Student_ 回顾复习("GanGaBaoBao" ,9 ); Student_回顾复习 student6 = new Student_ 回顾复习("GanGaBaoBao" ,9 ); treeSet00.add(student1); treeSet00.add(student2); treeSet00.add(student3); treeSet00.add(student4); treeSet00.add(student5); treeSet00.add(student6); for (Student_回顾复习 stu : treeSet00) { System.out.println(stu.getName() + "," + stu.getAge()); } System.out.println("=======================================" ); } }
Student_1
public class Student_1 { private String name; private int age; public Student_1 (String name, int age) { this .name = name; this .age = age; } public Student_1 () { } 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; } }
Student_2
package 基础知识.集合.Collection.Set.Set子集合.TreeSet.排序类.Comparable;public class Student_2 implements Comparable <Student_2>{ private String name; private int age; public Student_2 (String name, int age) { this .name = name; this .age = age; } public Student_2 () { } 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; } @Override public int compareTo (Student_2 o) { return this .age - o.age; } }
Student_3
package 基础知识.集合.Collection.Set.Set子集合.TreeSet.排序类.Comparable;public class Student_3 implements Comparable <Student_3>{ private String name; private int age; public Student_3 (String name, int age) { this .name = name; this .age = age; } public Student_3 () { } 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; } @Override public int compareTo (Student_3 o) { int num = this .age - o.age; int numL = num == 0 ? this .name.compareTo(o.name) : num; return numL; } }
Student_回顾复习
package 基础知识.集合.Collection.Set.Set子集合.TreeSet.排序类.Comparable;public 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; } 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; } @Override public int compareTo (Student_回顾复习 o) { int num = this .age - o.age; int i = num == 0 ? this .name.compareTo(o.name) : num; return i; } }
比较器排序
代码一
package 基础知识.集合.Collection.Set.Set子集合.TreeSet.排序类.Comparator;import java.util.Comparator;import java.util.TreeSet;public class TreeSet_Lei { public static void main (String[] args) { TreeSet<Student> treeSet = new TreeSet <>(new Comparator <Student>() { @Override public int compare (Student o1, Student o2) { int num = o1.getAge() - o1.getAge(); int num1 = num == 0 ? o1.getName().compareTo(o2.getName()) : num; return num1; } }); Student stu1 = new Student ("GanGa" , 18 ); Student stu2 = new Student ("GanGaJiang" , 9 ); Student stu3 = new Student ("GanGaDi" , 20 ); Student stu4 = new Student ("GanGaLe" , 120 ); Student stu5 = new Student ("GanGaBaoBao" , 9 ); Student stu6 = new Student ("GanGaBaoBao" , 9 ); treeSet.add(stu1); treeSet.add(stu2); treeSet.add(stu3); treeSet.add(stu4); treeSet.add(stu5); treeSet.add(stu6); for (Student stu : treeSet) { System.out.println(stu.getName() + "," + stu.getAge()); } } }
代码二
package 基础知识.集合.Collection.Set.Set子集合.TreeSet.排序类.Comparator;public class Student { private String name; private int age; public Student (String name, int age) { this .name = name; this .age = age; } public Student () { } 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; } }
案例复习 要求:
代码一
package 基础知识.集合.Collection.Set.Set子集合.TreeSet.排序类.案例;import java.util.Comparator;import java.util.TreeSet;public class TreeSet_Lei { public static void main (String[] args) { TreeSet<Student> treeSet = new TreeSet <Student>(new Comparator <Student>() { @Override public int compare (Student s1, Student s2) { int num1 = (s2.getChinese() + s2.getMath()) - (s1.getChinese() + s1.getChinese()); int num2 = num1 == 0 ? s2.getChinese() - s1.getChinese() : num1; int num = num2 == 0 ? s1.getName().compareTo(s2.getName()) : num2; return num; } }); Student stu1 = new Student ("GanGa" , 100 , 120 ); Student stu2 = new Student ("GanGaJiang" , 150 , 150 ); Student stu3 = new Student ("GanGaDi" , 120 , 120 ); Student stu4 = new Student ("GanGaBaoBao" , 1 , 0 ); Student stu5 = new Student ("GaGaLe" , 150 , 150 ); Student stu6 = new Student ("GanGaWWW" , 120 , 100 ); Student stu7 = new Student ("GanGaBaoB" , 0 , 1 ); Student stu8 = new Student ("GanGaLou" ,100 ,120 ); treeSet.add(stu1); treeSet.add(stu2); treeSet.add(stu3); treeSet.add(stu4); treeSet.add(stu5); treeSet.add(stu6); treeSet.add(stu7); treeSet.add(stu8); for (Student stu : treeSet) { System.out.println(stu.getName()); System.out.println("语文:" + stu.getChinese() + ",数学:" + stu.getMath() + "总分为:" + (stu.getChinese() + stu.getMath())); System.out.println(); } } }
代码二
package 基础知识.集合.Collection.Set.Set子集合.TreeSet.排序类.案例;public class Student { private String name; private int chinese; private int math; public Student () { } public Student (String name, int chinese, int math) { this .name = name; this .chinese = chinese; this .math = math; } public String getName () { return name; } public void setName (String name) { this .name = name; } public int getChinese () { return chinese; } public void setChinese (int chinese) { this .chinese = chinese; } public int getMath () { return math; } public void setMath (int math) { this .math = math; } }
Collection工具类 Collections概述
Collections类的常用方法
public static<T extends Comparable<?super T>>void sort(ist<T> list)
:将指定的列表按升序排序
public static void reverse(List<?>list)
:反转指定列表中元素的顺序
public static void shuffle(List<?> list)
:使用默认的随机源随机排列指定的列表
代码
package 基础知识.集合.Collections工具类;import java.util.ArrayList;import java.util.Collections;import java.util.List;public class TestCollection { public static void main (String[] args) { new TestCollection ().init(); } public void init () { System.out.println("===============================================================" ); List<String> list = new ArrayList <>(); list.add("3问君能有几多愁" ); list.add("4剑圣塔下达不溜" ); list.add("1风萧萧兮易水寒" ); list.add("2壮士一去兮不复还" ); Collections.sort(list); System.out.println(list); System.out.println("===============================================================" ); Collections.reverse(list); System.out.println(list); System.out.println("===============================================================" ); Collections.shuffle(list); System.out.println(list); System.out.println("===============================================================" ); List<String> listE; Collections.shuffle(list); System.out.println(list); } }
CollectionToComparator
代码
package 基础知识.集合.Collections工具类.CollectionToComparator;import java.util.*;public class TestCollectionToComparator { public static void main (String[] args) { new TestCollectionToComparator ().init(); } public void init () { Student stu1 = new Student ("尴尬酱" , 18 ); Student stu2 = new Student ("尴尬帝" , 20 ); Student stu3 = new Student ("尴尬了" , 11 ); Student stu4 = new Student ("这就尴" , 20 ); Student stu5 = new Student ("尴某某" , 21 ); ArrayList<Student> list = new ArrayList <>(); list.add(stu1); list.add(stu2); list.add(stu3); list.add(stu4); list.add(stu5); Collections.sort(list, new Comparator <Student>() { @Override public int compare (Student o1, Student o2) { int i = o1.getAge() - o2.getAge(); int num = i == 0 ? o1.getName().compareTo(o2.getName()) : i; return num; } }); Iterator<Student> itr = list.iterator(); while (itr.hasNext()){ Student stu = itr.next(); System.out.println("姓名: " +stu.getName()+", 年龄: " +stu.getAge()); } } }
package 基础知识.集合.Collections工具类.CollectionToComparator;public class Student { private String name; private int age; public Student () { } public Student (String name, int age) { this .name = name; this .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; } }
🌸Map集合体系 Map集合概述与使用 Map集合概述
Interface Map<K,V>
K:键的类型;V:值的类型
将键映射到值的对象;不能包含重复的键;每个键可以映射到最多一个值
举例:学生的学号和姓名
itheima001 林青霞
itheima002 张曼玉
itheima003 王祖贤
创建Map集合的对象
代码
package 基础知识.集合.Map;import java.util.HashMap;import java.util.Map;public class MapLei { public static void main (String[] args) { Map<String, String> map = new HashMap <String, String>(); map.put("吟留的诗人" ,"温迪" ); map.put("天动万象" ,"钟离" ); map.put("无想的一刀" ,"巴尔" ); map.put("天动万象" ,"摩拉克斯" ); System.out.println(map); } }
Map集合方法
代码
package 基础知识.集合.Map;import java.util.HashMap;import java.util.Map;public class MapMethod { public static void main (String[] args) { Map<String, String> map = new HashMap <>(); map.put("吟留的诗人" ,"温迪" ); map.put("天动万象" ,"钟离" ); map.put("无想的一刀" ,"巴尔" ); map.put("亡" ,"草神" ); System.out.println(map); map.remove("亡" ); System.out.println(map.remove("亡" )); map.clear(); System.out.println(map); map.put("吟留的诗人" ,"温迪" ); map.put("天动万象" ,"钟离" ); map.put("无想的一刀" ,"巴尔" ); System.out.println("重新添加回来:" + map); System.out.println(map.containsKey("无想的一刀" )); System.out.println(map.containsKey("亡" )); System.out.println(map.containsValue("巴尔" )); System.out.println(map.containsValue("草神" )); System.out.println("键值队个数:" + map.size()); System.out.println(map.isEmpty()); map.clear(); System.out.println("clear后:" + map.isEmpty()); } }
Map集合获取方法
代码
package 基础知识.集合.Map;import java.util.Collection;import java.util.HashMap;import java.util.Map;import java.util.Set;public class MapGetMethod { public static void main (String[] args) { Map<String, String> map = new HashMap <>(); map.put("吟留的诗人" ,"温迪" ); map.put("天动万象" ,"钟离" ); map.put("无想的一刀" ,"巴尔" ); map.put("亡" ,"草神" ); System.out.println("==================================" ); System.out.println(map.get("无想的一刀" )); System.out.println(map.get("天动万象" )); System.out.println(map.get("神罗天征" )); System.out.println("==================================" ); System.out.println(map.keySet()); Set<String> setMap = map.keySet(); System.out.println("键集合遍历:" ); for (String str : setMap) { System.out.println(str); } System.out.println("==================================" ); Collection<String> values = map.values(); System.out.println("值集合便利:" ); for (String str : values) { System.out.println(str); } System.out.println("==================================" ); } }
Map集合遍历 方式一 package 基础知识.集合.Map.Map遍历;import java.util.HashMap;import java.util.Map;import java.util.Set;public class Map01 { public static void main (String[] args) { Map<String, Integer> map = new HashMap <>(); map.put("尴尬酱" , 1 ); map.put("尴尬了" , 2 ); map.put("尴尬帝" , 3 ); map.put("真尴尬" , 4 ); map.put("贼尴尬" , 5 ); Set<String> key = map.keySet(); for (String strKey: key){ Integer value = map.get(strKey); System.out.println(strKey+"=" +value); } } }
方式二 package 基础知识.集合.Map.Map遍历;import java.util.HashMap;import java.util.Map;import java.util.Set;public class Map02 { public static void main (String[] args) { Map<String, Integer> map = new HashMap <>(); map.put("尴尬酱" , 1 ); map.put("尴尬了" , 2 ); map.put("尴尬帝" , 3 ); map.put("真尴尬" , 4 ); map.put("贼尴尬" , 5 ); Set<Map.Entry<String, Integer>> entry = map.entrySet(); for ( Map.Entry<String, Integer> me : entry) { String key = me.getKey(); Integer value = me.getValue(); System.out.println(key + "=" + value); } } }
Map集合练习 练习一 要求
代码
package 基础知识.集合.Map.Map练习.Map练习1 ;import java.util.HashMap;import java.util.Map;import java.util.Set;public class DameMap { public static void main (String[] args) { HashMap<String, Student> map = new HashMap <>(); Student stu1 = new Student ("尴尬酱" , 18 ); Student stu2 = new Student ("尴尬帝" , 20 ); Student stu3 = new Student ("尴尬了" , 11 ); map.put("20202218" , stu1); map.put("20202220" , stu2); map.put("20202211" , stu3); Set<String> keySet = map.keySet(); for (String id : keySet) { Student s1 = map.get(id); System.out.println("学号:" + id + ", 姓名:" + s1.getName() + ", 年龄:" + s1.getAge()); } System.out.println("====================================================================" ); Set<Map.Entry<String,Student>> entry = map.entrySet(); for (Map.Entry<String,Student> em: entry){ String key = em.getKey(); Student value = em.getValue(); System.out.println("学号:" + key + ", 姓名:" + value.getName() + ", 年龄:" + value.getAge()); } } }
package 基础知识.集合.Map.Map练习.Map练习1 ;public class Student { private String name; private int age; public Student () { } public Student (String name, int age) { this .name = name; this .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; } } ```` ---- ### 练习二 要求 ![img](https: 代码 ```java package 基础知识.集合.Map.Map练习.Map练习2 ;import java.util.HashMap;import java.util.Map;import java.util.Set;public class DameHashMap { public static void main (String[] args) { new DameHashMap ().init(); } public void init () { System.out.println("===============================================" ); HashMap<Student, String> hashMap = new HashMap <>(); Student stu1 = new Student ("尴尬酱" , 18 ); Student stu2 = new Student ("尴尬帝" , 20 ); Student stu3 = new Student ("尴尬了" , 11 ); Student stu4 = new Student ("尴尬了" , 11 ); hashMap.put(stu1, "20202218" ); hashMap.put(stu2, "20202220" ); hashMap.put(stu3, "20202211" ); hashMap.put(stu4, "20202211" ); System.out.println(hashMap); Set<Student> keyId = hashMap.keySet(); for (Student stu : keyId) { String id = hashMap.get(stu); System.out.println("姓名为: " + stu.getName() + ", 年龄: " + stu.getAge() + ", 学号: " + id); } System.out.println("===============================================" ); Set<Map.Entry<Student, String>> hashEm = hashMap.entrySet(); for (Map.Entry<Student, String> stuId : hashEm) { Student key = stuId.getKey(); String value = stuId.getValue(); System.out.println("姓名为: " + key.getName() + ", 年龄: " + key.getAge() + ", 学号: " + value); } } }
package 基础知识.集合.Map.Map练习.Map练习2 ;import java.util.Objects;public class Student { private String name; private int age; public Student () { } public Student (String name, int age) { this .name = name; this .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; } @Override public boolean equals (Object o) { if (this == o) return true ; if (o == null || getClass() != o.getClass()) return false ; Student that = (Student) o; return age == that.age && Objects.equals(name, that.name); } @Override public int hashCode () { return Objects.hash(name, age); } }
练习三 要求
代码
package 基础知识.集合.Map.Map练习.Map练习3 ;import java.util.ArrayList;import java.util.HashMap;import java.util.Map;import java.util.Set;public class DameHashMap { public static void main (String[] args) { new DameHashMap ().init(); } public void init () { System.out.println("=========================" ); ArrayList<HashMap<String, String>> arrayMap = new ArrayList <>(); HashMap<String, String> map1 = new HashMap <>(); map1.put("孙策" , "大桥" ); map1.put("周瑜" , "小桥" ); HashMap<String, String> map2 = new HashMap <>(); map1.put("郭靖" , "黄蓉" ); map1.put("杨过" , "小龙女" ); HashMap<String, String> map3 = new HashMap <>(); map1.put("亚索" , "瑞文" ); map1.put("艾希" , "蛮王" ); arrayMap.add(map1); arrayMap.add(map2); arrayMap.add(map3); for (HashMap<String, String> mapH1 : arrayMap) { Set<String> keySet = mapH1.keySet(); for (String keyL : keySet) { String valueL = mapH1.get(keyL); System.out.println(keyL + " 喜欢 " + valueL); System.out.println("-------------" ); } } System.out.println("=========================" ); for (HashMap<String,String> mapH2: arrayMap) { Set<Map.Entry<String, String>> em = mapH2.entrySet(); for (Map.Entry<String, String> keyToValue :em) { String key = keyToValue.getKey(); String value = keyToValue.getValue(); System.out.println(key + " 喜欢 " + value); System.out.println("-------------" ); } } } }
练习四 要求
代码
package 基础知识.集合.Map.Map练习.Map练习4 ;import java.util.ArrayList;import java.util.HashMap;import java.util.Map;import java.util.Set;public class DameHashMap { public static void main (String[] args) { new DameHashMap ().init(); } public void init () { System.out.println("==============================" ); HashMap<String, ArrayList<String>> map = new HashMap <>(); ArrayList<String> list1 = new ArrayList <>(); list1.add("郭靖" ); list1.add("黄蓉" ); ArrayList<String> list2 = new ArrayList <>(); list2.add("亚索" ); list2.add("瑞文" ); ArrayList<String> list3 = new ArrayList <>(); list3.add("岩神" ); list3.add("雷神" ); map.put("相爱" ,list1); map.put("想杀" ,list2); map.put("故人" ,list3); Set<String> keySet = map.keySet(); for (String keyL :keySet) { ArrayList<String> valueL = map.get(keyL); for (String str :valueL) { System.out.println("===" +keyL+"===" ); System.out.println(str); } System.out.println("===============" ); } System.out.println("==============================" ); Set<Map.Entry<String, ArrayList<String>>> em = map.entrySet(); for (Map.Entry<String, ArrayList<String>> keyToValue :em) { String key = keyToValue.getKey(); ArrayList<String> value = keyToValue.getValue(); for (String str :value) { System.out.println("===" +key+"===" ); System.out.println(str); } System.out.println("===============" ); } } }
练习五 要求
代码
package 基础知识.集合.Map.Map练习.Map练习5 ;import java.util.HashMap;import java.util.Map;import java.util.Scanner;import java.util.Set;public class DameHashMap { public static void main (String[] args) { new DameHashMap ().init(); } public void init () { System.out.println("请输入:" ); Scanner sc = new Scanner (System.in); String str = sc.nextLine(); HashMap<Character, Integer> map = new HashMap <>(); for (int i = 0 ; i<str.length(); i++){ char key = str.charAt(i); Integer value = map.get(key); if (value == null ){ map.put(key,1 ); }else { map.put(key,map.get(key)+1 ); } } Set<Character> keySet = map.keySet(); for (Character keyL :keySet) { Integer value = map.get(keyL); System.out.print(new StringBuilder ().append(keyL).append("(" ).append(value).append(")" )); } System.out.println(); System.out.println("=====================================================" ); Set<Map.Entry<Character, Integer>> em = map.entrySet(); for (Map.Entry<Character, Integer> keyToValue :em) { Character key = keyToValue.getKey(); Integer value = keyToValue.getValue(); System.out.print(new StringBuilder ().append(key).append("(" ).append(value).append(")" )); } } }
🌸常见的数据类型
数据结构之【栈】
在内存进出两个过程:
[先进后出的模型]
数据进入栈模型的过程为: 【压/进 栈】
数据离开栈模型的过程为: 【弹/出 栈】
**在进栈的时候,数据先进入栈的底部,栈底元素——>栈顶元素
** 在出栈的时候,数据先从栈最上部出,栈顶元素——>栈底元素
数据结构之【队列】
**队列与栈恰好相反: **
[先进先出的模型]
数据从后端进入队列模型的过程称为: 【入队列】
数据从后端离开队列模型的过程称为: 【出队列】
**进队列与栈相同 ** 出队列的时候是,前端
(下部)先出
数据结构之【数组】
优点
查询数据通过索引定位,查询任意数据耗时相同,查询效率高
缺点
**删除数据时,要将原始数据删除,同时后面毎个数据前移,删除效率低
** 添加数据时,添加位置后的每个数据后移,再添加元素,添加效率极低
由此可见 数组是一种查询快,增删慢的模型
数据结构之【链表】
优点
链表是一种增删快
的模型(对比数组)
缺点
链表是一种查询慢
的模型(对比数组)
数据类型之【哈希表】
JDK8之前,底层采用 [数组] + [链表] 实现,可以说是一个元素为链表的数组。 JDK8以后,在长度比较长的时候,底层实现了优化。
🌸集合案例
斗地主 package 基础知识.集合.集合案例;import java.util.ArrayList;import java.util.Collections;import java.util.HashMap;public class DouDiZu { private String userName1 = "尴尬了" ; private String userName2 = "尴尬帝" ; private String userName3 = "尴尬酱" ; public static void main (String[] args) { new DouDiZu ().init(); } public void init () { ArrayList<String> list = new ArrayList <>(); String[] colors = {"方块" , "红桃" , "黑桃" , "红桃" }; String[] nums = {"2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" , "J" , "Q" , "K" , "A" }; for (String color : colors) { for (String num : nums) { list.add(color + num); } } list.add("小王" ); list.add("大王" ); Collections.shuffle(list); ArrayList<String> user1 = new ArrayList <>(); ArrayList<String> user2 = new ArrayList <>(); ArrayList<String> user3 = new ArrayList <>(); ArrayList<String> dp = new ArrayList <>(); for (int i = 0 ; i < list.size(); i++) { String str = list.get(i); if (i >= list.size() - 3 ) { dp.add(str); } else if (i % 3 == 0 ) { user1.add(str); } else if (i % 3 == 1 ) { user2.add(str); } else if (i % 3 == 2 ) { user3.add(str); } } HashMap<String, Integer> map = new HashMap <>(); Value(userName1, user1); Value(userName2, user2); Value(userName3, user3); Value("底牌数" , dp); } public static void sort (String userName,ArrayList<String> list) { HashMap<String, String> map = new HashMap <>(); } public static void Value (String name, ArrayList<String> array) { System.out.println("姓名:" + name); System.out.print("牌为:" ); for (String str : array) { System.out.print(str + " " ); } System.out.println("\n======================================================================================" ); } }
New斗地主 package 基础知识.集合.集合案例;import java.util.ArrayList;import java.util.Collections;import java.util.HashMap;import java.util.TreeSet;public class DouDiZuNew { private String userName1 = "尴尬了" ; private String userName2 = "尴尬帝" ; private String userName3 = "尴尬酱" ; private String[] colorOf = {"方块" , "红桃" , "黑桃" , "红桃" }; private String[] numOf = {"2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" , "J" , "Q" , "K" , "A" }; private TreeSet<Integer> user1 = new TreeSet <>(); private TreeSet<Integer> user2 = new TreeSet <>(); private TreeSet<Integer> user3 = new TreeSet <>(); private TreeSet<Integer> hands = new TreeSet <>(); public static void main (String[] args) { new DouDiZuNew ().init(); } public void init () { HashMap<Integer, String> map = new HashMap <>(); ArrayList<Integer> list = new ArrayList <>(); this .hashMapAdd(map, list); this .shuffle(list); this .distribute(list, map); System.out.println(list); look(userName1, user1, map); look(userName2, user2, map); look(userName3, user3, map); } public void hashMapAdd (HashMap<Integer, String> map, ArrayList<Integer> list) { int index = 0 ; for (String num : numOf) { for (String color : colorOf) { map.put(index, color + num); list.add(index); index++; } } map.put(index, "小王" ); list.add(index); index++; map.put(index, "大王" ); list.add(index); } public void shuffle (ArrayList<Integer> list) { Collections.shuffle(list); } public void distribute (ArrayList<Integer> list, HashMap<Integer, String> map) { for (int i = 0 ; i < list.size(); i++) { if (i >= 54 ) { hands.add(list.get(i)); } else if (i % 3 == 0 ) { user1.add(list.get(i)); } else if (i % 3 == 1 ) { user2.add(list.get(i)); } else if (i % 3 == 2 ) { user3.add(list.get(i)); } } } public void look (String name, TreeSet<Integer> set, HashMap<Integer, String> map) { System.out.println("姓名:" + name); System.out.print("牌数为:" ); int in = 1 ; for (Integer index : set) { if (in < set.size()) { System.out.print(map.get(index) + " , " ); } else { System.out.print(map.get(index + "。" )); } in++; } System.out.println(); System.out.println("=====================================================================" ); } }
🌸单词复习
单词/引用
翻译/解释
Collection
remove
删除
clear
清除
contains
存在
isEmpty
is为空
Iterator
迭代器
hasNext
下一个
LinkedList
First
开头
Last
末尾
**Compatable **
自然排序
**Comparator **
比较器排序
**compare **
比较
Map
**地图 **
**Key **
**键 **
Value
值
put
**添加元素 **
**entrySet **
**便利使用 **