java:集合总结

「爱情、让人受尽委屈。」 2022-08-02 14:36 252阅读 0赞

集合的特点:只用于存储对象,集合长度可变,可以存储不同类型的对象

Java中常用的两个主要集合体系:

Collection接口

  1. |----List接口

|——Vector类

|——ArrayList类

|——LinkedList类

|——Set接口

|——HashSet类

|——TreeSet类

Map接口

|——HashMap类

|——TreeMap类

|——HashTable类

|——Properties类

下面简要介绍各类的主要特点和使用方法。

(一)Collection体系

(1)List集合:有序(元素的存入和取出顺序一致),可以重复

(a)Vector类特点:内部是数组数据结构,是同步的。枚举是Vector特有的取出

(b)ArrayList类特点:内部是数组数据结构,不同步。替代的Vector。

(c)LinkedList类特点:内部是链表数据结构,不同步,删除、添加元素速度很快。

  1. ArrayListt集合为例,说明该集合的一些操作:
  2. //导包
  3. import java.util.*;
  4. //学生类,作为集合的元素
  5. class Student
  6. {
  7. private String name;
  8. private int age;
  9. //get方法
  10. public String getName()
  11. {
  12. return name;
  13. }
  14. public int getAge()
  15. {
  16. return age;
  17. }
  18. //构造函数
  19. public Student(String name, int age)
  20. {
  21. this.name = name;
  22. this.age = age;
  23. }
  24. //调用集合的contains方法时,需要覆写元素对象的equals方法
  25. public boolean equals(Object obj)
  26. {
  27. Student s = (Student)obj;
  28. if(this.name == s.name && this.age == s.age)
  29. return true;
  30. else
  31. return false;
  32. }
  33. //toString方法
  34. public String toString()
  35. {
  36. return name +":"+age;
  37. }
  38. }
  39. class TestList
  40. {
  41. public static void main(String[] args)
  42. {
  43. //1.创建ArrayList集合
  44. ArrayList<Student> al = new ArrayList<Student>();
  45. //2.向集合中添加元素
  46. al.add(new Student("lisi01", 12));
  47. al.add(new Student("lisi11", 15));
  48. al.add(new Student("lisi02", 22));
  49. al.add(new Student("lisi01", 12));
  50. //3.打印集合
  51. System.out.println(al);
  52. //结果是:[lisi01:12, lisi11:15, lisi02:22, lisi01:12]
  53. //4.获取集合的长度
  54. System.out.println(al.size());//结果是:4
  55. //4.删除元素
  56. //al.remove("lisi00");
  57. //System.out.println(al);
  58. //结果是:[lisi30, lisi01, lisi20, lisi00]
  59. //5.清空集合
  60. //al.clear();
  61. //System.out.println(al); //结果是:[]
  62. //6.判断集合中是否包含元素
  63. Student stu = new Student("lisi02", 22);
  64. System.out.println(al.contains(stu)); //结果是:true
  65. //实现contains方法,必须覆写元素equals方法
  66. //7.判断结合是否为空
  67. System.out.println(al.isEmpty()); //结果是:false
  68. //8.取出list集合中的元素,运用迭代器Iterator
  69. Iterator<Student> it = al.iterator();
  70. while(it.hasNext())
  71. {
  72. Student s = it.next();
  73. System.out.print(s +" ");
  74. }
  75. System.out.println();
  76. //打印结果是:lisi01:12 lisi11:15 lisi02:22 lisi01:12
  77. //9.用高级for循环取出List中的元素
  78. for(Student s : al)
  79. {
  80. System.out.print(s +" ");
  81. }
  82. System.out.println();
  83. //打印结果是:lisi01:12 lisi11:15 lisi02:22 lisi01:12
  84. //10.用Lis集合特有的迭代器ListIterator取出元素,可以在遍历中增加元素
  85. ListIterator<Student> lit = al.listIterator();
  86. while(lit.hasNext())
  87. {
  88. Student s = lit.next();
  89. if(s.equals((Object)new Student("lisi11", 15)))
  90. lit.add(new Student("lisi12", 25));
  91. }
  92. System.out.println(al);
  93. //结果是:[lisi01:12, lisi11:15, lisi12:25, lisi02:22, lisi01:12]
  94. //11.通过脚标获取元素
  95. for(int i = 0; i < al.size(); i++)
  96. {
  97. System.out.print(al.get(i) +" ");
  98. }
  99. System.out.println();
  100. //结果是:lisi01:12 lisi11:15 lisi12:25 lisi02:22 lisi01:12
  101. //12.集合中的元素进行排序,按指定比较器排序
  102. Collections.sort(al, new myCpmpare());
  103. System.out.println(al);
  104. //结果是:[lisi01:12, lisi01:12, lisi02:22, lisi11:15, lisi12:25]
  105. ArrayList<Student> a = new ArrayList<Student>();
  106. a.add(new Student("lisi01", 12));
  107. a.add(new Student("lisi11", 12));
  108. a.add(new Student("lisi02", 22));
  109. System.out.println(a);
  110. //结果是:[lisi01:12, lisi11:12, lisi02:22]
  111. //13.取集合a1,a交集,al中保留和a相同的元素
  112. al.retainAll(a);
  113. System.out.println(al);
  114. //结果是:[lisi01:12, lisi01:12, lisi02:22]
  115. //14.取差集,al中保留和a不同的元素
  116. al.removeAll(a);
  117. System.out.println(al);
  118. //结果是:[]
  119. }
  120. }
  121. //自定义比较器
  122. class myCpmpare implements Comparator<Student>
  123. {
  124. //覆写compare方法
  125. public int compare(Student s1, Student s2)
  126. {
  127. //先按姓名排序
  128. int flag = s1.getName().compareTo(s2.getName());
  129. if(flag > 0)
  130. return 1;
  131. else if(flag < 0)
  132. return -1;
  133. else
  134. return s1.getAge() - s2.getAge();
  135. }
  136. }

LinkedList集合的一些特有方法:

  1. //导包
  2. import java.util.*;
  3. class TestLinkedList
  4. {
  5. public static void main(String[] args)
  6. {
  7. //创建LinkList的容器
  8. LinkedList<String> ld = new LinkedList<String>();
  9. //向容器中添加元素
  10. ld.add("zhangsan00");
  11. ld.add("zhangsan30");
  12. ld.add("zhangsan02");
  13. ld.add("zhangsan01");
  14. //打印容器
  15. System.out.println(ld); //结果是:[zhuangsan00, zhuangsan30, zhuangsan02, zhuangsan01]
  16. //删除头元素
  17. ld.removeFirst();
  18. System.out.println(ld); //结果是:[zhuangsan30, zhuangsan02, zhuangsan01]
  19. //删除尾元素
  20. ld.removeLast();
  21. System.out.println(ld); //结果是:[zhuangsan30, zhuangsan02]
  22. //向头添加元素
  23. ld.addFirst("zhangsan10");
  24. System.out.println(ld); //结果是:[zhangsan10, zhangsan30, zhangsan02]
  25. //向尾添加元素
  26. ld.addLast("zhangsan08");
  27. System.out.println(ld); //结果是:[zhangsan10, zhangsan30, zhangsan02, zhangsan08]
  28. //在指定位置插入元素
  29. ld.add(3,"zhangsan03");
  30. System.out.println(ld); //结果是:[zhangsan10, zhangsan30, zhangsan02, zhangsan03, zhangsan08]
  31. }
  32. }

(2)Set集合:无序(元素的存入和取出顺序不一致),不可重复

(a)HashSet类特点:底层是哈希表数据结构,不同步。通过元素覆写hashCode()和equals()方法,而保证元素的唯一性。

(b)TreeSet类特点:底层数据结构是二叉树,不同步。元素必须实现comparable接口并覆写compareTo()方法或把实现comparator接口并覆写compare()方法的比较器传入TreeSet集合,才能把元素添加到集合,同时保证元素的唯一性;如果两者都存在,以集合的比较器为主

HashSet的例子:

  1. //导包
  2. import java.util.*;
  3. //学生类,作为集合的元素
  4. class Student
  5. {
  6. private String name;
  7. private int age;
  8. //构造函数
  9. public Student(String name, int age)
  10. {
  11. this.name = name;
  12. this.age = age;
  13. }
  14. public int hashCode()
  15. {
  16. return name.hashCode() + age;
  17. }
  18. public boolean equals(Object obj)
  19. {
  20. Student s = (Student)obj;
  21. if(this.name == s.name && this.age == s.age)
  22. return true;
  23. else
  24. return false;
  25. }
  26. //toString方法
  27. public String toString()
  28. {
  29. return name +":"+age;
  30. }
  31. }
  32. class TestHashSet
  33. {
  34. public static void main(String[] args)
  35. {
  36. //创建集合
  37. HashSet<Student> hs = new HashSet<Student>();
  38. //向集合中添加元素
  39. hs.add(new Student("lisi01", 12));
  40. hs.add(new Student("lisi11", 15));
  41. hs.add(new Student("lisi02", 22));
  42. hs.add(new Student("lisi01", 12));
  43. //打印集合
  44. System.out.println(hs);
  45. //覆写元素的HashCode方法和equals方法前,结果是:[lisi01:12, lisi11:15, lisi02:22, lisi01:12]
  46. /*元素出现了重复。HashSet中的对象以哈希表的结构存储,HashCode表示对象的地址值。
  47. 不同的对象,HashCode就不一样,所以上面内容相同的两个对象都存储在了集合中。
  48. 为了保证集合中元素的唯一性,必须覆写元素的HashCode方法和equals方法
  49. 覆写元素的HashCode方法和equals方法后,结果是:[lisi02:22, lisi01:12, lisi11:15]
  50. */
  51. //用迭代器取出集合中的元素
  52. Iterator<Student> it = hs.iterator();
  53. while(it.hasNext())
  54. {
  55. System.out.print(it.next() +" ");
  56. }
  57. System.out.println();
  58. //打印结果是:lisi02:22 lisi01:12 lisi11:15
  59. //用高级for循环取出集合中的元素
  60. for(Student stu : hs)
  61. {
  62. System.out.print(stu +" ");
  63. }
  64. //打印结果是:lisi02:22 lisi01:12 lisi11:15
  65. }
  66. }

TreeSet的例子:

  1. import java.util.*;
  2. //学生类,作为集合的元素,实现Comparable接口的compareTo方法,使其具有比较性
  3. class Student implements Comparable
  4. {
  5. private String name;
  6. private int age;
  7. //构造函数
  8. public Student(String name, int age)
  9. {
  10. this.name = name;
  11. this.age = age;
  12. }
  13. //get方法
  14. public String getName()
  15. {
  16. return name;
  17. }
  18. public int getAge()
  19. {
  20. return age;
  21. }
  22. public int compareTo(Object obj)
  23. {
  24. Student s = (Student)obj;
  25. int com = this.name.compareTo(s.name);
  26. if(com > 0)
  27. return 1;
  28. else if(com < 0)
  29. return -1;
  30. else
  31. return this.age - s.age;
  32. }
  33. //toString方法
  34. public String toString()
  35. {
  36. return name +":"+age;
  37. }
  38. }
  39. class TestTreeSet
  40. {
  41. public static void main(String[] args)
  42. {
  43. TreeSet<Student> ts = new TreeSet<Student>(new MyCompare());
  44. //向集合中添加元素
  45. ts.add(new Student("lisi11", 15));
  46. ts.add(new Student("lisi02", 22));
  47. ts.add(new Student("lisi01", 12));
  48. ts.add(new Student("lisi01", 12));
  49. //打印集合
  50. System.out.println(ts);
  51. //结果是:[lisi01:12, lisi11:15, lisi02:22]
  52. }
  53. }
  54. //自定义比较器
  55. class MyCompare implements Comparator<Student>
  56. {
  57. public int compare(Student s1, Student s2)
  58. {
  59. int com = s1.getAge() - (s2.getAge());
  60. if(com > 0)
  61. return 1;
  62. else if(com < 0)
  63. return -1;
  64. else
  65. return s1.getName().compareTo(s2.getName());
  66. }
  67. }

(二)Map体系,以键值对的形式存储,并保证键的唯一性

(1)HashMap集合:底层是哈希表数据结构,允许null键null值,不同步。和HashSet很像,通过覆写键值类的hashCode()和equals()方法,而保证键值的唯一性。

(2)TreeMap集合:底层是二叉树数据结构,线程同步。和TreeSet很像,键值对象必须实现comparable接口并覆写compareTo()方法或把实现comparator接口并覆写compare()方法的比较器传入TreeMap集合,才能把元素添加到集合,同时保证元素的唯一性,并对键值进行排序。

(3)Hashtable集合:底层是哈希表数据结构,不可以存入null键和null值,线程同步。

下面以TreeMap为例,说明Map集合的主要操作:

  1. </pre><pre name="code" class="java">//导包
  2. import java.util.*;
  3. //学生类,作为集合的元素
  4. class Student implements Comparable
  5. {
  6. private String name;
  7. private int age;
  8. //构造函数
  9. public Student(String name, int age)
  10. {
  11. this.name = name;
  12. this.age = age;
  13. }
  14. //get方法
  15. public String getName()
  16. {
  17. return name;
  18. }
  19. public int getAge()
  20. {
  21. return age;
  22. }
  23. //覆写compareTo方法
  24. public int compareTo(Object obj)
  25. {
  26. Student s = (Student)obj;
  27. int com = this.name.compareTo(s.name);
  28. if(com > 0)
  29. return 1;
  30. else if(com < 0)
  31. return -1;
  32. else
  33. return this.age - s.age;
  34. }
  35. //toString方法
  36. public String toString()
  37. {
  38. return name +":"+age;
  39. }
  40. }
  41. class TestTreeMap
  42. {
  43. public static void main(String[] args)
  44. {
  45. //创建TreeMap集合
  46. TreeMap<Student, Integer> tm = new TreeMap<Student, Integer>(new MyCompare()); //
  47. //向集合中添加元素
  48. //向集合中添加元素
  49. tm.put(new Student("zhangsan11", 18), new Integer(1));
  50. tm.put(new Student("zhangsan03", 38), new Integer(3));
  51. tm.put(new Student("zhangsan32", 28), new Integer(2));
  52. tm.put(new Student("zhangsan03", 38), new Integer(3));
  53. tm.put(new Student("zhangsan23", 38), new Integer(2));
  54. System.out.println(tm);
  55. //打印结果:{zhangsan11:18=1, zhangsan32:28=2, zhangsan03:38=3, zhangsan23:38=2}
  56. /*当键值对象实现了comparable接口并覆写compareTo()方法时具有了比较性,同时集合又自定义了比较器,
  57. 键值的排序将以集合的比较器为主*/
  58. //取出Map集合中的元素
  59. //1.根据Mpa集合keySet()方法把键值对象保存在set集合中,再根据集合的get方法,取出对应键的值
  60. Set<Student> keys = tm.keySet();
  61. Iterator<Student> it = keys.iterator();
  62. while(it.hasNext())
  63. {
  64. Student stu = it.next();
  65. System.out.println(stu +":"+tm.get(stu));
  66. }
  67. /*打印结果:zhangsan11:18:1
  68. zhangsan32:28:2
  69. zhangsan03:38:3
  70. zhangsan23:38:2
  71. */
  72. //2.将Map集合的映射关系存到集合中,这个关系的数据类型是Map.Entry,再 根据该类型的getKey()和gatValue()方法
  73. Set<Map.Entry<Student, Integer>> entrySet = tm.entrySet();
  74. Iterator<Map.Entry<Student, Integer>> ite = entrySet.iterator();
  75. while(ite.hasNext())
  76. {
  77. Map.Entry<Student, Integer> me = ite.next();
  78. Student key = me.getKey();
  79. Integer value = me.getValue();
  80. System.out.println(key +":" +value);
  81. }
  82. /*打印结果:zhangsan11:18:1
  83. zhangsan32:28:2
  84. zhangsan03:38:3
  85. zhangsan23:38:2
  86. */
  87. if(tm.containsKey(new Student("zhangsan23", 38)))
  88. {
  89. tm.remove(new Student("zhangsan23", 38));
  90. }
  91. System.out.println(tm);
  92. //打印结果:{zhangsan11:18=1, zhangsan32:28=2, zhangsan03:38=3}
  93. System.out.println(tm.get(new Student("zhangsan11", 18)));
  94. //打印结果:1
  95. }
  96. }
  97. //自定义比较器
  98. class MyCompare implements Comparator<Student>
  99. {
  100. public int compare(Student s1, Student s2)
  101. {
  102. int com = s1.getAge() - (s2.getAge());
  103. if(com > 0)
  104. return 1;
  105. else if(com < 0)
  106. return -1;
  107. else
  108. return s1.getName().compareTo(s2.getName());
  109. }
  110. }

结构分析:

  1. // 数组数据结构,Object[] elementData, 不同步
  2. List list = new ArrayList();
  3. // 数组数据结构,Object[] elementData, 同步
  4. Vector vector = new Vector();
  5. /*
  6. 链表结构,不同步
  7. private static class Node<E> {
  8. E item;
  9. Node<E> next;
  10. Node<E> prev;
  11. Node(Node<E> prev, E element, Node<E> next) {
  12. this.item = element;
  13. this.next = next;
  14. this.prev = prev;
  15. }
  16. }
  17. */
  18. LinkedList<Object> linkedList = new LinkedList<>();
  19. // 不同步, 底层为 HashMap, HashMap 的 key 为存入set的元素,value 为 静态的 new Object()
  20. Set set = new HashSet();
  21. // 不同步,底层是 LinkedHashMap
  22. LinkedHashSet<Object> linkedHashSet = new LinkedHashSet<>();
  23. // 不同步,底层为 TreeMap, TreeMap 的 key 为存入set的元素,value为 静态的 new Object()
  24. TreeSet<Object> treeSet = new TreeSet<>();
  25. /*
  26. 不同步, 底层是Hash表数据结构, 允许 null key null value
  27. Node<K,V>[] table
  28. Node(int hash, K key, V value, Node<K,V> next) {
  29. this.hash = hash;
  30. this.key = key;
  31. this.value = value;
  32. this.next = next;
  33. }
  34. */
  35. Map map = new HashMap();
  36. /*
  37. 同步,底层是Hash表数据结构,不允许 null key null value
  38. Entry<?,?>[] table;
  39. protected Entry(int hash, K key, V value, Entry<K,V> next) {
  40. this.hash = hash;
  41. this.key = key;
  42. this.value = value;
  43. this.next = next;
  44. }
  45. */
  46. Hashtable<Object, Object> hashtable = new Hashtable<>();
  47. /*
  48. 底层是二叉树结构, 不同步
  49. static final class Entry<K,V> implements Map.Entry<K,V> {
  50. K key;
  51. V value;
  52. Entry<K,V> left;
  53. Entry<K,V> right;
  54. Entry<K,V> parent;
  55. boolean color = BLACK;
  56. }
  57. */
  58. TreeMap<Object, Object> treeMap = new TreeMap<>();
  59. /*
  60. 双向链表结构
  61. static class Entry<K,V> extends HashMap.Node<K,V> {
  62. Entry<K,V> before, after;
  63. Entry(int hash, K key, V value, Node<K,V> next) {
  64. super(hash, key, value, next);
  65. }
  66. }
  67. */
  68. LinkedHashMap<Object, Object> linkedHashMap = new LinkedHashMap<>();

发表评论

表情:
评论列表 (有 0 条评论,252人围观)

还没有评论,来说两句吧...

相关阅读

    相关 java集合总结

    首先是继承了Collection接口的两个接口,分别是List和Set。由于分析了下部分源码,所以总结一下:        List接口下有两个常用的类,分别是ArrayL

    相关 java集合总结

    版权声明:本文为博主原创文章,未经博主允许不得转载 一:集合接口的实现类 1.常用集合分为单列集合和双列集合,双列集合就是map映射。 2.单列的集合类的超级接口是