Talking about Collection Collection

Source: Internet
Author: User

As the saying goes: A thing, one thing can not be separated from three words: "What is, why, how to do"

What the collection is:

A collection is simply a high-level representation of a collection of arrays used to store data or objects in a container;

Why the collection exists:

Collection only embodies the flexibility of data manipulation, greatly facilitates the efficiency of developers;

How the collection is used:

Use is divided into the following ways: Collection collection 1.list 2.Set

List

List Underlying data structure Inquire Deletions Thread Efficiency
ArrayList Array Fast Slow Not safe High
Vector Array Fast Slow Safety Low
LinkedList Linked list Slow Fast Not safe High

 

Set

Set Underlying data structure Ordered Only Sort
HashSet Hash table Whether Yes (dependent on hashcode () and Equals ()) --
Linkedhashset Linked lists and hash tables Is

Is

The chain list ensures that the elements are orderly
Guaranteed element unique by hash table

--
TreeSet Red and black Trees -- Is

A. Natural sorting

B. Comparator sequencing

(4) Overview of Collection features

List cn=new ArrayList ();
A: Add Features

Add (Object e);//Direct Natural sequential insertion

Add (index, Element);//Insert by index
B: Delete Feature

Remove (index);//Remove by index
C: Judging function

Cn.contains (object e)//Determine if element e exists in the CN set
D: Get Features

Cn.get (index)//Index Get value
E: Length function

Cn.size ()//return set length
F: Intersection (understanding)

Cn.retainall (Collection C)//cn intersection of C

CN to c intersection The return value is a Boolean value

If there is a intersection, the return is true. The value in the CN collection returns the intersection value

return False if no intersection
G: Turn the set into an array

Cn.toarray ()
(5) Traversal of the collection collection
A: Turn the set into an array (usually not used)

Iterate over a set to iterate over an array
B: Iterator (Collection-specific method)
Iterators

To create an iterator for a CN collection Object

Iterator It=cn.iteraror ();

while (It.hasnext ())

{System.out.println (It.next ())}
1: Is the way the collection gets the elements.
2: There is a dependency on the collection.
3: The principle and source of the iterator.
A: Why is the definition for an interface instead of an implementation class?

Simply put: no matter what kind of collection, we have to get the data in the collection, and in the acquisition before the function of the judgment, that is, before the acquisition of elements before the first judgment, then the collection has two functions (judgment, query). In this way, if the implementation class can only implement a certain set, but the interface can do, let the sub-class to implement the interface, so that the requirements are met;

(5) Concurrency modification exceptions
A: a phenomenon that appears
Iterators iterate through collections, collections modify collection elements
B: Reason
Iterators are dependent on collections, and the collection's change iterators are not known.
C: Solutions
A: Iterator traversal, iterator modification (listiterator)
Element is added at the location of the iteration just
B: Collection traversal, collection modification (size () and get ())
Element is added at the end of the collection
(6) Common data structures
A: Stack advanced back out
B: Queue FIFO
C: Array query fast, adding and deleting slow
D: List query slow, and delete fast

Talking about Collection Collection

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.