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
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
添加 add(Object obj) addAll(Collection coll)
Get the number of active element int size ()
Empty collection void clear ()
Whether it is an empty set boolean isEmpty ()
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.
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
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
Set are equal boolean equals (Object obj)
Converted into an array of objects Object [] toArray ()
Gets a collection of objects hash value hashCode ()
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.
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
void add (int index, Object ele): the element is inserted in the index position ele
boolean addAll (int index, Collection eles ): start position index eles in adding all of the elements come
Object get (int index): Gets the element at the specified index location
int indexOf (Object obj): Returns the position of the first occurrence of obj in the collection
int lastIndexOf (Object obj): obj Returns the current collection appears in the last position
Object remove (int index): Removes the specified location index element, and returns this element
Object set (int index, Object ele): setting element at the specified index to ele
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
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
staticclassNode<K,V>implementsMap.Entry<K,V>{finalint hash;final K key;
V value;
Node<K,V> next;}
staticclassEntry<K,V>extendsHashMap.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 =newProperties();
pros.load(newFileInputStream("jdbc.properties"));
String user = pros.getProperty("user");
System.out.println(user)