Java from 0 (12) (collection)

set

  • A container set as Java, objects may be dynamically the plurality of reference into the container.
  • Java collections can be divided into two systems and Map Collection

Collection Interface

Here Insert Picture Description

  • Collection Interface Interface List is the parent, and Set Queue interface methods defined in this interface for operating both set Set, also be used to operate a set of List and Queue.
  • Collection Interface: single data set defines a set of objects access method
    List: element ordered, repeatable set
    Set: element disorder, non-repeatable set

Collection interface method

  1. 添加
    add(Object obj)
    addAll(Collection coll)
  2. Get the number of active element
    int size ()
  3. Empty collection
    void clear ()
  4. Whether it is an empty set
    boolean isEmpty ()
  5. Contains an element
    boolean contains (Object obj): equals method is judged by whether the element is the same object
    boolean containsAll (Collection c): also call the equals method to compare the elements. Take two sets of elements one by one comparison.
  6. Delete
    boolean remove (Object obj): judged by whether or not the equals method is to remove the element of that element. The first element will delete found
    boolean removeAll (Collection coll): Take the set difference of the current collection
  7. Taking the intersection of two sets of
    boolean retainAll (Collection c): result of the presence of the intersection of the current collection does not affect c
  8. Set are equal
    boolean equals (Object obj)
  9. Converted into an array of objects
    Object [] toArray ()
  10. Gets a collection of objects hash value
    hashCode ()
  11. Traversing
    iterator (): returns the iterator object used to traverse the set

Iterator iterator interface

  • Use Iterator interface through the collection element
    Iterator objects called iterators (one design mode), the main element for traversing Collection collection.
  • Iterator only through the collection, Iterator itself does not provide the ability for installing the object. If you need to create Iterator object, there must be a set of iterations.
  • Collection of objects calling each iterator () method gets a new iterator object, all before the first element of the collection of the default cursor.
/hasNext():判断是否还有下一个元素
while(iterator.hasNext()){
//next():①指针下移 ②将下移以后集合位置上的元素返回
System.out.println(iterator.next());
}
Iterator iter = coll.iterator();//回到起点
while(iter.hasNext()){
    Object obj = iter.next();
    if(obj.equals("Tom")){
    iter.remove();
}

One of the sub-interface Collection: List Interface

  • Given the limitations of Java in the array is used to store data, we usually use the alternate array List
  • List of elements in the ordered collection classes, and can be repeated, the set of each element has its corresponding sequence index.
  • List container element corresponds to an integer number indicating its position in the container, the container can be accessed according to the number of elements.
List interface method
  1. void add (int index, Object ele): the element is inserted in the index position ele
  2. boolean addAll (int index, Collection eles ): start position index eles in
    adding all of the elements come
  3. Object get (int index): Gets the element at the specified index location
  4. int indexOf (Object obj): Returns the position of the first occurrence of obj in the collection
  5. int lastIndexOf (Object obj): obj Returns the current collection appears in the last position
  6. Object remove (int index): Removes the specified location index element, and returns this element
  7. Object set (int index, Object ele): setting element at the specified index to ele
  8. List subList (int fromIndex, int toIndex): Returns to the subset from fromIndex position toIndex
List one implementation class: ArrayList
  • ArrayList class is a typical implementation of the List interface, the main achievement category
  • Essentially, ArrayList object reference is a "variable-length" array
  • Arrays.asList (...) method returns a set of List, neither ArrayList instance, is not a Vector instance. Arrays.asList (...) returns the value of a fixed length set of List
List implementation class of the two: LinkedList
  • For frequent operation of inserting or deleting elements, it is recommended to use the LinkedList class, high efficiency
  • LinkedList: doubly linked list, could not declare the array, but the definition of the type Node first and last, is used to record the first and last elements. Meanwhile, the class definition of internal Node, as LinkedList basic structure of the data stored. Node addition to saving the data, but also defines two variables:
    the position of a recording element of the front variable prev
    next record the position of a variable element
    Here Insert Picture Description
List implementation class of three: Vector
  • Vector is an old collection, JDK1.0 there. Most of the operations and ArrayList same, the difference is that Vector is thread-safe.
  • In various list, preferably ArrayList as the default choice. When inserted, deleted frequent use LinkedList; Vector is always slower than ArrayList, so try to avoid using.

Collection of the two sub-interface: Set Interface

  • Set接口是Collection的子接口, set接口没有提供额外的方法
  • Set 集合不允许包含相同的元素,如果试把两个相同的元素加入同一个Set 集合中,则添加操作失败。
  • Set 判断两个对象是否相同不是使用 == 运算符,而是根据 equals() 方法
Set实现类之一: HashSet
  • HashSet 是 Set 接口的典型实现,大多数时候使用 Set 集合时都使用这个实现类。
  • HashSet 按 Hash 算法来存储集合中的元素,因此具有很好的存取、查找、删除性能。
  • HashSet 具有以下特点:
    不能保证元素的排列顺序
    HashSet 不是线程安全的
    集合元素可以是 null\
  • HashSet 集合判断两个元素相等的标准: 两个对象通过 hashCode() 方法比较相等,并且两个对象的 equals() 方法返回值也相等。
Set实现类之二: LinkedHashSet
  • LinkedHashSet 是 HashSet 的子类
  • LinkedHashSet 根据元素的 hashCode 值来决定元素的存储位置,但它同时使用双向链表维护元素的次序,这使得元素看起来是以插入顺序保存的。
  • LinkedHashSet插入性能略低于 HashSet, 但在迭代访问 Set 里的全部元素时有很好的性能。
  • LinkedHashSet 不允许集合元素重复。
Set实现类之三: TreeSet
  • TreeSet 是 SortedSet 接口的实现类, TreeSet 可以确保集合元素处于排序状态。
  • TreeSet底层使用红黑树结构存储数据
    Here Insert Picture Description

Map接口

Here Insert Picture Description

  • Map与Collection并列存在。用于保存具有映射关系的数据:key-value
    ( Map 中的 key 和 value 都可以是任何引用类型的数据
  • Map 中的 key 用Set来存放, 不允许重复,即同一个 Map 对象所对应的类,须重写hashCode()和equals()方法
  • 常用String类作为Map的“键”
  • key 和 value 之间存在单向一对一关系,即通过指定的 key 总能找到唯一的、确定的 value
Map map = new HashMap();
//map.put(..,..)省略
System.out.println("map的所有key:");
Set keys = map.keySet();// HashSet
for (Object key : keys) {
    System.out.println(key + "->" + map.get(key));
}
System.out.println("map的所有的value: ");
Collection values = map.values();
Iterator iter = values.iterator();
while (iter.hasNext()) {
    System.out.println(iter.next());
}
System.out.println("map所有的映射关系: ");
// 映射关系的类型是Map.Entry类型,它是Map接口的内部接口
Set mappings = map.entrySet();
for (Object mapping : mappings) {
    Map.Entry entry = (Map.Entry) mapping;
    System.out.println("key是: " + entry.getKey() + ", value是: " + entry.getValue());
}
Map实现类之一: HashMap
  • HashMap是 Map 接口使用频率最高的实现类。
  • 允许使用null键和null值,与HashSet一样,不保证映射的顺序。
  • 所有的key构成的集合是Set:无序的、不可重复的。所以, key所在的类要重写:equals()和hashCode()
  • 所有的value构成的集合是Collection:无序的、可以重复的。所以, value所在的类要重写: equals()
    一个key-value构成一个entry
  • 所有的entry构成的集合是Set:无序的、不可重复的
  • HashMap 判断两个 key 相等的标准是:两个 key 通过 equals() 方法返回 true,hashCode 值也相等。
  • HashMap 判断两个 value相等的标准是:两个 value 通过 equals() 方法返回 true
Map实现类之二: LinkedHashMap
  • LinkedHashMap 是 HashMap 的子类
  • 在HashMap存储结构的基础上,使用了一对双向链表来记录添加元素的顺序
  • 与LinkedHashSet类似, LinkedHashMap 可以维护 Map 的迭代顺序:迭代顺序与 Key-Value 对的插入顺序一致
static class Node<K,V> implements Map.Entry<K,V> {
    final int hash;
    final K key;
    V value;
    Node<K,V> next;
}
static class Entry<K,V> extends HashMap.Node<K,V> {
    Entry<K,V> before, after;
    Entry(int hash, K key, V value, Node<K,V> next) {
        super(hash, key, value, next);
    }
}
Map实现类之三: TreeMap
  • TreeMap存储 Key-Value 对时, 需要根据 key-value 对进行排序。
  • TreeMap 可以保证所有的 Key-Value 对处于有序状态。
  • TreeSet底层使用红黑树结构存储数据
Map实现类之四: Hashtable
  • Hashtable是线程安全的。
  • Hashtable and HashMap same principle to achieve the same functionality. The underlying structure uses a hash table, query speed, in many cases can be interoperable.
  • Unlike HashMap, Hashtable does not permit null as a key and value
  • Like the HashMap, Hashtable can not guarantee the order in which the Key-Value pairs
  • Analyzing two equal Hashtable key, two equal value standards, consistent with the HashMap.
Map implementation of the five categories: Properties
  • Hashtable Properties class is a subclass of the object document for processing properties
  • Due to the nature of the document key, value is a string type, so Properties in the key and value are strings type
  • When accessing data, recommended setProperty (String key, String value) and the method getProperty (String key) method
Properties pros = new Properties();
pros.load(new FileInputStream("jdbc.properties"));
String user = pros.getProperty("user");
System.out.println(user)
Published 31 original articles · won praise 0 · Views 220

Guess you like

Origin blog.csdn.net/nimwwda/article/details/104038818