zoukankan      html  css  js  c++  java
  • 集合的相关信息

    体系结构
    单列集合
    Collection接口
    |-List接口:可以存储重复元素、有索引、存取有序
    |-ArrayList:数组,查询快增删慢
    |-LinkedList:链表,查询慢增删快
    |-Set接口:不能存储重复元素、无索引、存取无序
    |-HashSet:哈希表(数组+链表)
    |-LinkedHashSet:存取有序
    |-TreeSet:排序

    双列集合
    Map接口
    |-HashMap
    |-LinkedHashMap
    |-TreeMap

    学习的时候从上往下学习,最上面的接口或者父类定义共性的内容,
    学习了共性下面的实现类或者子类共性内容就不需要再单独学习了,
    只要在共性的基础上学习一些特有的内容即可。

    使用的时候从下往上,下面才是实现,上面都是接口不能直接使用

    单列集合
    Collection常用方法
    添加
    boolean add(E e)
    删除
    void clear()
    boolean remove(Object o)
    判断
    boolean contains(Object o)
    boolean isEmpty()

    获取
    Iterator<E> iterator()
    int size()
    Object[] toArray()
    <T> T[] toArray(T[] a)
    带all:涉及两个集合
    boolean addAll(Collection<? extends E> c)
    boolean containsAll(Collection<?> c)
    boolean removeAll(Collection<?> c)
    boolean retainAll(Collection<?> c)

    集合的遍历(迭代器)
    Iterator<E> iterator()

    // 1.创建集合对象
    Collection<Integer> list = new ArrayList<Integer>();

    // 2.添加元素
    list.add(10);
    list.add(50);
    list.add(20);
    list.add(30);

    // 3.迭代器遍历
    Iterator<Integer> it = list.iterator();
    while(it.hasNext()){
    int i = it.next();
    System.out.println(i);
    }

    迭代器Iterator
    遍历的工具

    Iterator接口的方法
    boolean hasNext()
    E next()
    * 取出下一个元素
    * 光标往下移动一位
    void remove()

    列表迭代器ListIterator
    ListIterator是Iterator的子接口
    ListIterator拥有比Iterator更多的功能

    迭代器遍历可能出现的异常
    没有这样的元素异常:NoSuchElementException
    * 已经没有下一个元素了,还通过next方法去取元素
    并发修改异常:ConcurrentModificationException
    产生的原因:使用迭代器才会可能会抛出的问题,满足一下添加就抛出:
    1、使用迭代器遍历
    2、使用集合的方法去对集合中的元素进行添加和删除操作
    modCount:实际修改集合的次数
    expectedModCount:预期修改集合的次数

    解决方法
    1、不使用迭代器遍历
    2、使用迭代器的方法进行添加和删除
    但是迭代器(Iterator)只有删除方法
    如果要添加需要借助列表迭代器(ListIterator)

    List接口
    特有方法
    add(int index,E e)
    remove(int index)
    set(int index,E e)
    get(int index)


    遍历方法
    方式一:迭代器(Iterator) [所有的单列集合都适用]
    方式二:增强for [所有的单列集合都适用]
    方式三:普通for [只适用于List集合]
    -----------------------------
    方式四:列表迭代器(ListIterator) [只适用于List集合]
    方式五:toArray() [所有的单列集合都适用]


    Set接口
    Set接口中没有特有方法,其方法和Collection的方法全部相同

    HashSet
    保证元素唯一的原理
    依赖的是hashCode和equals方法,这两个方法配合使用使得可以保证元素唯一
    LinkedHashSet
    除了存取有序之外,其他都和其父类一致
    TreeSet
    可以对元素进行排序

    构造方法
    TreeSet():使用自然排序
    TreeSet(Comparator c):使用比较器排序

    自然排序
    实现Comparable接口,重写compareTo(Object o)方法

    元素实现Comparable接口,规则写在compareTo(Object o)方法中


    compareTo的参数
    this
    o
    compareTo的返回值
    正数
    0
    负数
    比较器排序
    实现Comparator接口,重写compare(Object o1,Object o2)方法

    TreeSet构造方法中传递Comparator接口实现类,规则写在compare(Object o1,Object o2)方法

    compare的参数
    o1
    o2
    compare的返回值
    正数
    0
    负数


    Collections工具类
    static void sort(List<T> list):自然排序
    static void sort(List<T> list, Comparator<? super T> c):比较器排序
    reverse
    shuffle


    Map
    常用方法
    添加
    V put(K key, V value)
    void putAll(Map<? extends K,? extends V> m)
    删除
    void clear()
    V remove(Object key)
    判断
    boolean containsKey(Object key)
    boolean containsValue(Object value)
    boolean isEmpty()
    获取
    Set<Map.Entry<K,V>> entrySet()
    V get(Object key)
    Set<K> keySet()
    int size()
    Collection<V> values()

    Map的遍历
    方式一:根据键找值
    思路:
    (1)获取所有键的集合
    (2)遍历集合获取每一个键
    (3)根据键找值

    V get(Object key)
    Set<K> keySet()
    方式二:根据键值对找键和值
    思路:
    (1)获取所有的键值对(Map.Entry<K,V>)的集合
    (2)遍历集合获取每一个键值对对象
    (3)根据键值对对象找键(getKey())和值(getValue())

    Map<String,Integer> hm = new HashMap<String,Integer>();

    hm.put("zhangsan",18);
    hm.put("lisi",20);
    hm.put("wangwu",12);

    // 方法二:
    Set<Map.Entry<String,Integer>> entries = hm.entrySet();
    for (Map.Entry<String, Integer> entry : entries) {
    String key = entry.getKey();
    Integer value = entry.getValue();
    System.out.println(key+":"+value);
    }

  • 相关阅读:
    Haskell Interactive Development in Emacs
    Access Java API in Groovy Script
    手工设置Eclipse文本编辑器的配色
    Color Theme of Emacs
    Gnucash的投资记录
    Special Forms and Syntax Sugars in Clojure
    Use w3m as Web Browser
    SSE指令集加速之 I420转BGR24
    【图像处理】 增加程序速度的方法
    TBB 入门笔记
  • 原文地址:https://www.cnblogs.com/zxasd/p/12082045.html
Copyright © 2011-2022 走看看