Java Basics Point Grooming 3

Source: Internet
Author: User

Collection Frame

Characteristics:

1 : Objects encapsulate data, and objects need to be stored too. collection is used to store objects.

2 : The number of objects determines the array can be used, but is not sure what to do? You can use collections. Because the collection is variable-length.

The difference between a collection and an array:

1 : An array of fixed-length, variable-length sets.

2 : Arrays can store basic data types, or they can store reference data types, and collections can store only reference data types.

3 : The elements stored in an array must be of the same data type, and the objects stored by the collection can be of different data types.

Data structure: is how the data is stored in the container.

For collection containers, there are many kinds. Because each container's own characteristics are different, the principle is that each container's internal data structure is different.

The collection container is in the process of continuous upward extraction. There is a collection system.

When using a system, the principle: see top-level content. establishes the underlying object.


--< java.util >-- Collection Interface:

Collection :

|--list : Ordered (the order in which elements are stored in the collection is consistent with the order in which they are taken), and the elements are indexed. Elements can be duplicated.

|--set : Unordered (the deposit and fetch order may be inconsistent), cannot store duplicate elements. Element uniqueness must be guaranteed.

1 , add:

Add (Object) : Add a single element

AddAll (Collection) : Adds all the elements in a collection.

2 , Delete:

Clear () : Removes all elements from the collection, emptying the collection .

Remove (obj) : Deletes the specified object from the collection. Note: The length of the collection will change if the deletion succeeds.

RemoveAll (collection) : Deletes some elements. Some elements are consistent with incoming collection.

3 , Judge:

Boolean contains (obj) : Whether the specified element is contained in the collection.

Booleancontainsall (Collection) : Contains the specified number of elements in the collection.

boolean isEmpty () : Whether there are elements in the collection.

4 , get:

int size () : There are several elements in the collection.

5 , take the intersection:

boolean Retainall (Collection) : The same elements in the current collection that are persisted and specified in the collection. Returns flase if the two collection elements are the same, or true if Retainall modifies the current collection.

6 to get all the elements in the collection:

Iterator Iterator () : iterators

7 , turn the collection into an array:

ToArray ();

List : Ordered (the order in which elements are stored in the collection is consistent with the order in which they are taken), and the elements are indexed. Elements can be duplicated.

|--arraylist : The underlying data structure is an array, the thread is out of sync, ArrayList replaces the vector, and the query element is fast.

|--linkedlist : The underlying data structure is linked list, the thread is not synchronized, the speed of adding and deleting elements is very fast.

|--vector : The underlying data structure is an array, thread synchronization, the vector regardless of query and additions and deletions are huge slow.

--< java.util >-- Set Interface :

Set The methods in the interface are consistent with the methods in the collection. The set interface is removed in only one way, an iterator .

|--hashset : The underlying data structure is a hash table, and threads are not synchronized . disorderly, efficient;

HashSet set guarantees element uniqueness : Done by the Hashcode method of the element, and by the Equals method.

When the hashcode value of the element is the same, it continues to determine whether the equals of the element is true.

If true, then the same element is considered not saved. If False, then store.

If the hashcode value is different, then equals is not judged, thus increasing the speed of object comparisons.

|--linkedhashset : Ordered, HashSet subclasses.

|--treeset : sorts the specified order of the elements in the set collection. different steps . TreeSet the underlying data structure is a binary tree.



--< java.util >-- Iterator Interface:

Iterator: is an interface. Function: Used to take an element in a collection.


Map Collection:

|--hashtable : The underlying is a hash table data structure that is thread-synchronized. You cannot store null keys, null values.

|--hashmap : The underlying is a hash table data structure that is not synchronized by threads. You can store null keys, null values. Replaced the Hashtable.

|--treemap : The bottom layer is a two-fork tree structure that allows you to sort the keys in the map collection in a specified order.

Map there is a big difference between collection storage and collection:

Collection save one element at a time; map saves a pair of elements at a time.

Collection is a single-column collection;

Map a pair of elements stored in: One is the key, one is the value, and the key has a corresponding (mapping) relationship to the value.

Features: To ensure the uniqueness of the keys in the map set.

1 , Add.

put(key,value) : When the stored key is the same, the new value replaces the old value and returns the old value. Returns NULL if the key is not duplicated.

void Putall(MAP);

2 , delete.

void Clear() : Empty

value remove(key) : Deletes the specified key.

3 , judged.

boolean isEmpty() :

boolean containskey(key) : Does it contain key

boolean containsvalue(value) : Contains value

4 , remove.

int size() : Return length

value get(key) : Gets the corresponding value by specifying the key. If NULL is returned, you can tell that the key does not exist. Of course there is a special case, that is, in the HashMap collection, the null key can be stored in the null value.

Collection Values() : gets all the values in the Map collection.

5 , you want to get all the elements in the map:

principle: There is no iterator in map, collection has an iterator, and as long as the map collection is turned into a set set, an iterator can be used. The reason why the set is transferred is because the map set has the uniqueness of the key, in fact, the set set is derived from the Map,set set at the bottom of its practical is the map method.



Java Basics Point Grooming 3

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.