Java Collection Class--a rare summary

Source: Internet
Author: User
Tags array sort comparable

This article is in the study summary, welcome reprint but please specify Source:http://blog.csdn.net/pistolove/article/details/41346969


The following information is summed up in the study, I hope you have some help. Please reprint if necessary, thank you.

1. StringBuffer thread safety, StringBuilder thread security is slightly more efficient
Set: Objects in a set are not arranged in any particular way, manipulate data by index value, and cannot have duplicate elements
List: Objects in a sequence are stored in a linear fashion, manipulating data by index values, and can have duplicate elements
Map: Each item of the map is a "name-value" pair, the name cannot be duplicated, the value can be repeated, and a name corresponds to a unique value

Displays the specified collection type: Does not experience the boxing process.
collection<taskvo> Resulte = dao.retrievebyclause (taskvo.class, SQL);
explicitly specifies the size of the returned array when converting to a group
Return Result.toarray (New Taskvo[result.size ()));
Arrays cannot be forced to transform:
String[] s= (string[]) List.toarray ();--Error
String[] s= (string[]) List.toarray (new String[list.size ()]);--Correct

Iterators are the standard mechanism for accessing every element in a collection by getting all of the objects in the collection in order, one by one.
Creation of iterators: The iterator () method of the collection interface returns a iterator
Iterator It=test.iterator (); To convert the Test collection object to an iterator
Common Methods for iterators:
Hasnext ()//Determine if there is a next element in the iterator
Next ()//returns the next element of the iteration
Remove ()//Removes the newly returned element of the iterator

The Iterator interface is shown Below:public interface Iterator {
Boolean hasnext ();
Object next ();
void Remove (); Optional
}
You can think of an iterator as a position that points between two elements.
When you call the Remove () method, you must call the next () method.
The Remove () method actually deletes the last returned element.

List Common methods:
void Add (int index, object Element): Add object element to position index
Boolean addall (int index, Collection Collection): Adds all the elements in the container Collection after the index position
Object get (int index): Remove the element with the position labeled Index
int IndexOf (object element): Find the first occurrence of the object element in the list
int LastIndexOf (object element): Find where the object element last appears in the list
Object Remove (int index): Deletes an element at the index position
Listiterator listiterator (int startIndex): Returns a Listiterator, starting at StartIndex
List sublist (int fromIndex, int toindex): Returns a list of sub-lists with elements stored as an element from FromIndex to Toindex


ArrayList: We can look at it as an array that can grow capacity automatically.
Returns an array using ArrayList's ToArray ().
Arrays.aslist () returns a list.
Iterators (Iterator) provide us with a common way to access the elements in a collection.


View mode:
One is to measure the time of an iterative element:
Another measure is to create an array using the ToArray call:
Finally get the result:
An array iteration element created using the ToArray call is about 30% to 60% faster than using iterator.
However, if you use the ToArray method to create an array, the time overhead is included. Using iterator is actually 10% to 20% faster, so we try not to create an intermediate array. Instead, use an iterative approach.

ArrayList automatic expansion mechanism
Implementation mechanism: arraylist.ensurecapacity (int mincapacity)
The length oldcapacity of the current Elementdata property is obtained first.
Then by judging the oldcapacity and mincapacity parameters who are large to decide whether to expand if mincapacity is greater than
Oldcapacity, then we will expand the current list object.
The strategy for scaling is: Take (oldcapacity * 3)/2 + 1 and mincapacity the larger one. Then use the array copy
method to transfer previously stored data to a new array object
If the mincapacity is not much more than oldcapacity, then do not expand.

LinkedList
The LinkedList is implemented using a bidirectional circular chain list.
Use LinkedList to implement stacks (stack), queue, and bidirectional queues (double-ended queue).
It has methods AddFirst (), AddLast (), GetFirst (), GetLast (), Removefirst (), Removelast (), and so on.

To implement a queue with LinkedList:
A queue is a linear table that qualifies all inserts to be made only at one end of the table, while all deletions are made at the other end of the table.
The one end of the table that allows insertion is called the tail of the queue (Rear), and the one end that allows deletion is called the team header (Front).
The operation of the queue is performed on a first in, out (FIFO) basis.
The physical storage of a queue can be stored in a sequential structure, or a chain-stored structure.

To implement the stack with LinkedList:
Stack is also a special linear table, which is a last-in, first-out (LIFO) structure.
A stack is a linear table that restricts insert and delete operations only at the end of the table, and the footer is called the top of the stack, and the table header is called the bottom of the stack (bottom).
The physical storage of stacks can be stored sequentially, or chained storage structures can be used.

Comparison of ArrayList and LinkedList:
The ArrayList is done with an array, while the LinkedList is done with a generic doubly linked list (double-linked list), where each object, in addition to the data itself, has two references, pointing to the previous element and the latter element, respectively.
If we often add elements at the beginning of the list, or insert and delete in the list, we should use LinkedList, otherwise the use of ArrayList will be faster.

Summary:
A collection is an object that holds other objects
The collection interface defines methods in addition to all collection classes that implement a mapped collection class
The list collection type describes a sequence of objects that store data by location.
ArrayList is a universal array for storing data in contiguous memory regions

Coding habits:
interface-oriented programming to minimize code change rates:
list<t> lst = new arraylist<t> ();
Try to avoid creating unnecessary objects: Vo should be created when the judgment is established
Taskreportvo Reportvos = new Taskreportvo ();
if (i > 0) {
Lstallvos.add (Reportvos); }
Try to avoid traversing and deleting collections at the same time. Because this will change the size of the set;
for (iterator<comtype> iter = Comlist.iterator (); Iter.hasnext ();) {
ComType com = Iter.next ();
if (!com.getname (). Contains ("abc")) {
Comlist.remove (COM);}
}

Recommended:
for (iterator<comtype> iter = Comlist.iterator (); Iter.hasnext ();) {
ComType com = Iter.next ();
if (!com.getname (). Contains ("abc")) {
Iter.remove (COM); }

Estimate the size of the objects held in the collection, cannot hold a large number of objects, Occupy memory:
list<t> lst = new arraylist<t> ();
The unlimited add element in LST is bound to cause the LST to occupy too high memory. Cause memory Overflow

Map interface:
The second type of interface tree for the collection frame.
It provides a mapping of a set of key values. Each object stored in it has a corresponding keyword (key), which determines where the object is stored in the map.
Keywords should be unique, and each key can only map one value.
Common methods:
Object put (object Key,object value): Used to hold a key-value pair in map
Object remove (Object key): Removes the key-value pair according to key (key), and returns the value
void Putall (Map mapping): Depositing elements from another map into the current map
void Clear (): empties the elements in the current map
Object get (Object key): Gets the corresponding value according to key (key)
Boolean ContainsKey (Object key): Determine if a key exists in the map (key)
Boolean Containsvalue (Object value): Determine if there is a value in the map (value)
Public set KeySet (): Returns all keys (key) and uses the set container to store
Public Collection VALUES (): Returns all values (value) and uses Collection to store
Public set EntrySet (): Returns an element set that implements the Map.entry interface

HASHMAP:
The MAP is primarily used to store key (key) value pairs, resulting in values based on keys, so the keys do not allow duplicates, but allow values to be duplicated.
HashMap is the most commonly used map, which stores data according to the hashcode value of the key, which can be obtained directly from the key, with fast access speed.
HashMap allows a maximum of one record's key to be null, allowing multiple records to have a value of NULL;
HashMap does not support thread synchronization, where multiple threads can write HashMap at any one time, and may result in inconsistent data. If synchronization is required, you can use the collections Synchronizedmap method to make the HashMap capable of synchronizing.

HASHMAP Implementation Principle---hashing
The meaning of hash hashing algorithm is to provide a method of fast access data, which uses an algorithm to establish the correspondence between the key value and the real value. Hash lists are also called hash tables. The basic idea of the hash table algorithm is that the key of the node is the independent variable, and the corresponding function value is calculated by a certain function relation (hash function), which is stored in the hash list address as the node.
When the elements in the hash table are too full, they must be hashed again, resulting in a new hash list, with all elements stored in the new hash list, and the original hash list deleted. In the Java language, you decide when to hash a hash list by load factor (factor). For example, if the load factor is 0.75, and the hash list already has 75% locations already full, then the hash will be hashed.
The higher the load factor (the closer to 1.0), the more efficient the use of memory, and the longer the element is looking for time. The lower the load factor (closer to 0.0), the less time it takes to find the element, and the more memory is wasted.

When do I need to rewrite equals?
When a class has its own unique "logical equality" concept (different from the concept of object identity);
The object class simply provides a comparison of references, and if two references are not the same then return false, which is not enough to satisfy the needs of most object comparisons, so overwrite;
Use the = = operator to check if an argument is a reference to an object "
Use the instanceof operator to check if an argument is the correct type
Convert the arguments to the correct type;
For each "critical" field in the class, check whether the field in the argument matches the corresponding field value in the current object. For a field that is neither a float nor a double type, the = = operator can be used for comparison, and for fields of object reference types, the Equals method of the referenced object can be called recursively, for fields of type float and double, Convert to a value of type int or long, and then use the = = operator to compare;
Once you've written the Equals method, you should ask yourself three questions: Is it symmetric, transitive, consistent? If the answer is no, then find out why these attributes are not met, and then modify the code of the Equals method

Equals () and hashcode () simultaneously overwrite
In particular, it is emphasized that the two methods should be overridden when an object is used as a key value (or index).
When you overwrite equals, two different instances may be logically equal, but depending on the Object.hashcode method, a different hash code is generated, and the "equal" object must have an equal hash code.
Cause, when you use one of them as a key to save to HashMap, hasotable or HashSet, and then "equal" to find another as the key value to find them, it is not found at all
Different types of hashcode values
If the field is Boolean, the calculation (f? 0:1)
If it is char,short,byte or int, calculate (int) F
If it is a long type, calculate (int) (f^ (f>>>32))
If the float type, calculate float.floattointbits (f)
If it is a double type, calculate Dobule.doubletolongbits (f)
If the field is an object reference, call Hashcode recursively
If the field is an array, each element is treated as a separate field, and a hash code is computed for each important element.
Comparison:
The order of HashMap is independent of the output order.
Linkedhashmap preserves the order in which key-value pairs are stored.
TreeMap is the ordering of the elements in the map.
Because HashMap and Linkedhashmap store data faster than the direct use of treemap, access efficiency is high.
Once all the elements have been stored, we will then sort the elements in the entire map. This can improve the efficiency of the whole program and shorten the execution time.
Note: The treemap is sorted according to the keys (key). And if we are going to use treemap for normal sorting, the object stored in Key must implement the comparable interface.

Set:
Extended Collection Interface
Duplicate elements are not allowed
Added restrictions to add (), Equals (), and Hashcode () methods
HashSet and TreeSet are the implementation of set
set-"HashSet Linkedhashset
sortedset-"TreeSet

HashSet Common methods:
Public Boolean contains (Object O): Returns True if set contains the specified element
Public Iterator Iterator () returns an iterator to the elements in the set
Public object[] ToArray (): Returns an array that contains all the elements in the set public object[] ToArray (object[] a): Returns an array that contains all the elements in the set, and the run-time type of the returned array is the run-time type of the specified array
Public boolean Add (Object O): If the specified element does not exist in the set, the set is added to the
public boolean remove (Object o): Removes from set if the specified element exists in the set
public boolean RemoveAll (Collection C): If the set contains the specified collection, all elements of the specified collection are removed from the set
public boolean Containsall (Collection C): Returns True if the set contains all the elements of the specified collection. If the specified collection is also a set and only a subset of the current set, the method returns True

The realization of the hashset of the set interface depends on the HashMap.
We should define HASHCODE () and Equals () for each object to be stored in the hash table.
How do I achieve the purpose of not having duplicate elements?
"Key" is the object we want to deposit, and "value" is a constant. This ensures that the information we need to store
Is the "key". The "key" is not duplicated in the map, which ensures that all the elements we have in the set are not duplicated.
HashSet How to filter repeating elements
Call element Hashcode Get hash code--"Determine whether the hash code is equal, not equal to the input
---"equal to determine whether equals () after equality, not equal in the hashcode input, equal not input

TreeSet:
TreeSet is to rely on treemap to achieve.
TreeSet is an ordered set, the elements in the TreeSet will be sorted in ascending order, and the default is to arrange in the natural sequence, which means that the elements in the TreeSet implement the comparable interface
We can pass a comparer object that implements the comparator interface when constructing a TreeSet object.

Comparison of several set:
HashSet iterates outside the member.
Members can be objects of any object subclass, but if the Equals method is overridden, the same
Be careful when modifying the Hashcode method.
TreeSet traversing members in an orderly manner;
Additional implementation of the SortedSet, supporting the order of the subset and other requirements of the operation
Members require implementation of the comparable interface, or use comparator constructs
TreeSet. Members are generally of the same type.
Linkedhashset outside the member by the insertion Order of the Members
Members are similar to HashSet members
HashSet is based on the hash algorithm, and its performance is usually better than treeset. We should usually use HashSet, we use treeset when we need to sort the functions.

The order in which hashset elements are stored has no relation to the order in which we add them, while Linkedhashset preserves the order in which elements are added. TreeSet is the ordering of the elements in our set.

In general, TreeSet implementations are useful when you want to extract elements in an orderly manner from a collection. To be able to proceed smoothly, the elements added to the TreeSet must be sortable. You also need to implement comparable interface support for class objects added to TreeSet. In general, it is quicker to add elements to the HashSet and then convert the collection to TreeSet for an orderly traversal.

Comparable and comparator
Comparable interface to provide a natural sort order.
For classes that do not have a natural order, or when you want a different order from the natural order, you can implement
Comparator interface to define your own sorting functions. You can pass comparator to Collections.sort or arrays.sort.

Comparator interface
When a class does not implement comparable, or does not like the default comaparable behavior. can implement comparator interface
Implement the comparator compare interface directly to complete the custom comparison class.
Example: Arrays.sort (Results, new comparator<repdataqueryresultvo> () array sort Repdataqueryexecutor
Example: Collections.sort (lst,new comparator<taskprintschemevo> ()


Java Collection Class--a rare summary

Related Article

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.