Learning Notes-java Collection

Source: Internet
Author: User
Tags comparable object serialization

Background:

Look at the 8th edition of Java Core technology, covering jdk1.6. The main is to set the overall global and detailed grasp, more in-depth understanding of the collection. I am familiar with Java, but the details of the understanding is not deep, the knowledge point is not complete, this is the knowledge of the lack of missing.

I. Collection interface
    1. Interface and implementation separation

When a collection is used in a program, once a collection is built, it is not necessary to know exactly which implementation to use, so it is only meaningful to use a specific class when building a collection object. You can use an interface type to hold a reference to a collection.

2. Aggregation interface and iterative interface

The Java iterator should be considered to be in the middle of two elements, and when next is called, the iterator crosses the next element.

The Remove method of the iterator interface removes the element that was returned when the next method was last called. Importantly, the calls to the next method and the Remove method are interdependent.

In order to make it easier for the implementation to implement this interface, the Java class Library provides a class abstractcollection.

Two. Specific collections

1. Linked list LinkedList

LinkedList and ArrayList differences: from the implementation, AL uses an array, can be efficiently random access, and LL takes the internal entry, this class has objects and the nodes of the front and back pointers, so can not be randomly accessed, can only traverse from the beginning, but insert, delete simple, No need to move data before and after.

The Linkedlist.add method adds an object to the end of the list, and often requires inserting elements in the middle of the list, while the interface iterator does not have an Add method, and the collection class provides a sub-interface Listiterator, which provides the Add method, in addition to the method of direction traversal: Previo Us,hasprevious method.

2. Array list ArrayList

If you use dynamic arrays, experienced people will generally choose vectors, but why use ArrayList to replace vectors? Vectors are thread-synchronized, ArrayList threads are out of sync, but with ArrayList performance overhead when single-threaded access is relatively small.

3. Hash set

In Java, the implementation of a hash table is through the HashSet class. A hash table is implemented through a list of linked lists, each of which is called a bucket. The number of buckets in this box is usually 75%-150% of the number of elements, and the reload factor, load factor, determines when the hash table is hashed, and the hash is automatically hashed with double buckets.

4. Tree Set

Feature-ordered set. Inserts into the collection in any order, and each value is automatically rendered in sorted order as the collection is traversed.

The red and Black trees (red-black-tree) are used.

In TreeSet, how are the elements compared? By default, the tree set assumes that the inserted element implements the comparable interface, which defines the CompareTo method. However, the comparable interface, especially the obvious limitation, is embodied in: in a given class, only one interface can be implemented; What if a collection is sorted by the A property and sorted by the B property in another collection? In addition, if the object of a class needs to be sorted, and the creator of this class does not implement the comparable interface, what should be done?

In Java for this situation, the comparator object is presented to the TreeSet constructor to tell the tree set to use a different comparison method.

Is it necessary to replace a hash set with a tree set? This depends on the data you want to collect, and it is more appropriate to sort it in a tree set than if it is more difficult than a hash function. When there are no sorting requirements, it is more appropriate to use a hash set.

5. Queue and double-ended queue

The queue is inserted at the end of the element, the element is deleted in the head, and element is not supported in the queue

A double-ended queue that inserts or deletes elements at the same time at the head and tail.

The Deque interface is introduced in Java SE6 and is implemented by the Arraydeque and LinkedList classes.

6. Priority queue

Priority queues can be inserted in any order, but are always retrieved in the order in which they are sorted. That is, whenever you call the Remove method, you always get the smallest element in the current optimization level queue.

The most typical scenario for using priority queues is task scheduling.

7. Mapping table

mapping table (map), the Java class Library provides 2 common implementations: HashMap and TreeMap, 2 of which implement the map interface.

Choose a hash map or a tree mapping table? As with the previous set, the hash is slightly faster, and if you do not need to access the key in sort order, it is best to select the hash.

The mapping table can get its view, which is the object that implements the collection interface or the view of its sub-interface. These 3 views are key sets, value collections, and key/value pairs, respectively.

8. Special set and L Mapping table class

    • Weakhashmap
    • Link hash sets and link map tables

This is used to remember the order in which elements are inserted, so that the items in the hash list are randomly arranged on the surface. The link hash map Linkedhashmap will iterate through the mapping table entries in the order of access, rather than in the order of insertion.

    • Enumeration set and enumeration mapping table
    • Identifies the hash mapping table Identityhashmap, and the hash value of the key is calculated using the System.identityhashcode method. This class is useful when implementing an object traversal algorithm, such as Object serialization.

Three. Collection Framework
    1. View and Wrapper

The keyset method of the hash table class is one such view example. It returns a class object that implements the set interface, which operates on the original mapping table. This collection is called a view. Views

    • Lightweight set Wrapper

Arrays.aslist collections.ncopies

    • Child Range
    • Non-modifiable view
    • Synchronizing views
    • Inspected view

2. Batch Operations

3. Conversions between sets and arrays

Arrays.aslist

Four. Algorithms

1. Sorting and blending

Collections.sort () comparable and comparator

Sort Descending: Collections.sort (Items,collections.reverseorder ())

2. Two-point lookup method

Collections.binarysearch ()

3. Simple algorithm

Learning Notes-java 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.