Java Foundation--Set analysis

Source: Internet
Author: User
Tags comparable

Java Collection

When does an array seem to be too weak to meet demand and need a collection class?

    1. Don't know the exact length of the data
    2. Need to sort automatically
    3. Store key value pairs

Of course, the above situation is not absolute, but the array is more difficult to meet. The collection class (also known as the Container Class) shows its powerful features.

Classification of collection classes (Pictures from http://biancheng.dnbcw.info/1000wen/359774.html)

Does not contain the queue content, the implementation class for some maps is not given.

Common uses are list, Set, map, and their implementation classes.

List, Set, and map interfaces and the characteristics of each implementation class

Interface

Characteristics

Implementation class

Implementing Class Attributes

Member Requirements

List

Linear, ordered storage container that accesses elements through an index

ArrayList

Array implementations. Non-synchronous.

Vector

Similar to ArrayList, synchronous.

LinkedList

Doubly linked list. Non-synchronous.

Map

Save a key-value pair member

HashMap

The implementation of Map interface based on hash table to meet the common requirements

Any of the object objects, if you modify the Equals method, you need to modify the Hashcode method at the same time

TreeMap

Sort by default according to natural order, or sort according to the comparator provided when the mapping is created

Key members require implementation of the Caparable interface, or use comparator to construct treemap. Key members are generally of the same type.

Linkedhashmap

Similar to HashMap, but the order in which "key-value pairs" are obtained when iterating through is the order in which they are inserted or least recently used

Same as HashMap

Identityhashmap

Hash map to compare "key value" with = = instead of equals ()

Members are judged equal by = =

Weakhashmap

Weak key mapping, which allows you to release the object that the map points to

Concurrenthashmap

Map of linear security

Set

Members cannot be duplicated

HashSet

Set for quick Find design

element must be defined hashcode ()

TreeSet

The set of the hold order, the bottom is the tree structure

Element must implement comparable interface

Linkedhashset

Internal use of the chain list to maintain the order of elements (inserted order)

element must be defined hashcode ()

In case of meeting the requirements, map should use Hashmap,set as far as possible using HashSet.

Basic use of collection classes

List

List basic operations

Comparison of efficiency between ArrayList and LinkedList

ArrayList the efficiency of adding elements

Execution Time Comparison

Number of executions (inserted at position No. No. 0)

ArrayList time (MS)

LinkedList Time (MS)

10000

31

0

20000

141

0

40000

484

16

80000

1985

0

160000

7906

0

320000

31719

16

Number of executions (insert at tail)

ArrayList time (MS)

LinkedList Time (MS)

10000

0

0

20000

15

0

40000

0

0

80000

0

0

160000

0

15

320000

0

16

Number of cyclic outputs (get (index) method)

ArrayList time (MS)

LinkedList Time (MS)

10000

93

204

20000

188

797

40000

328

2734

80000

38]

13328

160000

1594

62313

320000

2765

Too long ...

Because the ArrayList is implemented by an array, all elements of the list are moved when inserted at position No. 0, and no movement is required to insert the element at the end. LinkedList is a doubly linked list, and it takes the same amount of time to insert elements at any location. So you should use LinkedList to improve efficiency when there are more insertions and deletions in the list, while there are more index queries using ArrayList (using enhanced for loops or iterator traversal linkedlist efficiency will be much higher).

Arrays are a very common data structure, and most programs are related to arrays when you start to touch programming. Just beginning to touch Java is also always use the array to write some programs, and then more and more think that the array this thing can not meet the demand, then a "senior" to me said: Do not use the collection class is equal not learned java. Then we know that there is a collection class.

Think about 3, 4 years ago, time is like fleeting ah.

When does an array seem to be too weak to meet demand and need a collection class?

    1. Don't know the exact length of the data
    2. Need to sort automatically
    3. Store key value pairs

Of course, the above situation is not absolute, but the array is more difficult to meet. The collection class (also known as the Container Class) shows its powerful features.

Classification of collection classes (Pictures from http://biancheng.dnbcw.info/1000wen/359774.html)

Does not contain the queue content, the implementation class for some maps is not given.

Common uses are list, Set, map, and their implementation classes.

List, Set, and map interfaces and the characteristics of each implementation class

Interface

Characteristics

Implementation class

Implementing Class Attributes

Member Requirements

List

Linear, ordered storage container that accesses elements through an index

ArrayList

Array implementations. Non-synchronous.

Vector

Similar to ArrayList, synchronous.

LinkedList

Doubly linked list. Non-synchronous.

Map

Save a key-value pair member

HashMap

The implementation of Map interface based on hash table to meet the common requirements

Any of the object objects, if you modify the Equals method, you need to modify the Hashcode method at the same time

TreeMap

Sort by default according to natural order, or sort according to the comparator provided when the mapping is created

Key members require implementation of the Caparable interface, or use comparator to construct treemap. Key members are generally of the same type.

Linkedhashmap

Similar to HashMap, but the order in which "key-value pairs" are obtained when iterating through is the order in which they are inserted or least recently used

Same as HashMap

Identityhashmap

Hash map to compare "key value" with = = instead of equals ()

Members are judged equal by = =

Weakhashmap

Weak key mapping, which allows you to release the object that the map points to

Concurrenthashmap

Map of linear security

Set

Members cannot be duplicated

HashSet

Set for quick Find design

element must be defined hashcode ()

TreeSet

The set of the hold order, the bottom is the tree structure

Element must implement comparable interface

Linkedhashset

Internal use of the chain list to maintain the order of elements (inserted order)

element must be defined hashcode ()

In case of meeting the requirements, map should use Hashmap,set as far as possible using HashSet.

Basic use of collection classes

List

List basic operations

Comparison of efficiency between ArrayList and LinkedList

ArrayList the efficiency of adding elements

Execution Time Comparison

Number of executions (inserted at position No. No. 0)

ArrayList time (MS)

LinkedList Time (MS)

10000

31

0

20000

141

0

40000

484

16

80000

1985

0

160000

7906

0

320000

31719

16

Number of executions (insert at tail)

ArrayList time (MS)

LinkedList Time (MS)

10000

0

0

20000

15

0

40000

0

0

80000

0

0

160000

0

15

320000

0

16

Number of cyclic outputs (get (index) method)

ArrayList time (MS)

LinkedList Time (MS)

10000

93

204

20000

188

797

40000

328

2734

80000

688

13328

160000

1594

62313

320000

2765

Too long ...

Because the ArrayList is implemented by an array, all elements of the list are moved when inserted at position No. 0, and no movement is required to insert the element at the end. LinkedList is a doubly linked list, and it takes the same amount of time to insert elements at any location. So you should use LinkedList to improve efficiency when there are more insertions and deletions in the list, while there are more index queries using ArrayList (using enhanced for loops or iterator traversal linkedlist efficiency will be much higher).

Java Foundation--Set analysis

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.