Learning and understanding of containers in Java

Source: Internet
Author: User

In the past has been the concept of Java containers do not understand, although learned, but never seriously understand, these days the teacher raised such a question, how do you understand the container in Java. The moment was blindfolded. So a variety of search materials to learn a bit, the following is my study after the collation of some of the experience. Welcome to the great gods of the road

When writing programs, we often need to manage a large number of object references . To achieve effective collation management, we often place similar references in the same data container .

the composition of the Java container

1, Collection Interface : Defines a method to access a set of objects, his sub-interface set and list respectively defines the access method. data Objects in set have no order and cannot be duplicated ;the data objects in the list are sequential and repeatable. The list is subdivided into LinkedList and ArrayList,LinkedList accesses the data in a linked list , andArrayList stores the data in an array way .

Collection interface-defined methods
return value Method name (parameter type parameter) Describe
Int Size () Number of objects in the container
Boolean IsEmpty () is empty
void Clear () Empty
Boolean Contains (Object element) is not a containing element object
Boolean Add (Object Element) Add an Element object
Boolean Remove (Object Element) remove Element Object
Iterator Iterator () Returns a iterator object that iterates through the objects in the container
Boolean Containsall (Collection c) Whether to include all objects in the C container
Boolean AddAll (Collection c) Add all the objects in the C container to the container
Boolean RemoveAll (Collection c) Remove all objects that exist in the C container from the container
Boolean Retainall (Collection c) To find the intersection of the current collection class and the C container
Object[] ToArray () Converts all objects in a container to the corresponding array

    (1) List: The elements in the list container are not in order and can be duplicated. The elements in the list container correspond to an integer ordinal that records its position in the container.

       1.LinkedList: Its data structure is linked list, the advantage of this structure is that it is efficient to delete and add, but the efficiency of random access element is lower than ArrayList class.

       2.ArrayList: Its data structure uses a linear table, the advantage of this structure is access and query is very convenient, but added and deleted when the efficiency is very low.

    (2) Set: The set interface does not provide an additional method, but the elements in the container class that implement the set interface are not sequential and cannot be duplicated. A set container is similar to the concept of a collection in mathematics.

       1.HashSet: The set class does not allow duplicate elements (sets), and cannot add a repeating element. HashMap uses the hash function to optimize the query efficiency, in which the contain () method is often called to determine if the relevant element has been added.

  2. Map Interface: Map provides a way to store data in a key-value pair (the key and the value are one by one corresponding)

(1)HashMap: provides the key-value data storage mechanism of key-value pairs, it is convenient to find the corresponding elements through the key value, and through the hash hashing mechanism, find very convenient.

  3. Iterator interface: All containers that implement the collection interface have a iterator method for returning an object that implements the iterator. The iterator object is an iterator that implements the traversal of elements in the container.

A container is like a warehouse where you can store your stuff in an unrestricted warehouse, and then take it out when you need it! Concept: Break through the fixed size of the array you can store objects without knowing how many objects are needed or in a more complex way, and present the concept of containers! List,set,queue and map These object types are referred to as collection classes, because the Java class Library uses the collection name to represent a special subset of the class library! So use a wider range of terms "containers" to call them! The advantage of a container over an array is that it can automatically adjust the size of the subset. Don't worry about how big it should be set! If the object stored in the container is not declared to inherit from which class, then it is automatically inherited from object, so when you take the object of the container, you get only the application of object, you must transform it to the type you want!

Learning and understanding of containers in Java

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.