Java Common Collection Selection guide

Source: Internet
Author: User

Java collection in the selection of the time should choose that appropriate? A table that tells you how to choose a Java collection

Java collection classes

is thread safe

Whether the key value pair

Sort

Performance

Whether the element can be duplicated

Scaling strategy

List interface

ArrayList

Whether

Whether

Maintain the order of the time

High query performance, and the performance of adding and deleting elements is low

Is

1.5 times-fold expansion with an element greater than 100% of the current capacity

LinkedList

Whether

Whether

Have order

Low query performance, and high performance of elements and additions

Is

Vector

Is

Whether

Have order

Low

Is

Stack

Is

Whether

Have order, advanced after out

Low

Is

Map interface

HashMap

Whether

Is

Not sorted

Quick Insert and find

Key cannot be duplicated, value can be duplicated

Twice-fold expansion with an element greater than 75% of the current capacity

Hashtable

Is

Is

Not sorted

Low

Key cannot be duplicated, value can be duplicated

TreeMap

Whether

Is

Auto Sort by key

Quick Insert and find

Key cannot be duplicated, value can be duplicated

Set interface

HashSet

Whether

Whether

Not sorted

Quick Insert and find

Whether

TreeSet

Whether

Whether

Default ascending sort (return value by compare to interface)

Quick Insert and find

Whether

User guide:

1 in the case of the set scenario, specify the initialization capacity, because the capacity is not specified, the initial capacity is 10, each expansion will be a copy of the underlying array, consumption performance;

2 All sets the underlying storage is an array, the array can hold the base type, and the collection can only save objects

The 3 Java.lang.reflect.Array tool provides an array of operations; The Java.util.collection tool provides a collection of operations

4 HashMap can be synchronized with map M = Collections.synchronizedmap (HASHMAP)

Java Common Collection Selection guide

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.