collection classes in Java

Source: Internet
Author: User
Tags comparable int size set set

Function: A large number of objects in the operation of the temporary storage problem, because the array exists can not flexibly change the size of storage space, all have a set.

Note: The difference between an array and a collection: The array length is immutable, and the base data type is stored, the collection length is variable, only the object is stored

Because the data structure in the container is different, there are many kinds of containers, and the general function is extracted, and a set system is formed.

Collection frame:

Collection
List: Ordered (in the order in which they were deposited and in the order in which they were taken). Indexed. Allow repeating elements
linkedlist linked list structure and additions (relative to ArrayList)
ArrayList Array structure query fast (relative to LinkedList)
│└vector
│└stack
Set:, Access unordered. Duplicate elements are not allowed
Map
Hashtable hash table. is synchronous. Null key not allowed, null value
HashMap hash table, is out of sync, allow null key, null value
└treemap two fork tree, out of sync. You can sort keys

-----------------------------------------------------------------------

One, collection collection

A. Generic function 1, add boolean Add (Object obj),//Add Single Object Boolean AddAll (Collection c);//Add Collection 2, delete void clear ();//Clear All Boolean remove  (Object obj);  Boolean RemoveAll (Collection c);//Delete intersection of Boolean retainall (Collection c);//Preserve intersection 3, get length int size (), 4, Judge Boolean isEmpty ();  Boolean contains (Object O);  Boolean containsall (Collection c); 5, sets the set to an array of ToArray ();  ToArray ([]); 6, remove the element in the collection Interator iterator ()//Gets the iterator object of the iteration function on the element in the collection//iteration: One way to take out the element. Iterators: Objects with iterative capabilities, iterators do not need new, and are obtained using the iterator () method directly: Interator it = Coll.iterator ();//coll--Collection System.out.println (It.nex       T);    For (Iterator it=coll.iterator (); It.hasnext ();) {System.out.println (It.next ()); Note: For the internal class extraction of the different containers about the extracted elements, the implementation of an interface iterator includes: Next (), Hasnext (), remove (), the iterator is a public method B that takes out the collection collection element. unique methods in the list interface: Its unique approach is to support additions and deletions around the definition of the index. Added: Add (index,element), delete: Remove (index), change: Set (index,newelement), check: int indexOf (element); Element get (index);//The use of a collection object to manipulate elements at the same time, resulting in an iterative uncertainty//Resolution: During the iterative process, an iterator is used to-->list the unique iterator. Listiterator the iterator object via the method Listiterator () of the list collection note: Listiterator can be used to make additions and deletions during the iterative process. The specific subclasses and features of the C:list collection: Subclasses are differentiated because of the internal data structure (the way they are stored) |--vector: The data structure is an array. The array is variable-length (constantly new and duplicates the original array) |--arraylist: Also Array Structure, which is also variable in length. Thread is out of sync, instead of vector deletion speed relative to LinkedList, query speed |--linkedlist: Linked list structure, thread is not synchronized. Deletion speed is fast, query speed is slow D.set collection: Duplicate elements are not allowed. and collect    The Ion method is the same. Set collection method only iterators |--hashset: hash (hash) table structure,--> efficient than array query the container for storing elements and hash values corresponds to a hash table 1, which does not allow the storage of duplicate elements because of uncertainties.    2, there is no guarantee that the order of deposit and withdrawal is consistent. When the value of the two elements calculated by the hashing algorithm is the same, called the hash conflict--and after the conflict, the element needs to be further judged (judgment content, equals),--> Zipper method
Note: The hash table determines whether the element is the same: according to the Hashcode method, if the hash repeats (the hash value is the same), then the Equals method of the element is judged, if equals returns True, does not exist, returns false
.
How to ensure uniqueness: The element must overwrite the hashcode and Equals methods, and the Hashcode method is overridden to determine the hash value based on the element's own characteristics. Overriding the Equals method is to resolve a violation of the value of the SID. |-->linkedhashcode (subclass of Hashcode) |--treeset two-tree data structure, the elements can be sorted, the need for the non-synchronous element has a comparison function, you must use the Compareable interface to implement the Compareato method.
This is also the case in the requirements, where the elements have a comparison function that is not required, that is, to sort the elements in a sort order that does not follow the natural sort, but to store the elements in the TreeSet in the event that there is no comparison function-- You can then use the second comparison-to make the collection comparable.---The TreeSet constructor has a method that is directly compared by the comparator when the object is created
Implementing the Comparetor interface, overriding the Compare method, Comparetor the object of the interface, passing it as a parameter to the Tressset set, the constructor comparator is flexible and reduces the coupling of the object and the comparison method by two, MapSet map: Two-column collection, one-time pair, key-value team. To guarantee the uniqueness of the key, the value does not require a. Common function 1. Add v put (key,value);//If the same key is passed in, the previous value is returned and overwritten with the current incoming value 2. Delete void Clear (); v remove (key);//returns value while deleting 3. To determine the Boolean ContainsKey (Object key) Boolean Containsvalue (object value) Boolean IsEmpty (); 4. Get V Get ()
Tip: JDK1.2 later appear in the collection framework of the common sub-class objects, there are rules---prefix name is the data structure name, suffix is the name of the system name ArrayList: array structure, see the array to know the query fast, see list can be added and modified LinkedList: Linked list structure, adding and deleting fast. Xxxfirst xxxlasthashset: Hash table, element must overwrite Hashcode and equals method, query fast, not guaranteed orderly, see Set knows not to repeat Linkedhashset: Linked list + hash table, can achieve orderly treeset : Two fork tree, can be sorted--two kinds of comparison, one is natural sort compareable, one is comparator, Comparetor
5, traverse out map all values set<k> set = Map.getkeyset ();//Get all set set containing key for (K Key:set) {V value = Map.get (key);//Get Value by key }

Or:

Get EntrySet (set set of objects that hold the key and map mappings) using this mapping relationship to construct an iterator and get the keys and Values by Me.getkey (), Me.getvalue (), respectively.

Note: The tool class for the collection Framework-------------------------------------------------------------------------collections: All defined are static methods for manipulating collection 1. Sort List class Student implements Comparable{}public static <t extends comparable <? Super t>> void Sort (list<t> List) {} 2. Reverse Reverseorder3.max Min4. Two points Lookup 5. Turn the Unsynchronized collection into a synchronous collection-------------------------------------------------------------------------Arrays: The tool class method that is used to manipulate the array, the methods are static

collection classes in Java

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.