8.0Java Collection Class

Source: Internet
Author: User
Tags set set

---restore content starts---

A collection can be seen as a container, such as red clothes can be seen as a collection, and all Java classes of books can be seen as a collection. It is easy to store the objects in the collection in the collection, and it is easy to remove them from the collection, and they can be placed in a certain order. Different collection classes are available in Java, which have different ways of storing objects and provide a way for users to traverse, add, delete, and find the specified object for the collection. Be sure to learn to use collections.

Collection class overview

Some collection classes are provided in the Java language's Java.util package, which is also known as a container. Referring to a container is not difficult to think of an array, the collection class and the array is different, the length of the array is fixed, the length of the collection class is variable, the array is used to hold the basic type of data, the collection is used to hold the object's reference. The common collection has a list set, a set set, a Map collection, where list and set implements the collection interface. Each interface also provides a different implementation class. The inheritance relationship of the above collection classes:

  

Common methods of collection class interface

The collection interface is the root interface in the hierarchy. The units that make up collection are called elements. The collection interface is not typically used directly, but it provides a way to add and remove elements and manage data. Because both the list interface and the set interface implement the collection interface, these methods are common to the list collection and set set.

Common methods for list interfaces

The list interface inherits the collection interface, so it contains all the methods in the collection. Again, because list is the type of listing, all the list interfaces provide some common methods that are appropriate for you.

  

  

The method that is appropriate for itself in the list interface is related to the index. Because the list collection stores objects in the current manner, objects can be manipulated through the object's index. Note: The list collection can contain duplicate objects, such as the IndexOf () method to get the index position of the first occurrence of the duplicate object, and you can use the LastIndexOf () method if you want to get the index position of the last occurrence of the duplicate object. When using the indexof () and LastIndexOf () methods, if the specified object has only one in the list collection, the index position obtained by the two methods is the same.

Common methods for Set interfaces

The set set consists of the implementation classes of the set interface and the set interface. The set interface inherits the collection interface, so it contains all the methods of the collection interface. Common methods in the set interface are table:

  

  

Because the set collection does not allow duplicate classes, you can use the AddAll () method in the Set collection to add the collection collection to the Set collection and remove the duplicate values.

Note: The collection iterator concept

Common methods for map interfaces

The map interface provides an object that maps keys to values. A map cannot include duplicate keys, and each key can be mapped to at most one value. The map interface also provides common methods for collections, such as clear (), size (), and so on, including the methods shown in the table:

  

  

Because the elements in the map collection are stored by key, value, to get the specified key or value value in the collection collection, you need to get the key collection or value collection by way of a response, and then traverse the key collection or value collection to get the specified value.

Implementation class for the collection class interface

Implementation class for the list interface

To use the list collection, you typically declare the list type, and then instantiate the collection through the implementation class of the list interface. The implementation classes of the list interface are commonly used with arrylist and LinkedList.

1) ArrayList class

The class implements a mutable array that allows all elements, including null. Collections can be accessed quickly and immediately based on the index location. The disadvantage is that inserting or deleting objects at the specified index location is slow. Syntax format:

List<string>lsit = new arraylist<string> ();

2) LinkedList class

This class uses a linked list structure to hold objects. This structure is something that makes it easier to insert and delete objects into a collection, often when you need to insert and delete objects into a collection, the list collection implemented with the LinkedList class is more efficient, but for objects in the collection, using the LinkedList class to implement the list collection is slow. Syntax format:

list<string> list = new linkedlist<string> ();

When using the list collection, the class list type is usually declared, and the collection can be instantiated by different implementation classes.

Instantiate the list collection, respectively, through the ArrayList, LinkedList class. Create a gather class in your project, create a collection object in the Main method, get an element in the collection by using the random () method of the math class, and then overflow the element in the array at index position 2, and then iterate through the array.

Implementation class for the set interface

To use a set collection, you typically declare it as a set type and then instantiate it through the implementation class of the set interface. The implementation class for the set interface is typically used with the HashSet and TreeSet classes. The syntax format is:

Set<string>collset = new hashset<string> (); Set<string>collset2 = new treeset<string> ();

Because the objects in the set collection are unordered, the result of traversing the set collection is not the same as the order in which the set is inserted.

The common implementation classes of the map interface are HashMap and TreeMap. It is generally recommended to implement a map collection using the HashMap implementation class, because the map collection implemented by the HashMap class is more efficient for adding and deleting mappings. HashMap is the implementation of the map interface based on hash table, hashmap the mapping relation of its interior by the hash code, and the map collection with HashMap class is efficient for adding or removing mappings, and the mapping relationship in TreeMap has a certain order. If you want the objects in the Map collection to be in a certain order, you should implement the Map collection using the TreeMap class.

1) HashMap class

This class is based on the implementation of the map interface of the hash table, which provides all the optional mapping operations and allows NULL values and NULL keys, but must guarantee the uniqueness of the keys. HashMap a fast lookup of the mapping relationships within the hash code. This type of department guarantees the order of the mappings, and in particular does not guarantee that the order is permanent.

2) TreeMap class

This class not only implements the map interface, but also implements the class Java.util.sortedmap interface, so the mapping relationship in the collection has a certain order. But on the relationship of adding delete and locating mappings, the performance of the TreeMap analogy HashMap class is poor. Because the mappings in the map collection implemented by the TreeMap class are arranged according to the key object in a certain order, the key object is not allowed to be null.

You can create a map collection from HashMap, and then create an instance of the TreeMap class that completes the same mapping relationship when sequential output is required.

Iterators

Creation and use of iterators

An iterator is created with the iterator interface, and the iterator interface is located under the Java.util report. There are 3 methods in the iterator interface.

Hasnext (): Returns True if there are still elements that can be iterated.

Next (): Returns the next element of the iteration.

Remove (): Removes the last element returned by the iterator from the collection that the iterator points to (optional action).

Listiterator interface dedicated to the iterator interface.

Summary: Common collections in the Java language, including the list collection, set set, and map collection. The characteristics of each set should be mastered. A long grasp of the collection's traversal, the addition of objects, the method of deleting objects. This chapter presents a typical application for each collection. Collections are important parts of the Java language, and you should learn to use collection classes.

  

---restore content ends---

8.0Java 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.