zoukankan      html  css  js  c++  java
  • JAVA的集合

    一、JAVA两大集合阵营

    1、Collection

      (1)Set

        ①HashSet(重写hashcode和equals方法)

          LinkedHashSet(有序的)(遍历比较快)

        ②SortedSet

          TreeSet(需要添加同一类型的数据,自动排序,需要实现comparable接口)((comparable的方法compareTo或者comparator的方法compare)和hashcode和equals三者一致)(在添加数据时排序,在添加完数据后修改属性是不会触发重新排序的,这样子就会导致数据的排序不准或者重复,所以TreeSet在添加完数据后不要修改)

      (2)List(ArrayList,LinkedList,Vector)

    2、Map

        (1)HashMap(重写hashcode和equals方法)(数组+链表+红黑树)(这里的value值的对象需要实现equals方法)

            LinkedHashMap(有序的)(遍历比较快)

        (2)SortedMap

            TreeMap(红黑树)(需要添加同一类型的数据,自动排序,需要实现comparable接口)((comparable的方法compareTo或者comparator的方法compare)和hashcode和equals三者一致)(在添加数据时排序,在添加完数据后修改属性是不会触发重新排序的,这样子就会导致数据的排序不准或者重复,所以TreeMap在添加完数据后不要修改)

        (3)HashTable      

            Properties

    3、Iterator

        ListIterator

        Enumeration(Iterator的旧版本)

    4、Collections

        扩展common-collections,guava-libraries

        同步器

        不可变的容器

    5、比较器comparable与comparator

    如果comparable的方法compareTo或者comparator的方法compare同时存在的话,那么按照后者来

    6、Hash的依据(与底层实现相关)

    equals和hashcode(需要重写这两个方法)

    7、Tree的依据(与底层实现相关)

    Comparable和Comparator(需要重写这两个方法其中之一)

    二、JAVA集合的一些注意点:

      使用内部类实现迭代器

    java集合是不能存放基本数据类型的

    要实现使用(增强for循环)遍历容器,需要容器实现(interface  Iterable<T>接口的Iterator<T> iterator()方法

    HashMap和HashSet是需要重写HashCode与equals方法的,其他的集合只需要重写equals方法的

    HashMap的HashCode方法要与equals方法一致

    1、Iterator迭代器

        迭代器是对容器遍历的一种抽象,更加能体现出java的接口的多态性,在实现类改变的情况下,代码可以不变。

    1.1、hasNext

    1.2、next

    1.3、remove

          注意:这里的remove只能在next后面执行一次

    三、Set

    1、HashSet可以添加null值

    2、TreeSet这是个实现,有序的set

    四、Map

      1、HashMap也可以添加null值(key可以为一个null,value可以为多个null)(线程不安全)

      2、HashTable里面不能存放null值(key与value都不能为null)(线程安全)

     五、Properties(key和value都只能为字符串)

    三种方式加载

        public static void main(String[] args) throws Exception {
                Properties properties = new Properties();
                File file = new File("test22.txt");
                InputStream is = new FileInputStream(file);
                properties.load(is);
                System.out.println(properties);
                is.close();
        }

    五、List

      Arraylist  底层实现是数组,线程不安全,查询快,(增加、修改、删除)慢

      LinkedList  底层实现是链表,线程不安全,查询慢,增加、修改、删除)快

      Vector  底层实现是数组,但是线程安全的。

    1、ArrayList

    2、LinkedList

    六、其他的集合

      1、Queue(普通队列,优先队列,堆栈)

      抛出异常 特殊值
    插入 add offer
    移除 remove poll
    获取 element peek

      

     

      2、Deque(双端队列)

         3、

     参考文献

    原型态的集合:https://segmentfault.com/a/1190000018189575?utm_source=tag-newest

  • 相关阅读:
    Git修改注释
    数组和切片的区别
    SpringBoot启动报:Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
    eclipse中web.xml报:The content of element type "web-app" must match "(icon?,display- name?,
    eclipse错误: 在类xxx中找不到 main 方法, 请将 main 方法定义为: public static void main(String[] args) 否则 JavaFX 应用程序类必须扩展javafx.application.Application
    idea Cannot resolve method ‘setAttribute(java.lang.String, java.lang.String)/不能使用pageContext.setAttribute()
    解决Nginx启动报nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
    数据仓库开发规范
    详解会话技术cookie、session和token
    Requests爬虫包及解析工具 xpath、正则、Beautiful Soup
  • 原文地址:https://www.cnblogs.com/erdanyang/p/10302540.html
Copyright © 2011-2022 走看看