Compare ArrayList and linkedlist__ databases in a database connection pool

Source: Internet
Author: User
Tags stringbuffer

In the JDBC link in our project, we typically use this stuff in the database connection pool, list

Database connection Pool
private linkedlist<connection> datasource = new Linkedlist<connection> ();

Why we must use this list LinkedList, personally think that the following third article is a good explanation of the problem.


1.ArrayList is based on the array, LinkedList based on the linked list implementation.
2. For random access get and set,arraylist feel better than LinkedList because linkedlist to move the pointer.
3. Add and Remove,linedlist are more advantageous for new and delete operations because ArrayList want to move the data.
4. Find Operation Indexof,lastindexof,contains and so on, the two are similar.
This is a theoretical analysis, in fact, not necessarily, such as ArrayList at the end of the insertion and deletion of data is not designed to move data, but still
There is a suggestion: random access more words must use ArrayList instead of LinkedList, if the need for frequent insertion and deletion should
Consider using LinkedList to improve performance.

If z references a string buffer object in which the current content is "start", this method invocation of Z.append ("le") causes the string buffer to contain "startle", while Z.insert (4, "le") changes the string buffer to include "starlet".

There is a certain capacity for each string buffer. You do not need to allocate a new internal buffer array as long as the string buffer contains a sequence of characters that does not exceed this capacity. This capacity is automatically increased if the internal buffer overflows. Starting with JDK 5, the class is supplemented by an equivalence class used by a single thread, that is, StringBuilder. As opposed to this class, the StringBuilder class should generally be used preferentially because it supports all the same operations, but is faster because it does not perform synchronization. For example, if SB references an instance of StringBuilder, Sb.append (x) and Sb.insert (Sb.length (), x) have the same effect. Java.lang.StringBuilder a mutable sequence of characters. This class provides an API that is compatible with StringBuffer, but does not guarantee synchronization. This class is designed as a simple replacement for stringbuffer, which is common when a string buffer is used by a single thread. If possible, it is recommended that this class be preferred, because in most implementations it is faster than StringBuffer. The main operations on StringBuilder are the Append and insert methods, which can be overloaded to accept any type of data. Each method effectively converts the given data to a string, and then appends or inserts the character of the string into the string builder. The Append method always adds these characters to the end of the generator, and the Insert method adds the characters at the specified point.

String/stringbuffer/stringbuilder, the performance of the string concatenation operation is enhanced in turn, I have done an experiment, 10,000 consecutive string operations, the three speed gap is amazing. It is best to use string when you know exactly what the invariant string is, because string in the Java language uses the "Flyweight" pattern, and there is only one string in the JVM. In determining the number of cycles, as far as possible to use for loop, in the loop nesting as far as possible to the outer layer, a small loop into the inner layers, this and VB and other languages just the opposite, after the code test, but do not know why. In short, in VB and other Microsoft series of languages with small sets of conventions faster, and in Java with large sets of small will be much faster. Of course, this refers to the time when the two can be replaced, such as two-dimensional array search and so on.

If the string is particularly long, it is time-consuming to take a charat character to get a particular location. Because each time you get an indexed character to cause a new retrieval process, it's a better idea to convert the string to a character array by calling the ToCharArray method, and then get the character at the specified position through the array index value.

For Boolean values, avoid unnecessary equality judgments the comparison of a Boolean value with a true is an identity operation (directly returns the value of the Boolean variable). There are at least 2 advantages to removing unnecessary operations for Boolean: 1 the code executes faster (the resulting byte code is 5 bytes short), and 2 the code is cleaner.

On the use of session issues, try not to put large objects into httpsession or other objects to be serialized, and pay attention to empty the session in time.

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.