Collection and map Summary __java

Source: Internet
Author: User
Tags array length iterable set set

I. Definition

A set framework is a unified standard architecture for representing and manipulating sets, and the set framework in Java is divided into two parts: the collection interface and the map interface. Second, use (i) data structures involved in the set framework

1. Data structure Classification

1 The linear table is an organization of data in memory, the way of storage; one-dimensional arrays, sequential tables, lists, stacks, queues, circular queues, and hash lists are logical concepts, a concept and an idea, and a logical realization in a linear table.

2 The data structure in Java involves arrays (one-dimensional, multidimensional), sequential table (ArrayList, Vector), List (linkedarraylist, Linkedset), stack (stack), queues (queue), hash list (HASHMAP), Tree (TreeSet, TreeMap) and other (two) set frame Atlas

1. The main implementation class of the set framework

2. Set Frame class Diagram

1 collection is the most basic set interface, and a collection represents a set of object, that is, the collection element (Elements). Some collection allow the same elements while others do not, some can sort and others do not. The Java SDK does not provide classes that directly inherit from collection, and the Java SDK provides classes that inherit from collection such as list and set.

2 All classes that implement the collection interface must provide two standard constructors: parameterless constructors are used to create an empty collection, and a constructor for a collection parameter is used to create a new collection. This new collection has the same element as the incoming collection. The latter constructor allows the user to replicate a collection.

3 why inherit iterable interface without inheriting iterator?

Because the core method of the Iterator interface next () or Hasnext () is dependent on the current iteration position of the iterator. If the collection directly implements the iterator interface, it is bound to cause the collection object to contain the data (pointers) for the current iteration position, and the result of the next () method becomes unpredictable when the collection is passed between different methods because the current iteration position is not preset. Unless you add a reset () method to the iterator interface to reset the current iteration position. Even so, collection can only have one current iteration position at a time, and iterable does not, each call returns an iterator that counts from the beginning. Multiple iterators are not interfering with each other.

The implementation class of the sub-interface of the different collection interfaces returns iterator specific types that may be different, the array may return Arrayiterator,set may return Setiterator,tree may return treeiterator, But they all implement the iterator interface. Therefore, the client does not care what kind of iterator, it only needs to obtain this iterator interface, this is the power of object-oriented.

Iterator it = Collection.iterator (); Gets an iterative child while 

(It.hasnext ()) { 

          Object obj = It.next ();//Get the next element 

(iii) collection interface

1. List Interface

The list is an ordered collection, using this interface to precisely control where each element is inserted. The user can access the elements in the list, similar to the Java array, by using the index (the position of the element in the list, similar to the array subscript). Unlike the set mentioned below, the list allows the same elements. In addition to the iterator () method necessary for collection interfaces, the list provides a Listiterator () method that returns a Listiterator interface, compared to the standard iterator interface, There are a number of add () listiterator that allow adding, deleting, setting elements, and traversing forward or backwards. Common classes that implement the list interface are Linkedlist,arraylist,vector and stack.

1) ArrayList

ArrayList implements a variable sized array that allows all elements, including null. The Size,isempty,get,set method runs at a constant time. But the Add method cost is the allocated constant, adding n elements requires O (n) time, and the other methods run at a linear time.

Each ArrayList instance has a capacity (Capacity), the size of the array used to store the elements, which can automatically increase as new elements are added, but the growth algorithm is not defined. When you need to insert a large number of elements, you can call the Ensurecapacity method before inserting to increase the capacity of the ArrayList to increase the insertion efficiency. Like LinkedList, ArrayList is also unsynchronized (unsynchronized).

2) LinkedList

LinkedList implements the list interface, allowing null elements. Additionally LinkedList provides additional Get,remove,insert methods at LinkedList's header or tail. These operations enable LinkedList to be used as stacks (stack), queues (queue), or bidirectional queues (deque), which can be considered linkedlist in method and logic as well as ArrayList, but with a certain difference in performance, ArrayList is suitable for random access linkedlist more suitable for inserting and deleting, and can ignore this difference when there is no great requirement for performance.

Note that LinkedList does not have a synchronization method. If multiple threads access a list at the same time, you must implement access synchronization yourself. One workaround is to construct a synchronized list when the list is created:

List List = Collections.synchronizedlist (new LinkedList (...));

3) Vector

Vectors are very similar to ArrayList, but vectors are synchronized. Iterator created by vector, although the same interface was created with ArrayList, but because vectors are synchronized, and when one iterator is created and is being used, another thread changes the state of the vector (for example, Add or remove some elements), the concurrentmodificationexception is thrown when the iterator method is invoked, so the exception must be caught.

ArrayList and vectors store data in an array that is larger than the actual stored data in order to add and insert elements, allowing direct ordinal index elements, but inserting data to design memory operations such as array element movement, so the index data quickly inserts data slowly, Vector because of the use of the Synchronized method (thread safety), so the performance is worse than ArrayList, LinkedList use a two-way linked list to store, indexed data by ordinal need to go forward or backward traversal, but when inserting data only need to log the item before and after, So the inserts are several times faster.

4) Stack

Stack inherits from the vector, implementing a LIFO stack. Stack provides 5 additional methods that allow vectors to be used as stacks. Basic push and Pop methods, and Peek method to get the top of the stack elements, empty method test stack is empty, the search method detects an element in the stack position. Stack is just created after stack is empty.

2. Set interface

A set is a collection that does not include duplicate elements. It maintains its own internal sort, so random access doesn't make any sense. Like the list, it also runs NULL, but only one. Because of the specificity of the set interface, the elements in all incoming set sets must be different, and note that any mutable object, if it causes e1.equals (E2) ==true when an element in the collection is manipulated, will inevitably cause some problems. The set interface is implemented with the following sets: Enumset, HashSet, TreeSet.

1) HashSet

HashSet is the fastest collection of queries, because its interior is implemented by Hashcode. The order of the elements inside it is determined by the hash code, so it does not guarantee the iteration order of the set, especially if it does not guarantee that the order is immutable.

2) TreeSet

Based on TreeMap, a set that is always in a sorted state is generated, and the interior is implemented as TreeMap. It uses the natural order of the elements to sort the elements, or to sort according to the comparator provided when the set was created, depending on the constructor method used.

3) Linkedhashset

The bottom layer is the list implementation, is the set set of the only one can be guaranteed how to save the collection object, because it is a subclass of HashSet, so it is also to ensure that the element is unique, and the principle of hashset.

3. Queue Interface

1 blocking Queue (Blockingqueue): When the queue is full, inserting elements will throw an exception, mainly including Arrayblockqueue, Priorityblockingqueue, Linkedblockingqueue.

2) Two-terminal queue (Deque): supports inserting and removing elements at both ends of the head and end, mainly including: Arraydeque, Linkedblockingdeque, LinkedList.

(d) Map interface

Unlike the list and set interfaces, map is a collection of key-value pairs that provide a mapping of key to value. It also did not inherit collection. In map, it guarantees the one by one correspondence between key and value. This means that a key corresponds to a value, so it cannot have the same key value, and of course the value can be the same. To achieve the map are: HashMap, TreeMap, Hashtable, Properties, Enummap.

1) HashMap

HashMap is similar to Hashtable, except that HashMap is unsynchronized and allows NULL, that is, null value and null key. , but when HashMap is treated as collection (the values () method returns collection), its iterative child operation time cost is proportional to the capacity of the HashMap. Therefore, if the performance of an iterative operation is significant, do not set the HASHMAP initialization capacity too high, or the load factor too low.

2) TreeMap

The key is sorted by some sort rule, the interior is implemented with Red-black (red-black) tree data structure, and the SortedMap interface is realized.

3) Hashtable

Hashtable inherits the dictionary class to implement the map interface and implements a hash table of key-value mappings. Any object that is Non-null (non-null) can be a key or value. Adding data using put (Key,value), fetching data using get (key), the time cost of these two basic operations is constant.

Hashtable adjusts performance by initial capacity and load factor two parameters. Typically, the default load factor 0.75 achieves a better balance of time and space. Increasing the load factor saves space but the corresponding lookup time increases, which can affect operations like get and put. The Hashtable is synchronized. Third, summary (i) Similarities and differences of collection types

1. Vector and ArrayList

1 vector is thread-synchronized, so it is thread-safe, and ArrayList is thread-asynchronous and unsafe. If the thread security factor is not taken into account, the ArrayList efficiency is generally higher.

2 If the number of elements in the collection is greater than the length of the current collection array, the vector growth rate is 100% of the current array length, while the ArrayList growth rate is 50% of the current array length. If you use data in a set that is larger than the amount of data, vector has some advantages.

3 If you look for data at a given location, vector and ArrayList use the same time, both 0 (1), and this time using both vector and ArrayList. And if you move data at a specified location for 0 (n-i) n for the total length, you should consider using linklist at this time, because it takes 0 (1) to move data at a given location, and it takes 0 (i) to query the data at a given location.

4) ArrayList and vector are used to store data in an array, this array element is larger than the actual stored data in order to increase and insert elements, both allow direct ordinal index elements, but the insertion of data to design to the array element movement, such as memory operations, so the index data fast insertion data slow, Vector because of the use of the Synchronized method (thread safety), so the performance is worse than ArrayList, LinkedList use a two-way linked list to store, indexed data by ordinal need to go forward or backward traversal, but when inserting data only need to log the item before and after, So the inserts are several times faster.

2. Aarraylist and LinkedList

1 ArrayList is a data structure based on dynamic array, LinkedList data structure based on linked list.

2 for random access get and set,arraylist feel better than LinkedList because linkedlist to move the pointer.

3 for new and delete operations Add and remove,linedlist more advantageous, because ArrayList to move the data.

4 This point depends on the actual situation. If only the single data inserted or deleted, ArrayList speed is better than LinkedList. But if the batch random inserts deletes the data, the LinkedList speed is much better than ArrayList. Because ArrayList each insertion of data, you move the insertion point and all subsequent data.

3. HashMap and TreeMap

1) HashMap through the hashcode of its content for quick lookup, and treemap all elements are maintained in a certain order, if you need to get an orderly result you should use TreeMap (HashMap in the ordering of elements is not fixed). The order of elements in the HashMap is not fixed.

2) HashMap through the hashcode of its content for quick lookup, and treemap all elements are maintained in a certain order, if you need to get an orderly result you should use TreeMap (HashMap in the ordering of elements is not fixed). The collection framework provides two general map implementations: HashMap and TreeMap (TreeMap implements SORTEDMAP interfaces).

3 inserts, deletes, and locates elements in the map, HashMap is the best choice. But if you want to iterate through the keys in natural or custom order, then TreeMap will be better. The implementation of Hashcode () and Equals () is clearly defined by the key class added using the HashMap requirement. This treemap has no tuning option because the tree is always in a balanced state.

4. Hashtable and HashMap

1 Historical reasons: Hashtable is based on the old Dictionary class, and HashMap is an implementation of the map interface introduced by Java 1.2.

2 synchronization: Hashtable is thread-safe, that is, synchronous, and HashMap is not secure, not synchronized.

3) Value: Only HashMap can let you use NULL as the key or value of a table entry. (ii) How to select the appropriate collection type

1. The selection of the list

1 for random queries and iterative traversal operations, arrays are faster than all containers. Therefore, ArrayList is generally used in random access.

2) LinkedList uses bidirectional linked lists to provide very good support for element additions and deletions, while ArrayList performs additions and deletions to elements that require element displacement.

3 for vectors, we generally avoid using them.

4 will ArrayList as the first choice, after all, for the set element we are traversing, only when the performance of the program because the list of frequent insertions and deletions, and then consider LinkedList.

2. The selection of the set

1) HashSet because of the use of hashcode implementation, so in a way its performance is always better than treeset, especially for the increase and lookup operations.

2) Although TreeSet has no hashset performance, it still exists because it can maintain the ordering of elements.

3. The choice of map

1 HashMap and HashSet the same, support quick query. Although Hashtable speed is not slow, but in front of the hashmap or slightly slower, so hashmap in the query can replace Hashtable.

2 because treemap need to maintain the order of internal elements, it is usually slower than HashMap and Hashtable.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.