What is the difference between ArrayList and rule list? Rule List
1. General differences between ArrayList and rule list
(1). ArrayList is a dynamic array-based data structure. The ArrayList is based on the linked list data structure.
ArrayList: elementData [size] storage. The value increases dynamically by 50%, which may cause a waste of memory.
Worker list: Create a Node object (save data, previous, next) based on the size, which consumes memory space.
(2). For random access to get and set, ArrayList thinks it is better than the sorted list because the sorted list needs to move the pointer.
-- Get (index)
ArrayList (OK): directly access the index array, elementData [index]
Sort list: determines that the index is smaller than the size, then from the first to next to the index Node. If the index is greater than the size, the Node from previous to index starts from the last one.
-- Set (index, newValue)
ArrayList (OK): elementData [index] = newValue.
Vertex list: locate the Node of the index through the Traversal method above, and then assign a value to newValue.
(3). For add and remove operations, LinedList is dominant because ArrayList needs to move data.
-- Add (obj), remove (obj): the performance of adding/deleting elements from the beginning is similar.
ArrayList (OK): elementData [size ++] = obj;
Sort list (OK): locate the last Node directly using the last identifier, and create a new Node to associate it with the last Node.
-- Instance:
Public class TestList {
Private static long test (List list ){
Long start = System. currentTimeMillis ();
For (int I = 0; I <50000; I ++)
List. add (new Object ());
Return System. currentTimeMillis ()-start;
}
Public static void main (String [] args ){
System. out. println ("ArrayList time consumed:" + test (new ArrayList ()));
System. out. println ("consumed list time:" + test (new consumed list ()));
}
}
-- Execution result:
ArrayList time consumption: 2
Consumed list time: 3
-- Add (index, obj), remove (index): add elements before.
ArrayList: Use System. arraycopy to create a new array, leave it blank at the index position, and then elementData [index] = element;
Sort list (OK): Find the Node of the index through the above method, and then associate obj with the Node of the index.
-- Instance:
List. add (0, new Object ());
-- Execution result:
ArrayList time consumed: 296
Consumed list time: 3
(4) Efficient random element access
ArrayList: implements RandomAccess
For (int I = 0, I <list. size (); I ++)
List. get (I );
Sorted list: each get (I) operation will traverse the page as shown in the preceding figure. Therefore, it is better to use it. next () to query the page quickly.
For (Iterator I = list. iterator (); I. hasNext ();)
I. next ();
2. Summary
ArrayList: add or delete the data that follows, query, support random access, and use for Traversal
Sort list: add or delete> previous or intermediate data, query> sequential access, and use iterator to traverse
When an operation adds data after a column of data instead of randomly accessing the elements, ArrayList provides better performance;
When you add or delete data in front or middle of a column of data and access the elements in the column in sequence, you should use the sort list.
Different ArrayList and rule list
ArrayList uses arrays to store objects. In this way, objects are placed in a continuous position, so the biggest drawback is that it is very troublesome to insert and delete objects.
The sorted list stores objects in independent spaces and stores the index of the next link in each space. However, it is very troublesome to search for the first index.
There are three important differences between the Hashtable and HashMap classes. The first difference is mainly due to the historical reasons. Hashtable is based on the obsolete Dictionary class, And HashMap is an implementation of the Map interface introduced by Java 1.2.
Perhaps the most important difference is that the Hashtable method is synchronous, while the HashMap method is not. This means that, although you can use a Hashtable in a multi-threaded application without taking any special actions, you must also provide external synchronization for a HashMap. A convenient method is to use the static synchronizedMap () method of the Collections class to create a thread-safe Map object and return it as an encapsulated object. The method of this object allows you to access the potential HashMap synchronously. The result is that when you do not need synchronization, you cannot cut off the synchronization in Hashtable (for example, in a single-threaded application), and synchronization increases a lot of processing costs.
The third difference is that only HashMap allows you to use a null value as the key or value of a table entry. Only one record in HashMap can be an empty key, but any number of entries can be empty values. That is to say, if no search key is found in the table, or if a search key is found but it is an empty value, get () returns null. If necessary, use the containKey () method to differentiate the two cases.
It is recommended that Hashtable be used when synchronization is required, and HashMap be used. However, when necessary, HashMap can be synchronized. HashMap has more functions than Hashtable, and it is not based on an old class. Some people think that, in various cases, HashMap takes precedence over Hashtable.
About Properties
Sometimes, you may want to use a hashtable to map the key string to the value string. There are some examples of Environment strings in DOS, Windows, and Unix. For example, the key string PATH is mapped to the value string C: \ WINDOWS; C: \ WINDOWS \ SYSTEM. Hashtables is a simple method to represent these, but Java provides another method.
The Java. util. Properties class is a subclass of Hashtable and is designed for String keys and values. The usage of Properties objects is similar to that of Hashtable, but the class adds two time-saving methods, you should know.
The Store () method saves the content of a Properties object in a readable form to a file. The Load () method is the opposite. It is used to read files and set Properties objects to include keys and values.
Note: Because Properties extends Hashtable, you can use the put () method of the super class to add keys and values that are not a String object. This is not desirable. In addition, if you use store () for a Properties object that does not contain a String object, store () will fail. As an alternative to put () and get (), you should use setProperty () and getProperty (). They use String parameters .... Remaining full text>
Different ArrayList and rule list
ArrayList uses arrays to store objects. In this way, objects are placed in a continuous position, so the biggest drawback is that it is very troublesome to insert and delete objects.
The sorted list stores objects in independent spaces and stores the index of the next link in each space. However, it is very troublesome to search for the first index.
There are three important differences between the Hashtable and HashMap classes. The first difference is mainly due to the historical reasons. Hashtable is based on the obsolete Dictionary class, And HashMap is an implementation of the Map interface introduced by Java 1.2.
Perhaps the most important difference is that the Hashtable method is synchronous, while the HashMap method is not. This means that, although you can use a Hashtable in a multi-threaded application without taking any special actions, you must also provide external synchronization for a HashMap. A convenient method is to use the static synchronizedMap () method of the Collections class to create a thread-safe Map object and return it as an encapsulated object. The method of this object allows you to access the potential HashMap synchronously. The result is that when you do not need synchronization, you cannot cut off the synchronization in Hashtable (for example, in a single-threaded application), and synchronization increases a lot of processing costs.
The third difference is that only HashMap allows you to use a null value as the key or value of a table entry. Only one record in HashMap can be an empty key, but any number of entries can be empty values. That is to say, if no search key is found in the table, or if a search key is found but it is an empty value, get () returns null. If necessary, use the containKey () method to differentiate the two cases.
It is recommended that Hashtable be used when synchronization is required, and HashMap be used. However, when necessary, HashMap can be synchronized. HashMap has more functions than Hashtable, and it is not based on an old class. Some people think that, in various cases, HashMap takes precedence over Hashtable.
About Properties
Sometimes, you may want to use a hashtable to map the key string to the value string. There are some examples of Environment strings in DOS, Windows, and Unix. For example, the key string PATH is mapped to the value string C: \ WINDOWS; C: \ WINDOWS \ SYSTEM. Hashtables is a simple method to represent these, but Java provides another method.
The Java. util. Properties class is a subclass of Hashtable and is designed for String keys and values. The usage of Properties objects is similar to that of Hashtable, but the class adds two time-saving methods, you should know.
The Store () method saves the content of a Properties object in a readable form to a file. The Load () method is the opposite. It is used to read files and set Properties objects to include keys and values.
Note: Because Properties extends Hashtable, you can use the put () method of the super class to add keys and values that are not a String object. This is not desirable. In addition, if you use store () for a Properties object that does not contain a String object, store () will fail. As an alternative to put () and get (), you should use setProperty () and getProperty (). They use String parameters .... Remaining full text>