14th Chapter Collection Class

Source: Internet
Author: User

Some collection classes are provided in the Java.util package, which is also known as a container.

Refers to the container is not difficult to think of the array, the collection class and the array is different, the length of the array is fixed, the length of the set is variable;

Arrays are used to hold basic types of data, and collections are used to hold references to objects.

Common collections include a list collection, set set, and map collection, where list and set inherit the collection interface, and each interface provides different implementation classes.

1. Collection interface

The collection interface is the root interface in the hierarchy. The units that make up the collection are called elements. The collection interface is not typically used directly, but it provides a way to add elements, delete elements, and manage data.

2. List interface

The list interface inherits the collection interface, so it contains all the methods in the collection, and the list interface defines two important methods.

Get (int index): Gets the element at the specified index position.

Set (int index,object obj): Modifies the element of the specified index position in the collection to the specified object

Common implementation classes for the list interface: ArrayList and LinkedList:

The Arraylistl class implements a mutable array that allows all elements to be saved, including NULL, and provides quick random access to the collection based on the index location, with the disadvantage of inserting or deleting objects at the specified index location at a slower speed;

The LinkedList class uses the linked list structure to save the object. The advantage of this structure is that it makes it easier to insert and delete objects into the collection, and to insert and delete objects into the collection, the list collection implemented with the LinkedList class is more efficient, but for random access to objects in the collection, using the LinkedList class to implement the list collection is less efficient.

3. Set interface

The objects in the set collection are not sorted in a particular way, but simply add the object to the collection, but the Set collection cannot contain duplicate objects. The set set consists of the implementation classes of the set interface and the set interface.

The common implementation classes for set interfaces are: HashSet and TreeSet classes:

The HashSet class is supported by a hash table, which does not guarantee the set iteration order;

The set collection implemented by the TreeSet class increments the sort in natural order as it traverses the collection, or it can be sorted incrementally by the specified comparer.

4. Map interface

The Map collection provides a mapping of key to value. The map cannot contain the same key, and each key can only map one value. Key also determines where the storage object is stored in the map. But it is not determined by the key object itself, but by a "hashing technique", which produces an integer value of a hash code, which is usually used as an offset that corresponds to the starting position of the memory area assigned to the map, thus determining where the storage object is stored in the map.

The map collection includes the map interface and all the implementation classes of the map interface.

14th Chapter Collection Class

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.