集合框架的概述

1.集合、数组都是对多个数据进行存储操作的结构,简称Java容器
说明:此时的存储,主要指的是内存层面的存储,不涉及到持久化的存储(.txt,.jpg,.avi,数据库中)
2.数组在存储多个数据方面的特点
>一旦初始化以后,其长度就确定了
>数组一旦定义好,其元素的类型也就确定了。我们也就只能操作指定类型的数据了
比如:String[] arr;int[] arr1;Object[] arrr2
3.数组存储多个数据方面的缺点
>一旦初始化以后,其长度就不可修改
>数组中提供的方法非常有限,对于添加、删除、插入数据等操作,非常不便,同时效率不高
>获取数组中实际元素的个数的需求,数组没有现成的属性或方法可用
>数组存储数据的特点:有序、可重复。对于无序、不可重复的需求,不能满足
屏幕截图 2022-03-29 152243.jpg

集合框架

Collection接口:单列集合,用来存储一个一个的对象
List接口:存储有序的、可重复的数据 —- >”动态”数组
ArrayList、LinkedList、Vector
set接口:存储无序的、不可重复的数据 —->高中讲的”集合”
HashSet、LinkedHashSet、TreeSet
Map接口:双列集合,用来存储一对(key - value)一对的数据 —>高中函数
HashMap、LinkedHashMap/TreeMap/Hashable/Properties
屏幕截图 2022-03-29 152633.jpg
屏幕截图 2022-03-29 152716.jpg
屏幕截图 2022-03-29 152841.jpg

Collection接口中常用方法

  1. public void test1(){
  2. Collection coll = new ArrayList();
  3. //add(Object e):将元素e添加到集合coll中
  4. coll.add("AA");
  5. coll.add("BB");
  6. coll.add(123);//自动装箱
  7. coll.add(new Date());
  8. //size():获取添加的元素的个数
  9. System.out.println(coll.size());//4
  10. //addAll(Collection coll1):将coll1集合中的元素添加到当前的集合中
  11. Collection coll1 = new ArrayList();
  12. coll1.add(456);
  13. coll1.add("CC");
  14. coll.addAll(coll1);
  15. System.out.println(coll.size());//6
  16. //clear():清空集合元素
  17. coll.clear();
  18. System.out.println(coll);//[]
  19. //isEmpty():判断当前集合是否为空
  20. System.out.println(coll.isEmpty());//true
  21. }
  1. import org.junit.Test;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.Collection;
  5. import java.util.List;
  6. public class CollectionTest1 {
  7. @Test
  8. public void test1(){
  9. Collection coll = new ArrayList();
  10. coll.add(123);
  11. coll.add(456);
  12. coll.add("AA");
  13. coll.add(new String("Tom"));
  14. coll.add(false);
  15. coll.add(new People("jerry", 19));
  16. //1.contains(Object obj):判断当前集合中是否包含obj
  17. //我们在判断时会调用obj对象所在类的equals()
  18. boolean contains = coll.contains(123);
  19. System.out.println(contains);//true
  20. System.out.println(coll.contains(new String("Tom")));//true
  21. System.out.println(coll.contains(new People()));//true
  22. //2.containsAll(Collection coll1):判断形参从coll1中的所有元素是否都存在于当前集合中
  23. Collection coll1 = Arrays.asList(123,456);
  24. System.out.println(coll.containsAll(coll1));//true
  25. }
  26. @Test
  27. public void test2(){
  28. //3.remove(Object obj):从当前集合中移除obj元素
  29. Collection coll = new ArrayList();
  30. coll.add(123);
  31. coll.add(456);
  32. coll.add("AA");
  33. coll.add(new String("Tom"));
  34. coll.add(false);
  35. coll.add(new People("jerry", 19));
  36. coll.remove(1234);
  37. //System.out.println(coll);
  38. coll.remove(new People("jerry", 19));
  39. System.out.println(coll);
  40. //4.removeAll(Collection coll1):从当前集合中移除coll1中所有的元素
  41. Collection coll1 = Arrays.asList(123,456);
  42. coll.removeAll(coll1);
  43. System.out.println(coll);
  44. }
  45. @Test
  46. public void test3(){
  47. Collection coll = new ArrayList();
  48. coll.add(123);
  49. coll.add(456);
  50. coll.add("AA");
  51. coll.add(new String("Tom"));
  52. coll.add(false);
  53. coll.add(new People("jerry", 19));
  54. //5.retainAll(Collection coll1):交集:获取当前集合和coll1集合的交集,并返回给当前集合
  55. //Collection coll1 = Arrays.asList(123,4567);
  56. //coll.retainAll(coll1);
  57. //System.out.println(coll);
  58. //6.equals(Object obj):
  59. Collection coll1 = new ArrayList();
  60. coll1.add(123);
  61. coll1.add(456);
  62. coll1.add("AA");
  63. coll1.add(new String("Tom"));
  64. coll1.add(false);
  65. coll1.add(new People("jerry", 19));
  66. System.out.println(coll.equals(coll1));//true
  67. }
  68. @Test
  69. public void test4(){
  70. Collection coll = new ArrayList();
  71. coll.add(123);
  72. coll.add(456);
  73. coll.add("AA");
  74. coll.add(new String("Tom"));
  75. coll.add(false);
  76. coll.add(new People("jerry", 19));
  77. //7.hashCode():返回当前对象的哈希值
  78. System.out.println(coll.hashCode());
  79. //8.集合--->数组:toArray():
  80. Object[] array = coll.toArray();
  81. for (int i = 0;i<array.length;i++){
  82. System.out.println(array[i]);
  83. }
  84. //拓展:数组--->集合:调用Arrays类的静态方法asList()
  85. List<String> strings = Arrays.asList(new String[]{"aa", "bb", "cc"});
  86. System.out.println(strings);
  87. //iterator():返回Iterator接口的实例,用于遍历集合元素
  88. }
  89. }

结论:向Collection接口的实现类的对象中添加数据obj时,要求obj所在类要重写equals()

集合元素的遍历

使用迭代器Iterator接口
1.内部方法:hasNext() 和 next()
2.集合对象每次调用iterator()方法都得到一个全新的迭代器对象,默认游标都在集合的第一个元素之前
3.内部定义了rename(),可以在遍历的时候,删除集合中的元素,此方法不同于集合直接调用remove()
注:如果还未调用next()或在上一次调用next方法之后已经调用了remove方法,再调用remove都会报IllegalStateException

  1. import org.junit.Test;
  2. import java.util.ArrayList;
  3. import java.util.Collection;
  4. import java.util.Iterator;
  5. public class IteratorTest {
  6. @Test
  7. public void test(){
  8. Collection coll = new ArrayList();
  9. coll.add(123);
  10. coll.add(456);
  11. coll.add(new String("Tom"));
  12. coll.add(false);
  13. coll.add(new People("jerry", 19));
  14. Iterator iterator = coll.iterator();
  15. //方式一
  16. System.out.println(iterator.next());//123
  17. System.out.println(iterator.next());//456
  18. System.out.println(iterator.next());//Tom
  19. System.out.println(iterator.next());//false
  20. System.out.println(iterator.next());//People{name='jerry', age=19}
  21. //报异常:NoSuchElementException
  22. System.out.println(iterator.next());
  23. //方式二:不推荐
  24. for (int i = 0;i<coll.size();i++){
  25. System.out.println(iterator.next());
  26. }
  27. //方式三:推荐
  28. while(iterator.hasNext()){
  29. System.out.println(iterator.next());
  30. }
  31. }
  32. }

屏幕截图 2022-03-30 162059.jpg

remove方法

  1. public void test3(){
  2. Collection coll = new ArrayList();
  3. coll.add(123);
  4. coll.add(456);
  5. coll.add(new String("Tom"));
  6. coll.add(false);
  7. coll.add(new People("jerry", 19));
  8. //删除集合中的”Tom“
  9. Iterator iterator = coll.iterator();
  10. while (iterator.hasNext()){
  11. Object obj = iterator.next();
  12. if ("Tom".equals(obj)) {
  13. iterator.remove();
  14. }
  15. }
  16. //遍历集合
  17. iterator = coll.iterator();
  18. while (iterator.hasNext()){
  19. System.out.println(iterator.next());
  20. }
  21. }

增强for循环

for(数组元素的类型 局部变量 :数组对象){
}

  1. import org.junit.Test;
  2. import java.util.ArrayList;
  3. import java.util.Collection;
  4. public class ForTest {
  5. @Test
  6. public void test1(){
  7. Collection coll = new ArrayList();
  8. coll.add(123);
  9. coll.add(456);
  10. coll.add(new String("Tom"));
  11. coll.add(false);
  12. coll.add(new People("jerry", 19));
  13. for (Object obj: coll){
  14. System.out.println(obj);
  15. }
  16. }
  17. @Test
  18. public void test2(){
  19. int[] arr = new int[]{1,2,3,4,5};
  20. for (int i : arr){
  21. System.out.println(i);
  22. }
  23. }
  24. }

ArrayList、LinkedList、Vector三者的异同

相同:三个类都实现了List接口,存储数据的特点相同:存储有序的、可重复的数据
不同:ArrayList:作为List接口的主要实现类,线程不安全的,效率高,底层使用Object[] elementData存储
LinkedList:对于频繁的插入、删除操作,使用此类效率比ArrayList高,底层使用双向链表存储
Vector:作为List接口的古老实现类,线程安全的,效率低,底层使用Object[] elementData存储

List接口的常用方法

屏幕截图 2022-04-04 085458.jpg

  1. public class ListTest {
  2. @Test
  3. public void test1(){
  4. ArrayList list = new ArrayList();
  5. list.add(123);
  6. list.add(456);
  7. list.add("AA");
  8. list.add(new People("Tom",12));
  9. System.out.println(list);
  10. //void add(int index,Object ele)
  11. list.add(1,"BB");
  12. //boolean addAll(int index,Collection eles)
  13. List list1 = Arrays.asList(1,2,3);
  14. list.addAll(list1);
  15. System.out.println(list);
  16. }
  17. @Test
  18. public void test2(){
  19. ArrayList list = new ArrayList();
  20. list.add(123);
  21. list.add(456);
  22. list.add("AA");
  23. list.add(new People("Tom",12));
  24. //int indexOf(Object obj)
  25. int index = list.indexOf(4567);
  26. System.out.println(index);//-1
  27. //int lastIndexOf(Object obj)
  28. System.out.println(list.lastIndexOf(456));//1
  29. //Object remove(int index)
  30. Object obj = list.remove(0);
  31. System.out.println(obj);//123
  32. System.out.println(list);//[456, AA, People{name='Tom', age=12}]
  33. //Object set(int index,Object ele)
  34. list.set(1,"CC");
  35. System.out.println(list);//[456, CC, People{name='Tom', age=12}]
  36. //List subList(int formIndex,int toIndex)
  37. List list1 = list.subList(2, 3);
  38. System.out.println(list1);//[People{name='Tom', age=12}]
  39. System.out.println(list);//[456, CC, People{name='Tom', age=12}]
  40. }
  41. }

总结

增:add(int index,Object ele)
删:remove(int index)
改:set(int index,Object ele)
查:get(int index)
插:add(int index,Object ele)
长度:size()
遍历:

  1. public void test3(){
  2. ArrayList list = new ArrayList();
  3. list.add(123);
  4. list.add(456);
  5. list.add("AA");
  6. //方式一:Iterator迭代器方式
  7. Iterator iterator = list.iterator();
  8. while(iterator.hasNext()){
  9. System.out.println(iterator.next());
  10. }
  11. //方式二:增强for循环
  12. for (Object obj : list){
  13. System.out.println(obj);
  14. }
  15. //方式三:普通for循环
  16. for (int i = 0;i < list.size();i++){
  17. System.out.println(list.get(i));
  18. }
  19. }

Set接口

1.无序性
2.不可重复性
屏幕截图 2022-04-04 163214.jpg

TreeSet排序

1.向TreeSet中添加的数据,要求是相同类型的对象
2.两种排序方式:自然排序(实现Comparable接口)和定制排序(Comparator)
3.自然排序中,比较两个对象是否相同的标准为:compareTo()返回0,不再是equals()
4.定制排序中,比较两个对象是否相同的标准为:compareTo()返回0,不再是equals()

  1. public void test3() throws RuntimeException {
  2. Comparator com = new Comparator() {
  3. @Override
  4. public int compare(Object o1, Object o2) {
  5. if (o1 instanceof People && o2 instanceof People)
  6. People p3 = (People) o1;
  7. People p2 = (People) o2;
  8. return Integer.compare(u1.getAge(),u2.getAge());
  9. }else{
  10. throw new RuntimeException("输入的数据类型不匹配");
  11. }
  12. }
  13. }
  14. TreeSet set = new TreeSet(com);
  15. set.add(new People("Tom",12));
  16. set.add(new People("Jack",22));
  17. set.add(new People("Mike",52));
  18. set.add(new People("Timi",2));
  19. Iterator iterator = set.iterator();
  20. while (iterator.hasNext()){
  21. System.out.println(iterator.next());
  22. }

实例

  1. public class MyDate implements Comparable{
  2. private int year;
  3. private int month;
  4. private int day;
  5. public MyDate(){
  6. }
  7. public MyDate(int year, int month, int day) {
  8. this.year = year;
  9. this.month = month;
  10. this.day = day;
  11. }
  12. public int getYear() {
  13. return year;
  14. }
  15. public void setYear(int year) {
  16. this.year = year;
  17. }
  18. public int getMonth() {
  19. return month;
  20. }
  21. public void setMonth(int month) {
  22. this.month = month;
  23. }
  24. public int getDay() {
  25. return day;
  26. }
  27. public void setDay(int day) {
  28. this.day = day;
  29. }
  30. @Override
  31. public String toString() {
  32. return "MyDate{" +
  33. "year=" + year +
  34. ", month=" + month +
  35. ", day=" + day +
  36. '}';
  37. }
  38. //方式二:
  39. @Override
  40. public int compareTo(Object o) {
  41. if (o instanceof MyDate){
  42. MyDate m = (MyDate) o;
  43. //比较年
  44. int minusYear = this.getYear() - m.getYear();
  45. if (minusYear != 0){
  46. return minusYear;
  47. }
  48. //比较月
  49. int minusMonth = this.getMonth() - m.getMonth();
  50. if (minusMonth != 0){
  51. return minusMonth;
  52. }
  53. //比较日
  54. return this.getDay() - m.getDay();
  55. }
  56. throw new RuntimeException("传入的数据类型不一致");
  57. }
  58. }
  1. public class Employee implements Comparable{
  2. private String name;
  3. private int age;
  4. private MyDate birthday;
  5. public Employee() {
  6. }
  7. public Employee(String name, int age, MyDate birthday) {
  8. this.name = name;
  9. this.age = age;
  10. this.birthday = birthday;
  11. }
  12. public String getName() {
  13. return name;
  14. }
  15. public void setName(String name) {
  16. this.name = name;
  17. }
  18. public int getAge() {
  19. return age;
  20. }
  21. public void setAge(int age) {
  22. this.age = age;
  23. }
  24. public MyDate getBirthday() {
  25. return birthday;
  26. }
  27. public void setBirthday(MyDate birthday) {
  28. this.birthday = birthday;
  29. }
  30. @Override
  31. public String toString() {
  32. return "Employee{" +
  33. "name='" + name + '\'' +
  34. ", age=" + age +
  35. ", birthday=" + birthday +
  36. '}';
  37. }
  38. //按 name 排序
  39. @Override
  40. public int compareTo(Object o) {
  41. if(o instanceof Employee){
  42. Employee e = (Employee) o;
  43. return this.name.compareTo(e.name);
  44. }
  45. //return 0;
  46. throw new RuntimeException("传入的数据类型不一致");
  47. }
  48. }
  1. import org.junit.Test;
  2. import java.util.Comparator;
  3. import java.util.Iterator;
  4. import java.util.TreeSet;
  5. public class EmployeeTest {
  6. @Test
  7. public void test2(){
  8. TreeSet set = new TreeSet(new Comparator() {
  9. @Override
  10. public int compare(Object o1, Object o2) {
  11. if (o1 instanceof Employee && o2 instanceof Employee){
  12. Employee e1 = (Employee) o1;
  13. Employee e2 = (Employee) o2;
  14. MyDate b1 = e1.getBirthday();
  15. MyDate b2 = e2.getBirthday();
  16. //方式一:
  17. //比较年
  18. //int minusYear = b1.getYear() - b2.getYear();
  19. //if (minusYear != 0){
  20. // return minusYear;
  21. //}
  22. //比较月
  23. //int minusMonth = b1.getMonth() - b2.getMonth();
  24. //if (minusMonth != 0){
  25. // return minusMonth;
  26. //}
  27. //比较日
  28. //return b1.getDay() - b2.getDay();
  29. //方式二:
  30. return b1.compareTo(b2);
  31. }
  32. //return 0;
  33. throw new RuntimeException("传入的数据类型不一致");
  34. }
  35. });
  36. Employee e1 = new Employee("liudehua",55,new MyDate(1965,5,4));
  37. Employee e2 = new Employee("zhangxueyou",43,new MyDate(1987,5,4));
  38. Employee e3 = new Employee("guofucheng",44,new MyDate(1987,5,9));
  39. Employee e4 = new Employee("liangzhaowei",21,new MyDate(1978,12,4));
  40. Employee e5 = new Employee("liming",51,new MyDate(1954,8,12));
  41. set.add(e1);
  42. set.add(e2);
  43. set.add(e3);
  44. set.add(e4);
  45. set.add(e5);
  46. Iterator iterator = set.iterator();
  47. while (iterator.hasNext()){
  48. System.out.println(iterator.next());
  49. }
  50. }
  51. @Test
  52. public void test1(){
  53. TreeSet set = new TreeSet();
  54. Employee e1 = new Employee("liudehua",55,new MyDate(1965,5,4));
  55. Employee e2 = new Employee("zhangxueyou",43,new MyDate(1987,5,4));
  56. Employee e3 = new Employee("guofucheng",44,new MyDate(1987,5,9));
  57. Employee e4 = new Employee("liangzhaowei",21,new MyDate(1978,12,4));
  58. Employee e5 = new Employee("liming",51,new MyDate(1954,8,12));
  59. set.add(e1);
  60. set.add(e2);
  61. set.add(e3);
  62. set.add(e4);
  63. set.add(e5);
  64. Iterator iterator = set.iterator();
  65. while (iterator.hasNext()){
  66. System.out.println(iterator.next());
  67. }
  68. }
  69. }

Map接口

Map实现类结构

屏幕截图 2022-04-06 153606.jpg

Map结构的理解

Map中的key:无序的、不可重复的,使用Set存储所有的key —->key所在的类要重写equals()和hasCode()
Map中的value:无序的、可重复的,使用Collection存储所有的value—->value所在的类要重写equals()
一个键值对:key-value构成了一个Entry对象
Map中的entry:无序的、不可重复的,使用Set存储所有的entry
屏幕截图 2022-04-08 172125.jpg
屏幕截图 2022-04-08 172328.jpg
屏幕截图 2022-04-08 173150.jpg

Map常用方法

屏幕截图 2022-04-08 212118.jpg

Map的遍历

  1. public class MapTest {
  2. @Test
  3. public void test1(){
  4. Map map = new HashMap();
  5. map.put("AA",123);
  6. map.put(45,123);
  7. map.put("BB",56);
  8. //遍历所有的key集:keySet()
  9. Set set = map.keySet();
  10. Iterator iterator = set.iterator();
  11. while(iterator.hasNext()){
  12. System.out.println(iterator.next());
  13. }
  14. //遍历所有的value集:values()
  15. Collection values = map.values();
  16. for (Object obj : values){
  17. System.out.println(obj);
  18. }
  19. //遍历所有的key-value
  20. //方式一:entrySet()
  21. Set entrySet = map.entrySet();
  22. Iterator iterator1 = entrySet.iterator();
  23. while (iterator1.hasNext()){
  24. Object obj = iterator1.next();
  25. Map.Entry entry = (Map.Entry) obj;
  26. System.out.println(entry.getKey() + "--->" + entry.getValue());
  27. }
  28. //方式二:
  29. Set keySet = map.keySet();
  30. Iterator iterator2 = keySet.iterator();
  31. while (iterator2.hasNext()) {
  32. Object key = iterator2.next();
  33. Object value = map.get(key);
  34. System.out.println(key + "===" + value);
  35. }
  36. }
  37. }

Collection常用方法

屏幕截图 2022-04-09 145604.jpg
屏幕截图 2022-04-09 145652.jpg