Collection classes in Java

Source: Internet
Author: User
Java Collection class (version 1 )--

  • Why use a collection class?

When you do not know the number of data to be stored, or you need a more flexible method than the array subscript access mechanism, you need to use the collection class.

  • Understanding collection classes

The Collection class is stored in the Java. util package.
The Collection class stores object references instead of objects. For convenient expression, we call the objects in the Set refer to the reference of objects in the set ).
Set types include set, list, and map ).

(1) Set
Set is the simplest kind of set. Its objects are not sorted in a specific way, but simply add objects to the set, as if they were put in a pocket.
Access and operations to members in a set are performed through reference of the Set object, so there cannot be repeated objects in the set.
There are also multiple variants in the set to implement sorting and other functions, such as treeset, which adds objects to the set and then inserts them into the ordered Object Sequence according to some comparison rules. It implements the sortedset interface, that is, the method of adding object comparison. Through iteration of objects in a set, we can get a set of objects in ascending order.

(2) List
The main feature of a list is that its objects are stored in a linear manner, with no specific order. There is only one beginning and one end. Of course, it is different from a set with no order at all.
In the data structure, the list is represented by arrays and vectors, linked lists, stacks, and queues.
The collection classes of the implementation list are frequently used in our daily work and will be detailed in the following notes.

(3) ing
There is a significant difference between ing and set or list. Each item in the ing is paired. Each object stored in the ing has a related key object, which determines the storage location of the object in the ing. The corresponding keywords must be provided during object retrieval, just like a word in a dictionary. The keyword should be unique.
The keyword itself cannot determine the storage location of an object. It needs to process a hashing technique to generate an integer called hash code, hash code is usually used as a offset. The offset is relative to the starting position of the memory area allocated to the ing, and the storage location of the keyword/object pair is determined. Ideally, hash processing should generate evenly distributed values within a given range, and each keyword should obtain different hash codes.

  • Collection class Introduction

Java. util has 13 classes that can be used to manage collection objects. They support collection, list, or ing sets. The following is a brief introduction to these classes.

Set:
Hashset:
Use the implementation of a set of hashmap. Although the set definition is unordered, there must be some method to find an object quite efficiently. Using a hashmap object to store and retrieve a set is implemented within a fixed period of time.
Treeset:The implementation of object sorting sets in ascending order in a set. This means that obtaining the first iterator from a treeset object will provide the object in ascending order. The treeset class uses a treemap.
List:
Vector:Implement a table similar to an array and automatically increase the capacity to accommodate the elements you need. Using subscript to store and retrieve objects is just like in a standard array. You can also use an iterator to retrieve objects from a vector. Is Vector the only synchronization container class ?? The performance is good when two or more threads access the same time.
Stsck:This class is derived from the vector and adds the method implementation stack ?? A post-import, first-out storage structure.
Shortlist:Implement a linked list. The linked list defined by this class can also be used as a stack or queue.
Arraylist:Implement an array, which is variable in size and can be accessed like a linked list. It provides functions similar to the Vector class but is not synchronized.
Ing:
Hashtable:
To implement an image, all keys must be not empty. To work efficiently, the key-Defining class must implement the hashcode () method and equal () method. This class is an inheritance implemented by Java and can be better used in other classes that implement the image.
Hashmap:To implement an image, empty objects can be stored, and keys can be null (because keys must be unique, of course only one can be ).
Weakhashmap:To implement such an image, a key/object pair is usually discarded if it is no longer referenced by an object. In contrast to hashmap, the key in the image maintains the lifecycle of the key/object pair, although the program using the image does not reference the key and therefore cannot retrieve the object.
Treemap:To implement such an image, objects are listed in ascending order by buttons.

Both set and list are extended by the public interface collection, so they can be referenced using a variable of the collection type. This means that any list or set can be referenced in this way, except for the ing Class (but not completely excluded, because a list can be obtained from the ing .) Therefore, the standard way to pass a list or set to a method is to use collection parameters.

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.