Collection class overview

Source: Internet
Author: User

First look at the difference between the collection class and the array:
1. The collection class is Java's Extensible API class, and the array is the data structure of the Java language itself;
2. The number of objects stored in the collection is dynamically variable, while the size of the array is static;
3. Collections can only store objects, while arrays can store basic types of data, or objects;
4. Set use methods, such as add () or iterator (), to add or access objects, while the array uses subscript;
5. Collection exploring many methods can be invoked to perform various operations, while arrays have only a few methods to use.

The following figure lists the Java collection classes and their inheritance relationships (figure from http://baike.baidu.com/picview/1848852/1848852/0/263e802fe8883c231f3089b9.html# albumindex=0&picindex=0)

The collection interface defines the basic method of the collection class and is perfected by HashSet, ArrayList, and LinkedList. The purpose of the interface set and list is to divide the collection class into two classes of different functions, that is, set with duplicate elements and ArrayList and LinkedList that allow duplicate elements.

The collection class consists of two separate interfaces (collection and maps), but they have many of the same operations. The difference is in how the element is stored (the map saves the value in Key-value form). Interface Interpretation Collection defines the basic methods for Collection collection class operations, such as size (), IsEmpty (), contains (), add (), remove (), iterator (), etc. 15 methods. Inherited from the Iterable interface interface. Set defines the basic operation method of a collection class that does not allow repeating elements. Inherits all the methods defined in collection. The List defines the basic operation method of the collection class that allows repeating elements.  In addition to inheriting all of the methods defined in collection, an overloaded signature is defined for some methods and a new method is added. Map defines the basic methods of the Map collection class operation, such as ContainsKey (), Containsvalue (), keyset (), put (), values (), etc. 14 methods. It is itself a root interface.

The collection class of the Common Collection Interface collection The collection class to explain the collection class of the ARRAYLIST array structure. Its size can be adjusted automatically. Keep the location of the element added. Ordered and random access to elements is very effective, but it is less efficient to add elements at the end LinkedList is similar to ArrayList but applies a connection structure. It is very effective not to add and remove elements at the end of the HashSet the hash code is used to store the collection class without duplicate elements. Stored elements must refine the hashcode () method to ensure its uniqueness.

The Collection Class map collection class in the

  Common Map interface interprets HASHMAP similar to HashSet, but they belong to different interfaces. Stores no duplicate elements in the form of a key-value pair. A key can only reference one object value. TreeMap are similar to HashMap, but store elements in tree-like data structures. Elements must be stored in key-value form. A key can only reference one object value. Automatically sorted by object key.

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.