1. The difference between ArrayList and vectors
These two classes all implement the list interface (the list interface inherits the collection interface), they are all ordered collections, that is, the location of the elements stored in these two collections are sequential, equivalent to a dynamic array, we can later by the location index number to take out an element, And the data in it is allowed to be duplicated, which is the largest difference from a collection such as HashSet, and a collection such as HashSet cannot retrieve its elements by index number, nor does it allow duplicate elements.
Then the difference between ArrayList and Vector is mentioned, which mainly includes two aspects:.
1) Vector methods are synchronous (Synchronized), is thread-safe (Thread-safe), and ArrayList method is not, because the synchronization of threads will inevitably affect performance, therefore, ArrayList performance is better than vector.
2) When an element in a vector or ArrayList exceeds its initial size, the vector doubles its capacity and ArrayList only increases its size by 50%, so that the ArrayList is useful for saving memory space.
Note: For Vector&arraylist, Hashtable&hashmap, to remember the problem of thread safety, remember that the vector and Hashtable are old, Java is a birth to provide, they are thread-safe, ArrayList and HashMap are provided at JAVA2, and they are thread insecure. So, we talk about the old first class.
2. The difference between HashMap and Hashtable
HashMap is a lightweight implementation of Hashtable (non-thread-safe implementation), they all complete the map interface, the main difference is that the HASHMAP allows null (NULL) key value (key), because of non-thread security, in the case of only one thread access, More efficient than Hashtable.
HashMap allows NULL to be used as a entry key or value, and Hashtable is not allowed.
HashMap hashtable contains method removed, changed to Containsvalue and ContainsKey. Because the contains method is easy to cause misunderstanding.
Hashtable inherits from the dictionary class, and HashMap is an implementation of the map interface introduced by Java1.2.
The biggest difference is that the Hashtable method is synchronize, and HashMap is not, when multiple threads access Hashtable, they do not need to synchronize their methods, and HashMap must provide external synchronization.
The Hash/rehash algorithms used by Hashtable and HashMap are probably the same, so there is no big difference in performance.
HashMap and Hashtable are mainly from three aspects.
A. Historical reason: Hashtable is based on the old dictionary class, HashMap is an implementation of the map interface introduced by Java 1.2
B. Synchronization: Hashtable is thread-safe, that is, synchronous, and HashMap is a thread program is not secure, not synchronous
C. Value: Only HashMap can let you use NULL as the key or value of an entry for a table
3, List and Map differences? The
One is a collection of single-column data, and the other is a collection of two columns of data such as keys and values, and the data stored in the list is sequential and repeatable; the data stored in the map is not sequential, its keys cannot be duplicated, and its values can be duplicated.
4, List, Set, map inherit from collection interface?
List,set Yes, map is not
5, List, map, set three interfaces, when accessing elements, what are the characteristics of each?
Such a problem belongs to the random play problem: Such a question comparison Test level, two aspects of the level: first, to really understand these content, the second is to have a strong summary and presentation capacity. If you understand, but the expression is not clear, in others is equivalent to do not understand.
First, list and set have similarities, both of which are collections of single-column elements, so they have a common parent interface called collection. The set does not allow repeating elements, so-called repetition, that is, there cannot be two equal (note, not just the same) object, that is, assuming that the set set has an A object, now I want to save a set set of a B object, but the B object is equal to equals to the A object, the B object is not stored in, so The Add method of the Set collection has a Boolean return value, when there is no element in the collection, when the Add method can successfully join the element, returns True when the collection contains an element equal to the equals of an element, the Add method cannot join the element, and the return result is false. When set takes an element, it is impossible to say the first number of elements, only the iterator interface to get all the element, and then iterate through each element. The
list represents a sequential collection, noting that it is not sort by age, by size, by price, and so on. When we call the Add (OBJ-E) method multiple times, each time the object is added is like a train station to buy tickets in the same order as the queue, sorted by first served. Sometimes, you can also jump in the queue, that is, by calling the Add (int index,obj e) method, you can specify where the current object is stored in the collection. An object can be repeatedly stored in the list, each call to the Add method, the object is inserted into the collection once, in fact, not the object itself is stored in the collection, but in the collection with an index variable point to the object, when the object is add multiple times, That is, there are multiple indexes in the collection pointing to this object, as shown in X. List In addition to the iterator interface can get all the elements, and then iterate through each element, you can also call get (index i) to clarify the number of the first. Unlike list and set,
Map is a collection of two columns, with the Put method defined as follows: Put (obj key,obj value), each time you store a pair of key/value, you cannot store duplicate keys, This repeating rule is also equal to equals. You can obtain the corresponding value according to key, which is the value that the get (Object key) returns to key. In addition, you can get all the key combinations, you can also get all the value of the union, you can also get a combination of key and value of the Map.entry object collection.
Lists hold elements in a particular order, and can have repeating elements. Set cannot have duplicate elements, internal ordering. Map holds the Key-value value, and value can be multivalued.
HashSet is stored according to the hashcode value, rather than directly by the size of the hashcode value. For example, "ABC"--->, "def"--->, "xyz"---> 65 the Order of storage in HashSet is not 62,65,78,linkedhashset stored in the order inserted, What does the Hashcode method of the stored object do? Students think of the!hashset collection to compare two objects for equality, first to see if the Hashcode method is equal, and then to see if the Equals method is equal. New two student are inserted into the hashset, look at the size of the hashset, implement the Hashcode and Equals methods, and then look at size.
The same object can be added multiple times in the vector. Add elements to the collection, which is equivalent to a string connected to the target object. To HashSet but not to add many times.
6, say Arraylist,vector, LinkedList storage performance and characteristics
Both ArrayList and vectors use arrays to store data, which is larger than the actual stored data in order to add and insert elements, both of which allow the element to be indexed directly by ordinal, but the insertion element involves memory operations such as array element movement, so the index data is fast and the data is inserted slowly. Vector because of the use of the Synchronized method (thread-safe), usually performance is worse than ArrayList, and LinkedList using a doubly linked list for storage, index data by ordinal need to be forward or backward traversal, but when inserting data only need to record the item before and after items can be , so the insertion speed is faster.
LinkedList is also thread insecure, LinkedList provides methods that enable LinkedList to be used as stacks and queues.
7. Remove duplicate elements from a vector set
Vector newvector = new vector ();
for (int i=0;i<vector.size (); i++)
{
Object obj = Vector.get (i);
if (!newvector.contains (obj);
Newvector.add (obj);
}
There is also a simple way to HashSet set = new HashSet (vector);
8. The difference between Collection and collections.
Collection is the ancestor interface of the collection class, and the main interface for inheriting it is set and list.
Collections is a helper class for a collection class that provides a series of static methods for searching, sorting, threading, and so on for various collections.
9, set of elements can not be repeated, then what method to distinguish the repetition or not? Are you using = = or equals ()? What is the difference between them?
The elements in the set cannot be duplicated, and the repetition of the elements is judged using the Equals () method.
The Equals () and = = methods Determine whether the reference value points to the same object Equals () is overwritten in the class so that when the contents and types of the two detached objects match, the truth is returned.
10. What are the collection classes you know? The Main method?
The most common collection classes are List and Map. The specific implementation of the list includes ArrayList and vectors, which are variable-sized lists that are more appropriate for building, storing, and manipulating any type of object. List is useful for cases where elements are accessed by numeric indexes.
MAP provides a more general method of storing elements. The Map collection class is used to store element pairs (called "Keys" and "values"), where each key is mapped to a value.
Arraylist/vector-->list
-->collection
Hashset/treeset-->set
Propeties-->hashtable
-->map
Treemap/hashmap
Remember not the method name, but thought, I know that they have to increase the method of the search, but the specific name of these methods, I remember not very clear, for set, the approximate method is Add,remove, contains; for map, the approximate method is Put,remove, Contains and so on, because, as long as I press the dot operator under Eclispe, it is natural that these methods come out. Some of the ideas I remember is that the list class will have a method such as get (int index), because it can take elements sequentially, and the set class does not have a method such as get (int index). Both the list and the set can iterate over all the elements, and the set and list classes have a iterator method for returning the iterator object before iterating to get a iterator object. Map can return three sets, one is to return all the keys of the collection, the other one returns a collection of all values, and then a return key and value combined into the EntrySet object collection, Map has a Get method, the parameter is key, The return value is the value corresponding to key.
Summary of Java Fundamentals (II.)