Two types of Java container classes list and set analysis

Source: Internet
Author: User
Tags xms

The container class can greatly improve the programming efficiency and programming ability, in JAVA2, all the containers are redesigned by Sun's Joshua Bloch, enriching the function of the container class library.

The purpose of the Java2 container class library is "Save Object", which is divided into two categories:

Collection----A set of independent elements that are usually subject to some sort of rule. The list must maintain an element-specific order, and set cannot have duplicate elements.

MAP----A pair of "key-value pairs" objects, that is, its elements are paired objects, the most typical application is a data dictionary, and there are other widely used. In addition, map can return a set of all its keys and all its values, or a set consisting of its key-value pairs, and can also extend a multidimensional map like an array, as long as each "value" of the key-value pairs in the map is a map. Collection.

1. iterators

An iterator is a design pattern that is an object that can traverse and select objects in a sequence, and the developer does not need to know the underlying structure of the sequence. Iterators are often referred to as "lightweight" objects because they are less expensive to create.

The iterator functionality in Java is relatively simple and can only be moved one way:

(1) Use method iterator () requires the container to return a iterator. The first time you call Iterator's next () method, it returns the first element of a sequence.

(2) Use Next () to get the next element in the sequence.

(3) Use Hasnext () to check if there are elements in the sequence.

(4) use remove () to delete the newly returned element of the iterator.

Iterator is the simplest implementation of Java iterators, with more functionality for the listiterator of list design, which can traverse the list in two directions or insert and delete elements from a list.

function method of 2.List

List (interface): Order is the most important feature of the list; It ensures that elements are maintained in a specific order. The list adds a number of methods to collection, allowing you to insert and remove elements to the middle of the list (only recommended for linkedlist use). A list can generate Listiterator, which can be used to traverse a list in two directions or to insert and delete elements from the middle of a list.

ArrayList: A list implemented by an array. It allows for fast random access to elements, but it is slow to insert and remove elements into the list. Listiterator should only be used to traverse the ArrayList backward, rather than to insert and delete elements, as this is much larger than the linkedlist overhead.

LinkedList: Sequential access is optimized to insert and delete in the middle of the list with little overhead and random access is relatively slow (available ArrayList instead). It has methods AddFirst (), AddLast (), GetFirst (), GetLast (), Removefirst (), Removelast (), and these methods (not defined in any interface or base class) Enables LinkedList to be used as stacks, queues, and bidirectional queues.

function method of 3.Set

Set (interface): Each element that is stored in the set must be unique because set does not save duplicate elements. The object that joins the set must define the Equals () method to ensure the uniqueness of the object. The set has exactly the same interface as the collection. The set interface does not guarantee the order in which elements are maintained.

HashSet: Set designed for quick find. The object that is deposited into the hashset must define HASHCODE ().

TreeSet: The set of the hold order, the bottom is the tree structure. Use it to extract ordered sequences from the set.

Linkedhashset: has hashset query speed, and internally uses the chain list to maintain the order of elements (the Order of insertions). The result is displayed in the order in which the elements are inserted when iterating through the set using Iterators.

HashSet uses a hash function to sort the elements, which is specifically designed for quick queries, treeset the data structure of the red-black tree, and linkedhashset internally uses hashing to speed up the query, while maintaining the order of the elements using the linked list, Makes it seem that elements are saved in the order in which they are inserted. It is important to note that when generating your own classes, the set needs to maintain the order in which the elements are stored, so implement the comparable interface and define the CompareTo () method.

Java heap size determines the frequency and speed of garbage collection, the larger the Java heap, the lower the frequency of garbage collection, the slower the speed. Similarly, the smaller the Java heap, the higher the frequency of garbage collection, and the faster it gets. To set the ideal parameters, you still need to know some basic knowledge. The maximum value of the Java heap cannot be too large, which can cause system memory to be exchanged and paged frequently. Therefore, the maximum memory must be less than the physical memory minus the memory required by other applications and processes. and heap settings too large, resulting in garbage collection time is too long, this will not outweigh the loss, greatly affect the performance of the program. Here are some of the frequently used parameter settings:

1) Set the value of-xms equal to-XMX;

2) estimate the size of the space occupied by the surviving objects in memory, set-XMS equal to this value,-xmx four times times the value;

3) Set-xms equal to-xmx 1/2 size;

4) Set the-xms between 1/10 and 1/4-xmx;

5) Use the default settings.

You need to determine the most appropriate parameter settings for your own running program based on the specific usage scenario. In addition to the two most important parameters of-XMS and-xmx, there are a number of parameters that are likely to be used, which are often strongly dependent on garbage collection algorithms, and may vary depending on the version of the JDK and the manufacturer. But these parameters are generally used in web development less, I do not do a detailed introduction. In practical applications, it is important to set-XMS and-xmx so that they can optimize the application as much as possible. For applications with high performance requirements, it is necessary to study the mechanism of Java Virtual machine and garbage collection algorithm.


Two types of Java container classes list and set analysis

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.