Java Container Classes

Source: Internet
Author: User

Java container Classes-common concepts and common methods introduced, the implementation of the principle of analysis:
1, the array is stored the same type of fixed length of data, using subscript operators.
2, the container is another way to store data, you can dynamically store multiple objects, the underlying implementation involves a large number of data structure and algorithm content, so the principle part of the detailed analysis later.

Java Container class Framework
Reference Accessories

Container Common interface:
1, Collection: Defines the method of accessing the object, it has two commonly used sub-interfaces
List interface: Holds elements in order and allows elements to be duplicated
Set interface: Stored elements are unordered and do not allow duplication
2. Map interface: Defines a method for storing key-value pairs

Some basic features of the List:
Ordering: Refers to the order in which the elements are deposited and the order in which they are taken, and each element corresponds to an index.
Repeatability: Refers to the equality of results after the Equals method comparison.
Common methods:
Add (Object)
Add (Index,object)
Get (Index)
Remove (Index)
IndexOf (object)
Size (); Gets the number of container elements
Iterator (); Different list implementation classes will have different ITE inner classes. This enables the traversal of elements within the container

List interface specific Implementation class
ArrayList: A list container with an array structure for internal storage
LinkedList: Memory storage is a list container implemented using a doubly linked list structure.

Some basic features of Set
Disorder: There is no guarantee that the order of the elements is consistent with the order of withdrawal.
Repeatability: Two objects are compared after the Equials method is equal.

The specific implementation class for the set interface:
HashSet: Stored according to hash code, storing elements unordered, non-repeating
Linkedhashset: Storing elements in order
TreeSet: Storing with a tree-shaped list
For custom objects that are stored in the Set container, you should override the Equals and Hashcode methods as appropriate to improve Hashtable performance.
When a new object is added to the set, the hashcode is checked first, and if the hashcode is not equal, it is considered to be a different element.
When the hashcode is equal, the equal elements are compared according to the Equals method, and the equivalent is considered as repeating elements, no longer added, and the elements are added to the bucket (data block) corresponding to the hashcode.

Map related content:
stored in the same way as key-value pairs.
HASHMAP: The internal "key" is stored from set and cannot be duplicated.
TreeMap, HashTable

Put (Key,value)
Get (Key)
Remove (key)
Containkey (Key)
Containvalue (value)
KeySet ();//Returns the set of all keys in this map

Iterator iterators:
All container classes that implement the collection interface have a iterator () method that returns an object that implements the iterator interface
You can easily implement the traversal of elements within a container.
Hasnext (); Whether there is a next element
Next (); Returns the next element.

generic type in Container:
The concept of generics originates from templates, where the compiler generates classes of a particular type based on the type parameters passed in.
In Java, it is the container class that holds a particular type of object. When specifying a type, the default is the object type


This article is from the "Technical Life" blog, please be sure to keep this source http://6621180.blog.51cto.com/6611180/1923117

Java Container Classes

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.