What is the difference between the collection classes of Java and their relationships?

Source: Internet
Author: User
Tags comparable

The collection classes used in the Java API are implemented Collection Interface , one of his class inheritance structures is as follows:

collection<--list<--vector Array to implement query fast, and delete slow

collection<-- list<--arraylist Array to implement query fast, and delete slow

collection<-- list<--linkedlist Linked list structure query slow, adding and deleting fast

collection<-- Set<--hashset Custom sorting to object implementation comparable rewrite CompareTo (object o);

collection<-- set<--hashset<--linkedhashset output in the order in which they are stored

collection<--Set<--sortedset<--treeset


Vector: array - based List, which actually encapsulates an array Some features that are not available are convenient for us to use, and it is impossible to go into the limit of Array. Performance is impossible .

beyond Array. So, if possible, we need to use Arraymore. Another important point is the vector "sychronized" , which is also a vector and

the only difference between the ArrayList.

ArrayList: like a Vector is a linked list based on an Array , but the difference is ArrayList are not synchronized. So the performance is better than the Vector , but

is when running into a multithreaded environment, you can need your own synchronization issues in managing threads.

linkedlist:linkedlist is different from the previous two lists, it is not based on Array , So it is not limited by the Array performance. Each nodecontains a two-party

content of the polygon:1. the data for the node itself ( Data ); 2. information for the next node (nextnode). So when adding to linkedlist , deleting the action doesn't have to be like

as with Array -based lists , a large amount of data movement is required. As long as you change The information about the NextNode can be achieved. This is The advantage of LinkedList.

List Summary:

1. all lists can contain only a single table of different types of objects, not a key- value pair. For example:[tom,1,c];

2. all lists can have the same elements, such as Vector can have [Tom,koo,too, Koo];

3. all lists can have null elements, for example [tom,null,1];

4. Array - based List(Vector,ArrayList ) for queries, and LinkedList (linked list) suitable for adding, deleting operations.

HashSet: Although the Set and List are implemented Collection Interface , but they are very different in how they are implemented. List Basically, they are all based on Array as a basis. But the Set is

on the basis of HashMap , this is the fundamental difference between Set and List . The HashSet is stored in the HashMap Key as set the corresponding storage item.

Collection, List set Map

Set unordered, elements cannot be duplicated

HashSet: unordered, element not duplicated, if custom entity object, need to override Hashcode () and Equals (object obj)

TreeSet: Custom sorting to object implementation comparable rewrite CompareTo (object o);

Map is stored as a key-value pair

HASHMAP: Auto-sort, key can be null

Linkedhashmap output in the order in which they are stored

Lists are ordered, subscript, and elements can be duplicated.

ArrayList "Focus"

Array to implement query fast, adding and deleting slow

jdk1.2 threads are unsafe and highly efficient

2) Vector

Array to implement query fast, adding and deleting slow

jdk1.0 thread safety, low efficiency

3) LinkedList

List structure query slow, delete

What is the difference between the collection classes of Java and their relationships?

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.