• java集合介绍(list 、set、map)


    1.Collection单列集合

    在这个接口下主要有一下的2大类

    List 元素有序、可重复的集合
    Set 元素无序、不可重复的集合

    1.1Collection 接口方法

    1 、添加
      add(Object obj)
     addAll(Collection coll)
    2 、获取有效元素的个数
      int size()
    3 、清空集合
      void clear()
    4 、是否是空集合
      boolean isEmpty()
    5 、是否包含某个元素
      boolean contains(Object obj) 是通过元素的 equals 方法来判断是否
    是同一个对象
      boolean containsAll(Collection c) 也是调用元素的 equals 方法来比
    较的。拿两个集合的元素挨个比较。
    6 、删除
      boolean remove(Object obj) 通过元素的 equals 方法判断是否是
    要删除的那个元素。只会删除找到的第一个元素
      boolean removeAll(Collection coll) 取当前集合的差集
    7 、取两个集合的交集
      boolean retainAll(Collection c) 把交集的结果存在当前集合中,不
    影响 c
    8 、集合是否相等
      boolean equals(Object obj)
    9 、转成对象数组
      Object[] toArray()
    10 、获取集合对象的哈希值
      hashCode()
    11 、遍历
      iterator() 返回迭代器对象,用于集合遍历
    案例
    1. /**
    2. * 单列数据,定义了存取一组对象的方法的集合
    3. * List:元素有序、可重复的集合
    4. * Set:元素无序、不可重复的集合
    5. */
    6. public class CollectionDome
    7. {
    8. public static void main(String[] args) {
    9. // f1();
    10. // f2();
    11. f3();
    12. }
    13. //基础方法
    14. public static void f1(){
    15. Person person1=new Person("张三",22);
    16. Person person2=new Person("王五",12);
    17. Collection collection1=new ArrayList<>();
    18. //添加单个元素元素
    19. collection1.add(123);
    20. collection1.add("DFP");
    21. collection1.add(person1);
    22. System.out.println("collection1:"+collection1);
    23. Collection collection2=new ArrayList<>();
    24. collection2.add(345);
    25. collection2.add("CCC");
    26. collection2.add(person2);
    27. System.out.println("collection2:"+collection2);
    28. //添加单个集合元素
    29. collection1.addAll( collection2);
    30. System.out.println("collection1:"+collection1);
    31. //获取有效元素的个数
    32. int size = collection1.size();
    33. System.out.println("长度:"+size);
    34. //是否是空集合
    35. boolean empty1 = collection1.isEmpty();
    36. System.out.println(empty1);
    37. //清空集合
    38. // collection2.clear();
    39. System.out.println("collection2.isEmpty:"+collection2.isEmpty());
    40. //是否包含某个元素是通过元素的equals方法来判断是否是同一个对象
    41. System.out.println("equals方法来判断是否是同一个对象"+collection1.contains(123));
    42. // 也是调用元素的equals方法来比较的。拿两个集合的元素挨个比较。
    43. System.out.println("是否包含某个集合"+collection1.containsAll(collection2));
    44. //移除某个元素
    45. boolean remove = collection2.remove(345);
    46. System.out.println("移除某个元素"+remove);
    47. System.out.println("collection2:"+collection2);
    48. Collection collection3=new ArrayList<>();
    49. collection3.add("CCC");
    50. //取当前集合的差集
    51. boolean removeAll = collection1.removeAll(collection3);
    52. System.out.println("取当前集合的差集:"+removeAll);
    53. System.out.println("C1:"+collection1);
    54. //取两个集合的交集
    55. System.out.println(collection2);
    56. boolean b = collection2.retainAll(collection3);
    57. System.out.println("collection2:"+collection2);
    58. }
    59. //迭代器运用
    60. public static void f2(){
    61. Person person1=new Person("张三",22);
    62. Collection collection1=new ArrayList<>();
    63. //添加单个元素元素
    64. collection1.add(123);
    65. collection1.add("DFP");
    66. collection1.add(person1);
    67. System.out.println("collection1:"+collection1);
    68. //获取Iterator类
    69. Iterator iterator = collection1.iterator();
    70. System.out.println("iterator:"+iterator);
    71. // while (iterator.hasNext()){
    72. // Object next = iterator.next();
    73. // System.out.println(next);
    74. // }
    75. //因为上面的遍历 已经将iterator遍历到末尾,如果不重新创建Iterator类下一个指向空指针
    76. // System.out.println(iterator.next());//报错
    77. //执行行到末尾报错空指针
    78. // while (iterator.next()!=null){
    79. // System.out.println(iterator.next());
    80. // }
    81. for (Iterator it = iterator; it.hasNext(); ) {
    82. System.out.println(iterator.next());
    83. }
    84. }
    85. //转换
    86. public static void f3(){
    87. Collection list=new ArrayList<>();
    88. //数组 转换为 列表
    89. char[] c=new char[] {'c','b','w'};
    90. List<char[]> chars = Arrays.asList(c);
    91. System.out.println(chars);
    92. Iterator<char[]> iterator = chars.iterator();
    93. while (iterator.hasNext()){
    94. char[] next = iterator.next();
    95. System.out.println(next);
    96. }
    97. list.add(1);
    98. list.add(11);
    99. list.add(111);
    100. //列表转换为 数组
    101. Object[] objects = list.toArray();
    102. System.out.println(objects);
    103. for (int i=0;i<objects.length;i++){
    104. System.out.println(objects[i]);
    105. }
    106. }
    107. }

    1.2List接口

    List的实现类 :
    ArrayList:list的主要实现类、线程不安全,查找效率高,底层使用Object [] elementDate数组。自动扩容 1.5倍。
    
    LinkedList :对于频繁 插入、删除效率更高,底层使用双向链表。
    
    Vector:list的古老接口的实现类,线程安全,效率低,使用object对象。
    1. public class ListDome {
    2. public static void main(String[] args) {
    3. // f1();
    4. f2();
    5. }
    6. //ArrayList方法操作
    7. public static void f1(){
    8. Person person=new Person("张三",22);
    9. List list=new ArrayList<>();
    10. list.add(1234);
    11. list.add("DFP");
    12. list.add("222");
    13. list.add(person);
    14. System.out.println("Lsit:"+list);
    15. List list1=new ArrayList<>();
    16. list1.add(111);
    17. //在index位置插入ele元素
    18. list1.add(0,222);
    19. List list2=new ArrayList<>();
    20. list2.add(222);
    21. list2.add(222);
    22. list.addAll(list1);
    23. //从index位置开始将eles中的所有元素添加进来
    24. list.addAll(0,list2);
    25. System.out.println("Lsit:"+list);
    26. //获取指定index位置的元素
    27. System.out.println("获取指定index位置的元素:"+list.get(2));
    28. //返回obj在集合中首次出现的位置
    29. System.out.println("返回obj在集合中首次出现的位置:"+list.indexOf(1234));
    30. //返回obj在当前集合中末次出现的位置
    31. System.out.println("返回obj在当前集合中末次出现的位置:"+list.lastIndexOf(person));
    32. //移除指定index位置的元素,并返回此元素
    33. Object remove = list.remove(2);
    34. System.out.println("移除:"+remove+"后的:"+list);
    35. Object remove1 = list.remove(person);
    36. System.out.println("移除:"+remove1+"后的:"+list);
    37. //设置指定index位置的元素为xx,返回值是当前位置的原有值并且替换了
    38. Object set = list.set(2, "00");
    39. System.out.println("set:"+set+"后的:"+list);
    40. //返回从fromIndex到toIndex 位置的子集合 不包含 2位置的
    41. List list3 = list.subList(0, 2);
    42. System.out.println(list3);
    43. for (Object i:list) {
    44. System.out.println(i);
    45. }
    46. }
    47. //LinkedList放操作
    48. public static void f2(){
    49. LinkedList list=new LinkedList<>();
    50. list.add(11);
    51. list.add("sss");
    52. list.add(new Person("DFP",18));
    53. System.out.println(list);
    54. //大部分方法和ArrayList相识 介绍不同的方法
    55. //收尾插入
    56. list.addFirst("the 1");
    57. list.addLast("the end");
    58. list.add(0,"00");
    59. System.out.println(list);
    60. Object[] objects = list.toArray();
    61. for (Object object : objects) {
    62. System.out.println("sss:"+object);
    63. }
    64. //获取收尾元素
    65. Object first = list.getFirst();
    66. Object last = list.getLast();
    67. System.out.println(first);
    68. System.out.println(last);
    69. //移除收尾元素
    70. list.removeFirst();
    71. list.removeLast();
    72. System.out.println(list);
    73. }
    74. }

    1.3set接口

    主要实现类

    HashSet:存储无序的,不可重复的数据

    LinkedHashSet:HashSet的子类,遍历数据的时候可以按照插入顺序遍历 对于频繁的操作效率更高。

    TreeSet:按照对象的指定属性进行排序

    *  1.无序性不等于随机性、存储的数据再底层数组中并非按照数组的索引的顺序进行添加而是根据数据的哈希值进行添加.
    * 2.不可重复性,在保证添加的的元素按照equals()判断,不放回为true进行添加
    要求;添加到set中的数据 在他的类中 要包含 hashCode和equals方法重写的hashCode和equals方法  尽可能保持一致性。
    1. /**
    2. * 添加过程hashset为例:(数组+链表)
    3. * 向hashset添加a元素的时候,首先调用元素a类中的hashCode()方法计算值
    4. * 此时的哈希值接着计算出HashSet底层数组的存放位置(索引位置),再判断这
    5. * 个位置有没有占用元素。
    6. * 如果没有--元素a直接添加 *
    7. * 如果有其他元素b---(获取一链表的形式以存在多个元素),比较a,b的hash值
    8. * 如果hash值不相同 a元素成功添加 *
    9. * 如果相同,需要调用equals()方法进行比较
    10. * 返回true a添加失败
    11. * 返回false a添加成功 *
    12. * 3.要求;添加到set中的数据 在他的类中 要包含 hashCode和equals方法
    13. * 重写的hashCode和equals方法 尽可能保持一致性
    14. */
    15. public class SetDome {
    16. public static void main(String[] args) {
    17. // f1();
    18. // f2();
    19. f3();
    20. }
    21. //HashSet
    22. public static void f1(){
    23. //set中的方法和Collection一样 使用方法也相识
    24. HashSet set=new HashSet<>();
    25. set.add(11);
    26. set.add("ccc");
    27. set.add(new Person("DFP",23));
    28. System.out.println("HashSet:"+set);
    29. Iterator iterator = set.iterator();
    30. while (iterator.hasNext()){
    31. System.out.println(iterator.next());
    32. }
    33. }
    34. //LinkedHashSet
    35. public static void f2(){
    36. /**
    37. * 因为底层添加了链表 记录了添加的顺序
    38. */
    39. Set set=new LinkedHashSet();
    40. set.add(111);
    41. set.add("ccc");
    42. set.add(new Person("DFP",23));
    43. System.out.println("LinkedHashSet:"+set);
    44. }
    45. /**TreeSet
    46. * //为了实现自然或者定制排序 实现Comparable或者Comparator
    47. * 自然排序中,比较2个对象是否相等的标准为:compareTo()返回0 不再是equals方法
    48. * 定制排序比较2个对象是否相等的标准为:compare()返回0 不再是equals方法
    49. *
    50. */
    51. public static void f3(){
    52. TreeSet treeSet=new TreeSet<>();
    53. treeSet.add(11);
    54. treeSet.add(22);
    55. treeSet.add(2);
    56. treeSet.add(-67);
    57. // treeSet.add("1");//报错所以内容类型一致性
    58. System.out.println(treeSet);
    59. TreeSet treeSet1=new TreeSet<>();
    60. treeSet1.add(new Person("DFP",22));
    61. treeSet1.add(new Person("AB",77));
    62. treeSet1.add(new Person("Jm",25));
    63. //排序规则的体现
    64. treeSet1.add(new Person("Tom",17));
    65. treeSet1.add(new Person("Tom",67));
    66. treeSet1.add(new Person("Tom",41));
    67. System.out.println(treeSet1);
    68. }
    69. }

    2.Map双列集合

    主要的实现类 HashMap、 LinkedHashMap、 TreeMap、 Hashtable

    2.1 HashMap

            ​​​​​​​Map的主要实现类,线程不安全,效率高,可以存储null的key和value。 jdk7(数组+链表)jdk8(数组+链表+红黑树)

    1. * HashMap底层原理(jdk7):
    2. * HashMap map=new HashMap(); 在实例化与一个对象之后 创建一个长度是16的一位数组Entry【】table;
    3. * 在执行多次put之后...map.put(k1,v1);
    4. * 首先调用,k1所在类的hashCode()计算k1的哈希值,此时哈希值经过某种算法之后,得到Entry数组中的存放位置。
    5. * 1. 如果此时位置上的数据不为空,此时的k1-v1直接成功添加。
    6. * 2. 如果此时位置的数据不为空,(意味着此时位置上存在一个或者多个数据通过链表的形式存储),比较k1和已存在的数据的哈希值。
    7. * a.如果k1的哈希值与已存在的数据的哈希值不相同,此时的k1-v1 添加成功。
    8. * b。如果k1的哈希值和已存在的某个(key-value)的哈希值相同,继续比较k1类的equalskey
    9. * 如果equals返回结果false表示不相同。成功添加k1-v1
    10. * 如果equals返回结果true表示相同,使用v1替换value。
    11. * 3.扩容问题:
    12. * 超出临界值并且存储的位置非空的时候触发
    13. * 默认方式:扩容为原来 的2倍 将原数据拷贝过来
    14. * HashMap底层原理(jdk8):
    15. * 1. HashMap map=new HashMap():不创建长度16的数组
    16. * 2. 是Node【】数组不是 entry【】数组
    17. * 3、首次调用put方法的时候,创建长度16的数组
    18. * 4.数组+链表+红黑树:当前数组的某个位置的索引位置上的元素以链表形式存在的数据个数>8
    19. * 且长度>64的时候 此时的数组改为红黑树存储

    2.2 LinkedHashMap

    保证在遍历Map元素的时候,可以按照添加属性进行遍历。原因是在HashMap的基础上底层结构上,添加了一对指针,指向首尾。频繁的遍历操作,效率更高。

    2.3TreeMap

    在保证按照添加的key-value对进行排序,实现排序的遍历,此时可以考虑key的自然或者定制排序(底层使用红黑树)。

    2.4Hashtable

    古老的实现类,线程安全。

    2.5 案例

    1. public class MapDome {
    2. public static void main(String[] args) {
    3. f1();
    4. // f2();
    5. }
    6. //HashMap
    7. public static void f1(){
    8. System.out.println("f1*****************");
    9. Map map=new HashMap<>();
    10. map.put("A",-5);
    11. map.put("kk",76);
    12. map.put("3412",1);
    13. map.put("D",new Person("张三",22));
    14. System.out.println(map) ;
    15. // HashMap hashMap=new HashMap<>();
    16. // hashMap.put("李四",145);
    17. // hashMap.put("李六",190);
    18. // hashMap.put("王四",1425);
    19. // System.out.println("hashMap:"+hashMap);
    20. // //将m中的所有key-value对存放到当前map中
    21. // hashMap.putAll(map);
    22. // System.out.println("hashMap:"+hashMap);
    23. // //移除指定key的key-value对,并返回value
    24. // Object remove = hashMap.remove("3412");
    25. // System.out.println("移除"+remove+"的hashMap:"+hashMap);
    26. // //获取指定key对应的value
    27. // Object o = hashMap.get("李四");
    28. // System.out.println("得到李四:"+o);
    29. // //是否包含指定的key
    30. // System.out.println("存在王四?"+hashMap.containsKey("王四"));
    31. 是否包含指定的value
    32. // System.out.println("存在1425?"+hashMap.containsValue(1425));
    33. // //返回map中key-value对的个数
    34. // System.out.println("hashmap的长度:"+hashMap.size());
    35. // //清空当前map中的所有数据
    36. // hashMap.clear();
    37. // System.out.println(hashMap);
    38. //替换key的value,返回原来的value
    39. Object replace = map.replace("A", 100);
    40. System.out.println("替换"+replace+":"+map);
    41. Set entrySet = map.entrySet();
    42. Iterator iterator = entrySet.iterator();
    43. while (iterator.hasNext()){
    44. Object next = iterator.next();
    45. System.out.println(next);
    46. }
    47. }
    48. //LinkedHashMap
    49. public static void f2(){
    50. //方法上一致
    51. System.out.println("f2*****************");
    52. LinkedHashMap map=new LinkedHashMap<>();
    53. map.put("A",-5);
    54. map.put("kk",76);
    55. map.put("3412",1);
    56. map.put("D",new Person("张三",22));
    57. System.out.println(map);
    58. //遍历
    59. /**
    60. * 因为我们的map集合是双列集合
    61. * 对于key这一列:用set结构的方式存放 不允许重复
    62. * 对于value这一列 相当于Collection
    63. *
    64. *  Set keySet():返回所有key构成的Set集合
    65. *  Collection values():返回所有value构成的Collection集合
    66. *  Set entrySet():返回所有key-value对构成的Set集合
    67. */
    68. Set set = map.keySet();
    69. Collection values = map.values();
    70. Iterator setI = set.iterator();
    71. Iterator valueI = values.iterator();
    72. while (setI.hasNext()){
    73. Object next = setI.next();
    74. System.out.print(next+"\t");
    75. }
    76. System.out.println();
    77. while (valueI.hasNext()){
    78. Object nextv = valueI.next();
    79. System.out.print(nextv+"\t");
    80. }
    81. System.out.println();
    82. Set entrySet = map.entrySet();
    83. Iterator iterator = entrySet.iterator();
    84. while (iterator.hasNext()){
    85. Object next = iterator.next();
    86. System.out.println(next);
    87. }
    88. }
    89. }

  • 相关阅读:
    DOM--事件
    在Linux中tomcat占用内存过高可以通过导出hprof日志来解决
    Oracle
    React教程(详细版)
    基于STC15单片机电子时钟液晶1602串口显示-proteus仿真-源程序
    机器学习(22)---信息熵、纯度、条件熵、信息增益
    龙讯旷腾并行科技签署战略合作协议,将PWmat加入以北京超级云计算中心算力资源为“基座”的超算云平台
    【概念】make 与 configure
    华为---DHCP中继代理简介及示例配置
    Flutter笔记:GetX模块中不使用 Get.put 怎么办
  • 原文地址:https://blog.csdn.net/weixin_53472653/article/details/125508690