Java Review--collection

Source: Internet
Author: User

1, the basic structure of the class

Collection
├list
│├linkedlist
│├arraylist
│└vector
│└stack
└set

|-hashset

|-treeset


Map
├hashtable
├hashmap
└weakhashmap


2. Basic Concepts

0) Collection: The top-level interface, which supports a iterator () method that returns an iteration that uses the iteration to access each element of the Collection one at a time. Public interface Collection<e> extends iterable<e>

1) Set (collection): Objects in the collection are not sorted in a particular way, and there are no duplicate objects. Some of its implementation classes (TreeSet) can sort the objects in the collection in a particular way.
2) List: Objects in the collection are sorted by index position, and can have duplicate objects, allowing objects to be retrieved by object at the index of the collection.
3) Map: Each element in the collection contains a pair of key objects and value objects, there are no duplicate key objects in the collection, and the value objects can be duplicated.


3. Important difference of realization class

1) ArrayList and vectors are used to store data in an array way , this array element is larger than the actual stored data in order to increase and insert elements, both allow the direct ordinal index element, but the insertion of data to design to the array element movement and other memory operations, so the index data fast insertion data slow, this is the linear table of the order table advantages and disadvantages, that is, the query fast, delete, insert slower.

2) vector because of the use of the Synchronized method (thread safety) so the performance is worse than ArrayList, but in some security methods, he is very useful. To use a list as a linear table for synchronization, this can be used: collections.synchronizedlist (list);

3) LinkedList use doubly linked list To implement storage, index data by ordinal need to traverse forward or backward, but insert data only need to record the item's front and rear items, so the number of insertions faster! This is the advantage and disadvantage of the list of linear tables, that is, the query is slow, but delete inserts fast. It can be used as a queue.

4) stack inherits from Vector and implements a last-in-first-out stack. The stack provides 5 additional ways to make the vector available as a stack. The basic push and pop methods, and the Peek method to get the stack top element, the empty method tests if the stack is empty, and the search method detects the position of an element on the stack. Stack has just been created as an empty stack.

5) queue is a collection sub-interface, Deque is 1.6 new interface inherits the queue interface, his subclass is basically the function of the stack to complete the queue, Compared to the previous adoption of the Stack or list (LinkedList implemented Deque) as a queue and stack effect is better, and, Deque can be both as a queue and stack, he is a big difference from the list is that he does not have index index to find elements. So, if you need to use a queue or stack, consider deque subclasses: Arraydeque

6) The implementation of the set and map has a significant relationship, look at the source code you will find that the set is the key part of the map, the value of map is a constant substitution.


4. Other matters needing attention

Note that when you start to learn, we all think that the greatest benefit of the set is that what objects can be deposited, if it is an array, can only be stored in certain types of objects, in fact, this feature is a set of shortcomings, which is why there is a generic appearance, for the array, the largest set of sets can be the edge of long storage elements! Is it possible to store an object of any data type if it is an array of type object? But what's the use of that? Type conversion is a dangerous operation.


5. Collection Traversal and deletion
private void print (collection<string> list) {iterator<string> iter = List.iterator ();   while (Iter.hasnext ()) {String str = iter.next ();   if (Str.equals ("key")) {iter.remove (); }  } }

You cannot delete an element in another way for a loop, otherwise an exception will occur: Java.util.ConcurrentModificationException

Java Review--collection

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.