"Reprint" Java Collection class array, List, map differences and links

Source: Internet
Author: User

The Java collection classes fall into three main categories:

First Category: Array, Arrays
Class II: Collection:list, Set
Category III: MAP:HASHMAP, HashTable

One, Array, Arrays

Array is the most efficient of all Java "storage and random access to a series of objects".

1, high efficiency, but the capacity is fixed and can not be changed dynamically.
One drawback of array is that it is not possible to determine how many elements are actually stored, and length simply tells us the capacity of the array.

2, Java has a arrays class, specifically used to manipulate the array.
Arrays has a set of static functions:
Equals (): Compares two arrays for equality. Array has the same number of elements, and all corresponding element 22 is equal.
Fill (): Fills the value into the array.
Sort (): Used to sort the array.
BinarySearch (): Looks for elements in a sorted array.
System.arraycopy (): Copy of Array.


Second, Collection, Map

If you do not know exactly how many objects you need to write your program, and you need to automatically expand capacity when you are running out of space, you need to use the Container class library, which is not applicable.

1. The difference between Collection and Map

The number of elements stored in the container is different.
collection type, with only one element per position.
Map type, holding Key-value pair, like a small database.

2, the sub-category of their respective relations

Collection
--list: Elements are stored in a specific order. So the order taken out may be different from the order in which it is put.
--arraylist/linkedlist/vector
--set: cannot contain duplicate elements
--hashset/treeset


Map
--hashmap
--HashTable
--TreeMap

3. Other Features

* List,set,map holds objects as Object type.
* Collection, List, Set, map are all interfaces and cannot be instantiated.
ArrayList, vectors, HashTable, and HashMap are inherited from them, and these can be instantiated.
* The vector container knows exactly what type of object it holds. Vectors do not perform boundary checks.


Third, collections

Collections is a helper class for the collection class. Provides a series of static methods for searching, sorting, threading, and so on for various collections.
A class--arrays equivalent to a similar operation on an array.
For example, Collections.max (Collection coll); Take the largest element in the Coll.
Collections.sort (list list); Sort elements in List

Iv. How to choose?

1, the difference between the container class and the array, playable
* The container class can only hold object references (pointers to objects), rather than copy the object information to a position in a sequence.
* Once the object is placed in the container, the object's type information is lost.

2.

* In various lists, the best practice is to use ArrayList as the default choice. When inserting and deleting frequently, use LinkedList ();
Vector is always slower than ArrayList, so try to avoid it.
* In various sets, hashset is usually better than hashtree (INSERT, find). Use TreeSet only if you need to produce a sorted sequence.
The only reason for the existence of Hashtree: the ability to maintain the ordering state of its elements.
* In a variety of maps
The HashMap is used for quick lookups.
* When the number of elements is fixed, use array, because array efficiency is the highest.

Conclusion: The most commonly used is arraylist,hashset,hashmap,array.


Attention:

1. Collection does not have a get () method to get an element. Elements can only be traversed by iterator ().
2. Set and collection have identical interfaces.
3, List, you can use the Get () method to remove one element at a time. Use numbers to select one of a bunch of objects, get (0) .... (Add/get)
4, generally use ArrayList. Use LinkedList to construct stack stacks, queue queues.

5, map with put (K,V)/get (k), you can also use ContainsKey ()/containsvalue () to check if it contains a key/value.
HashMap will use the object's hashcode to quickly find the key.
* Hashing
The hash code is the transformation of the object's information into a unique int value, stored in an array.
We all know that the array lookup speed is the fastest in all storage structures. So, you can speed up lookups.

When a collision occurs, let the array point to multiple values. That is, the array generates a table of cassia at each location.

6, the element in the map, you can extract the key sequence, the value sequence alone.
Use Keyset () to extract the key sequence and generate a set for all keys in the map.
Use values () to extract the value sequence and generate a collection for all values in the map.

Why a build set, a Build collection? That's because key is always unique, and value allows repetition.

(This article reproduced from the network, if infringement, please contact delete)

"Reprint" Java Collection class array, List, map differences and links

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.