2.2.AbstractList and AbstractSequentialList abstraction
There are two abstract List implementations: AbstractList and AbstractSequentialList. Like the AbstractSet class, they cover the equals () and havehcode () methods to ensure that the two equally collections return to the same hash code. These two lists are equal if the two lists are equal and contain the same elements as the same order. The HashCode () implementation here is specified in the List interface definition, but implemented here.
In addition to equals () and havehcode (), AbstractList and AbstractSequentialList are part of the rest of the List method. Because the random access and order access of the data is implemented separately, the creation of a specific list implementation is easier. A set of methods that need to be defined depends on the behavior you want to support. You will never have to provide yourself is the implementation of the Iterator method.
2.3. LinkedList class and arraylist class
There are two conventional List implementations in the Collection Framework: ArrayList and LinkedList. Which of the use of two LIST depends on your specific needs. If you want to support random access, you don't have to insert or remove elements anywhere in the end of the end, then ArrayList provides an optional collection. However, if you want to add and remove elements from the middle of the list, as long as the order of access list elements, the LinkedList is better.
"ArrayList and LinkedList implement the Cloneable interface, all offer two constructor, one uncomfortable, one accept another Collection"
2.3.1. LinkedList class
The LinkedList class adds some methods of processing two end elements.
(1) Void AddFirst (Object O): Add the object o to the beginning of the list
Void AddLast (Object O): Add the object o to the end of the list
(2) Object getFirst (): Return to the element starting
Object getlast (): Returns the elements ending at the list
(3) Object removefirst (): Delete and return the elements starting at the list
Object removelast (): Delete and return the elements ending the list
(4) LinkedList (): Build a list of empty links
LinkedList (Collection C): Build a list of linked lists and add all elements of the collection C
"Use these new methods, you can easily treat LinkedList as a stack, queue, or other-oriented data structure. "
2.3.2. ArrayList class
The ArrayList class encapsulates a dynamic redistributed Object [] array. Each ArrayList object has a Capacity. This Capacity indicates the capacity of the array of elements in the store. When the element is added to arraylist, its Capacity is automatically increased within a constant time.
In a program that adds a large number of elements to an ArrayList object, you can add Capacity using the EnSureCapacity method. This reduces the increase of the number of redistributions.
(1) Void EnSureCapacity: Adds ArrayList object capacity to MINCAPACITY
(2) Void Trimtosize (): Solving the ArrayList object capacity is the current size of the list. The program can use this operation to reduce ArrayList object storage space.
2.3.2.1. Randomaccess interface
A feature interface. This interface does not have any methods, but you can use this interface to test whether a collection supports valid random access. ArrayList and VECTOR class are used to implement the interface.
3.SET interface
The SET interface inherits the Collection interface, and it does not allow a replica in the collection, and each particular SET implements the equivalent of the class-dependent Equals () method to check the uniqueness. The SET interface does not introduce a new method, so set is a collection, but its behavior is different. 3.1. Hash table
The Hash table is a data structure for finding an object. The Hash table calculates an integer for each object, called the Hash Code (Hash code). The Hash table is an array of a link list. Each list is called a buckets (hash). Calculation of object position Index = hashcode% buckets (hashcode is the object hash code, BUCKETS is the total number of hash tables).
When you add an element, sometimes you will encounter a hash dollar that has already filled the element, which is called Hash Collisions. At this time, you must determine if the element has already exist in the hash table.
If the hash code is reasonably randomly distributed, and the number of hash tables is large enough, then the number of hash conflicts will decrease. At the same time, you can also better control the running of the hash table by setting an initial number of hash tables. The number of initial hash tables is buckets = size * 150% 1 (size is the number of expected elements).
If the elements in the hash table are too full, it must be rehashing (another hash). Then, the hash of the hash of the hash, and retransmit the original object into the new hash table, and the original hash table is deleted. The Load Factor decides when to have a hash of the hash table. In the Java programming language, the load factor default is 0.75, and the default hash table is 101.
3.2. Comparable interface and Comparator interface
There are two comparison interfaces in the Collection Framework: Comparable interface and Comparator interface. Java build classes such as String and Integer implements the Comparable interface to provide certain sorting methods, but this can only be implemented once. For classes that do not implement the COMPARABLE interface, or custom classes, you can define your own comparison by the Comparator interface.
3.2.1. Comparable interface
In the java.lang package, the Comparable interface is suitable for a class with natural sequence. Assuming that the object collection is the same type, which allows you to sort a collection into natural sequence.
(1) INT COMPARETO (Object O): Compare the current instance object with the object O, returns a negative value before located in the object O, if the two objects are the same in the sort, then return 0, if located behind the object O, return Positive value
Twenty-four classes in Java 2 SDK implementation Comparable interface. The following table shows the natural sort of eight basic types. Although some categories share the same natural sorting, only the class is more than one class.
class
Sort
BigDecimal, Biginteger, Byte, Double, Float, Integer, Long, Short
Sort by digital size
Character
Sort by numbers in the unicode value
String
Sort by character unicode value in strings
Create your own sort order using the Comparable interface, just implement the compareto () method. Usually, depending on natural sorting of several data members. At the same time, class should also override Equals () and havehcode () to ensure that two equal objects return to the same hash code.
3.2.2. Comparator interface
If a class cannot be used to implement java.lang.comparable, or you don't like the default Comparable behavior and want to provide your own sort order (possibly multiple sorting methods), you can implement a comparator interface to define a comparator.
(1) INT COMPARE (Object O1, Object O2): Compare two objects O1 and O2, if O1 is located in front of O2, return negative, if it is considered to be the same in the sort order, return 0 If the O1 is behind O2, the return positive value "is similar to the Comparable, the 0 return value does not represent the elements." One 0 return value is only the same position in two objects. How to deal with the COMPARATOR user. If the result of the two unequal elements is zero, you should first be confident that it is the result you want, then record behavior. "
(2) Boolean Equals (Object Obj): Indicates whether the object OBJ is equal to the comparator.
"This method overwrites the Object's equals () method, checking the equality of the Comparator implementation, not an object in a comparison state. "
3.3. Sortedset interface
"Collection Framework" provides a special SET interface: sortedset, which keeps the elements' ordered order. The SortedSet interface is set to the view (subset) and its both ends (ie, head and tail) provide an access method. Change the view will reflect the source set when you process the subset of the list. In addition, the change source set is also reflected in the subset. This happens that the view is specified by the elements of the two ends instead of the subscript element, so if you want a special high-end element (TOEELEMENT) in subset, you must find the next element.
Adding an element that is added to the sortedset implementation class must implement the Comparable interface, otherwise you must provide a constructor to provide a COMPARATOR interface implementation. The Treeset class is its only implementation.
"Because the set must contain unique items, if the two elements are compared, the two elements have caused the two elements (by comparable COMPARETO () method or Compare () method), then the new element is not added. If the two elements are equal, it is ok. But if they are not equal, you should modify the comparison method to make the comparison method and equals (). "
(1) Comparator Comparator (): Returns the comparator used when sorting the element, returns NULL if the CompareTo () method of the Comparable interface is compared
(2) Object first (): Return the first (minimum) element in an ordered collection
(3) Object last (): Return the last (highest) element in an ordered collection
(4) Sorted Subset (Object Fromelement): Returns the sortedset view (subset) (subset) (subset) (subset) (subset) (subset) (subset) (subset) (subset) (subset) (subset) (subset) (subset)
(5) SortedSet headset: Returns a view of the sortedset, each element is less than TOEEEMENT
(6) SortedSet Tailset: Returns a view of the sortedset, each element is greater than or equal to FromenElement
3.4. AbstractSet Abstract Class
The AbstractSet class covers the Object class equals () and hashcode () methods to ensure that the two equal sets return the same hash code. These two sets, etc. if the two sets are equal and including the same elements. According to the definition, the hash code is the sum of the concentrated element hash code. Therefore, regardless of the intended internal order, two equal gatherings have the same hash code.
3.4.1. Object class
(1) Boolean Equals (Object Obj): Compare two objects to determine if they are the same
(2) Int hashcode (): Returns the hash code of the object. The same object must return the same hash code 3.5. Hashset class, and Treeset classes
"Collection Framework" supports two common implementations of the SET interface: Hashset and Treeset (TreeSet implementation SortedSet). In more cases, you will use the Hashset to store a collection of repeating freedom. Taking into account the efficiency, the object added to the Hashset needs to implement the HashCode () method in a way that is properly assigned a hash code. Although most system classes cover the default HashCode () and Equals () implementations in Object, don't forget to overwrite HashCode () and Equals () when you create a class that you want to add to the Hashset.
The TreeSet implementation is useful when you want to insert and extract elements in an orderly manner from a collection. In order to be smooth, the elements added to the Treeset must be sorted.
3.5.1.hashset class
(1) Hashset (): Build an empty hash collection
(2) Hashset (Collection C): Build a hash set and add all the elements in the collection C
(3) Hashset (int initialcapacity): Build an empty haveh collection with specific capacity
(4) Hashset (int initialcapacity, float loadfactor): Build a holiday set with specific capacity and load factors. LoadFactor is a number between 0.0 to 1.0
3.5.2. Treeset class
(1) Treset (): Build an empty tree set
(2) Treeset: Build a tree set and add all elements in the collection C.
(3) Treeset: Build a tree set and sort the elements using a specific comparator
"The Comparator comparator does not have any data, it is just a memory of the comparison method. This object is sometimes referred to as a function object. Function objects are typically defined as an instance of an anonymous internal class in the "running process". "
Treeset (SortedSet S):
Build a tree set, add an orderly collection
s
All elements and use with ordered collection
s
Sort by the same comparator