[Java Collection class] differences and connections between Array, List, and Map [summary]

Source: Internet
Author: User

Java Collection classes are divided into the following three types:

Category 1: Array, Arrays

Class 2: Collection: List, Set
Category 3: Map: HashMap, HashTable


I. Array, Arrays

Array is the most efficient method for Java's "Storage and random access to a series of objects.

1,
High efficiency, but the capacity is fixed and cannot be changed dynamically.
Another disadvantage of array is that it cannot determine the actual number of elements in the array. length only tells us the array capacity.

2. There isArrays class, used to operate the array. Arrays is a tool class in java. util. It contains various methods used to operate Arrays, such as sorting and searching. This class also contains a static factory that allows you to view arrays as a list

Arrays has a set of static functions,
Equals (): checks whether two arrays are equal. Array has the same number of elements, and all corresponding elements are equal to each other.
Fill (): Enter the value in array.
Sort (): used to sort arrays.
BinarySearch (): Search for elements in the sorted array.
System. arraycopy (): copying an array.


Ii. Collection and Map

If you do not know how many objects are needed when writing a program and need to automatically expand the capacity when the space is insufficient, you need to use the container class library. array is not applicable.

1. Differences between Collection and Map

The number of elements stored in each container is different.
Collection type, each location has only one element.
Map type, holding key-value pair, like a small database.

2. subcategories

Collection
-- List: stores elements in a specific order. Therefore, the order obtained may be different from the order placed.
-- ArrayList/Vector list/Vector
-- Set: duplicate elements cannot be contained.
-- HashSet/TreeSet
Map
-- HashMap
-- HashTable
-- TreeMap

3. Other features

* List, Set, and Map objects are regarded as Object types.
* Collection, List, Set, and Map are all interfaces and cannot be instantiated.
The ArrayList, Vector, HashTable, and HashMap inherited from them are concrete classes, which can be instantiated.
* The vector container knows exactly the type of the object it holds. Vector does not perform boundary check.


Iii. Collections

Collections is a help class for the Collection class.Provides a series of static methods for searching, sorting, and thread-based operations on various sets.
It is equivalent to Arrays, a class that performs similar operations on Arrays.
For example, Collections. max (Collection coll); obtains the largest element in coll.
Collections. sort (List list); sorts the elements in the list.

4. How to choose?

1. Differences between the container class and Array
* The container class can only hold the pointer from the object reference to the object), instead of copying the object information to a certain position in the series.
* Once an object is placed in a container, the type information of the object is lost.

2,
* Among various Lists, it is best to use ArrayList as the default choice. Use the sort list () When insertion or deletion is frequent ();
Vector is always slower than ArrayList, so avoid using it whenever possible.
* In various Sets, HashSet is generally better than HashTree insertion and search ). TreeSet is used only when a sorted sequence is generated.
The only reason for the existence of HashTree is that it can maintain the sorting status of its elements.
* In various Maps
HashMap is used for quick search.
* When the number of elements is fixed, Array is used because the Array efficiency is the highest.

Conclusion: ArrayList, HashSet, HashMap, and Array are commonly used.


Note:

1. Collection does not have the get () method to obtain an element. You can only use iterator () to traverse elements.
2. Set and Collection have the same interface.
3. List. You can use the get () method to retrieve an element at a time. Use numbers to select one of a bunch of objects, get (0 ).... (Add/get)
4. ArrayList is generally used. Use the queue list to construct the stack and queue.

5. Map uses put (k, v)/get (k) and containsKey ()/containsValue () to check whether a key/value exists.
HashMap uses the hashCode of the object to quickly find the key.
* Hashing
The hash code transforms the object information to form a unique int value, which is stored in an array.
We all know that the array search speed is the fastest in all storage structures. Therefore, the search can be accelerated.

When a collision occurs, let the array point to multiple values. That is, an orders table is generated at each position of the array.

6. Elements in Map can be extracted separately from the key sequence and value sequence.
Use keySet () to extract the key sequence and generate a Set for all the keys in the map.
Use values () to extract the value sequence and generate a Collection for all values in the map.

Why does one generate a Set and one generate a Collection? That's because the key is always unique and the value can be repeated.


This article from the "CEO Road" blog, please be sure to keep this source http://zhaohaibo.blog.51cto.com/7808533/1286475

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.