My Java journey Lesson 5 JAVA language syntax collection, java journey

Source: Internet
Author: User

My Java journey Lesson 5 JAVA language syntax collection, java journey
JAVA collections can only store reference data types, but cannot store basic data types. int can be replaced by Integer.

I. Collection Interface
1. Iterable <E> implements this interface to allow the object to become the target of the "foreach" statement.
2. The Collection <E> interface extends the Iterable interface. This interface is the Root Interface in the Collection hierarchy.
The basic interface of the Collection class is the Collection interface, which has two basic methods: add (add elements to the Set) and iterator (return an object that implements the Iterator interface ).
There are also many useful methods, such as size, isEmpty, and contains. To make it easier for the implementer to implement this interface, the java class library provides a AbstractCollection class. It abstracts the basic method size and iterator, but provides a routine method here.
3. Iterator <E> interface. Iterator used to iterate the collection. Replaces the Enumeration interface. There are three methods:
Next,
Before calling the next method, hasNext should first execute the hasNext method to determine whether there is another element.
Remove deletes the elements returned when the next method is called last time.

2. The Map interface is implemented at the end of the specific set and all others implement the Collection interface.★The ArrayList array List implements the List interface and another dynamic array class Vector class. All the methods in this class are synchronized, while the methods in ArrayList are not synchronized. ArrayList is used when synchronization is not required to save resources. It takes a lot of money to delete an element from an array or array list, because all elements in the array after the element is deleted must be moved to the front-end of the array. Insert an element. The linked list below solves this problem.★The Linked List implements that all linked lists in the List interface java are actually two-way links, that is, each node also stores references pointing to the front node. In this way, you only need to update the node near the deleted element when deleting an element from the linked list.
A major difference between a linked list and a generic list. A linked list is an ordered set.The location of each object is very important. The linked list does not support fast random access. If you want to view the nth element in the linked list, you must start from scratch and overwrite n-1 elements. Therefore, when an integer index is required to access elements, the linked list is usually not used. Even so, the shortlist class still provides a get method to obtain the nth element, which is of course inefficient.★A HashSet hash can be used to quickly search for the desired objects. A hash is an integer for each object, which is called a hash code. In java, the hash table is implemented using a linked list array. A hash can be used to implement several important data structures. The simplest type is set. Set type is No repeated Elements. (Ensure that there are no duplicates: When an element is added, if this set does not contain any (e = null? E2 = null: e. equals (e2) element e2, then add the specified Element e to the set. If this set already contains this element, the call does not change this set and returns false. Combined with the constructor limitations, this ensures that the set never contains repeated elements .) The java Collection class library provides a HashSet class that implements the set interface and implements a hash-based set. When When the sequence of elements in a collection is not concernedTo use HashSet.★The TreeSet class is similar to the hash set. However, a tree set is an ordered set. Sorting is completed in the tree structure. Each time an element is added, it is placed in the correct position. Therefore, it is slower to add to the tree than to add to the hash. For object comparison, the Comparator object is passed to the TreeSet constructor. Compared with hash sets, hash sets are used when sorting is not required. Tree sets are required to save costs.★Queue and double-end queue allow people to effectively add an element at the end and delete an element in the header. A dual-end queue is a two-end queue that can effectively add or delete elements at the same time at the header and tail. Adding Elements in the middle is not supported. The Deque interface is implemented by the ArrayDeque and inclulist classes. Both classes provide dual-end queues. The length of the queue can be increased when necessary. Elements in a PriotiryQueue priority queue can be inserted in any order, but are always retrieved in the order of order. That is to say, whenever the remove method is called, the smallest element in the priority queue is always obtained. The priority queue uses the heap data structure ). Heap is a self-adjusting binary tree. The addition and deletion operations on the tree allow the smallest elements to be moved to the root without having to spend time sorting the elements. D. iterations are not accessed in the order of elements. A typical example of using a priority queue is task scheduling.★The ing table contains key/value pairs. The data structure of the ing table is used to find the corresponding elements through some keys. The Java class library has two common implementations: HashMap and TreeMap. These two classes implement the Map interface. Like a set, if you do not need to sort it, select hash map. The Collection framework does not regard the ing table as a Collection, but provides a view of the ing table. This is a set of views that implement Collection interface objects or its sub-interfaces. These three views are: keySet, values, and entrySet ).★Weak hash ing set of private set and ing table set 1. Deletes useless values from the long-lived ing table. WeakHashMap Class 2 link hash set and link ing table. Used to remember the order of inserted element items. LinkedHashSet and LinkedHashMap link hash ing tables use the access sequence instead of the insertion sequence to iterate the ing table entries. The access sequence is critical to the "least recently used" principle for implementing high-speed cache. 3. Enumeration set and ing table EnumSet are efficient implementations of an enumeration type element set. You can use common methods of the Set interface to modify EnumSet. EnumMap is a ing table whose key type is enumeration. It can be directly and efficiently implemented using a value array. 4. IdentityHashMap. Different key objects, even if the content is the same, are considered different objects. This feature can be used to track the traversal status of each object.★The Collection framework set has two basic interfaces: Collection and Map. The List interface is an ordered set. RandomAccess interface. There is no way, but it can be used to detect whether a specific set supports efficient random access. The ListIterator interface defines a method to add an element to the front of the iterator. The Set interface is the same as the Collection interface, but its method behavior has a more rigorous definition. (Set rejects repeated elements ). The SortedSet and SortedMap interfaces expose the comparator objects used for sorting, and the defined methods can obtain the subset view of the set. Finally, Java SE 6 introduces the interfaces NavigableSet and NavigableMap, which include several methods for searching and traversing in an ordered set and ing table. The TreeSet and TreeMap classes implement these interfaces.
★The Hashtable class of the legacy set 1 works the same as HashMap. 2. The Enumeration interface traverses the element sequence. There are two methods, hasMoreElements and nextElement, which are similar to the hasNext method and next method of the Iterator interface. 3. An Attribute ing table has a special ing table structure. Three features: keys and values are both strings. tables can be saved to a file or loaded from the file. A default auxiliary table is used. 4. Stack class. Stack class extended to Vector class 5-bit set BitSet class

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.