Win in the Java Collections Framework articles Interview (3)

21, the difference between ArrayList and the Vector

        Both classes implement the List interface (List interface extends the Collection interface), they are ordered set, ie, the position of these two elements stored in the collection are ordered, the equivalent of a dynamic array we later can be taken out by the position of an element index, and wherein the data is allowed to repeat, which is different from the maximum set HashSet and the like, and the like can not be set HashSet index number to retrieve its elements , also does not allow duplicate elements.

         Vector and ArrayList difference mainly includes two aspects:
(1) synchronization:

       Vector is thread-safe, that is the way it is between thread synchronization, and ArrayList line is unsafe procedures, among which the method is thread sync. If only one thread can access to the collection, it is best to use ArrayList, because it does not consider thread safety, efficiency will be higher; if there are multiple threads access to the collection, it is best to use Vector, because we do not need to own and again consider writing thread-safe code.

(2) Data Growth:

       Vector ArrayList and has an initial capacity size, when the storage into the inside thereof the number of elements exceeds the capacity, it is necessary to increase the storage space ArrayList and Vector, each time to increase the storage space, an increase of only one memory cell is not , but the increase plurality of memory cells, each increase in the number of memory cells between the memory space and the utilization efficiency of the procedure to achieve a certain balance. Vector twice the original default growth, while the ArrayList growth strategy is not clearly defined (see the source code from the original 1.5-fold increase) in the document. ArrayList and the Vector can set the initial size of the space, you can also set Vector space growth, while setting does not provide room for growth ArrayList methods.

    Summary: growth that is twice the original Vector, ArrayList increase of 0.5 times the original.

 

The difference between 22, HashMap and Hashtable of

        Hashtable HashMap is a lightweight implementation (non-thread-safe implementation), they have completed the Map interface, the main difference is that HashMap allows null (null) key (key), due to non-thread-safe, with only one thread accessed , the efficiency is higher than Hashtable.

        HashMap allows null as a key entry or the value, and not allowed Hashtable.

HashMap Hashtable contains methods removed, replaced containsvalue containsKey. Because it contains could easily lead to misunderstanding.

        Dictionary class inherited from Hashtable, and HashMap is a realization of Java1.2 introduction of the Map interface.

        The biggest difference is, Hashtable method is Synchronize, while HashMap is not, when multiple threads access Hashtable, do not need to sync method for its implementation, and HashMap must be synchronized to provide them.

     HashMap and HashTable three aspects to it.
        A historical reason:. Hashtable is based on obsolete Dictionary class, HashMap is an implementation of Java 1.2 introduced the Map interface of
        two synchronization:. Hashtable is thread-safe, that is synchronized, and HashMap line is unsafe procedures , not synchronized
        . ternary: only HashMap allows null as an entry you will be a table of the key or value

 

23, List and Map difference?

        Is stored in a separate set of data set storage key and the other is a double-row values ​​such data, the data stored in the List is a sequence, and allows repeated; Map data is not stored in the order, which keys are not repeating, its value can be duplicate.

 

24, List, Set, Map whether inherited from the Collection interface?

   List, Set Shi, Map not

 

25, List, Map, Set three interfaces, accessing elements, what are the differences?

(This question comparing the test level, level two aspects: one is to really understand the content, the second is to have a strong summary and presentation skills.)

        First, List and Set similarities, they are a collection of separate elements, so that they have a common parent interface, called Collection. Set inside does not allow duplicate elements, that can not have two equal (note, not just the same) object, which assumes Set A set with a target, and now I would like to again into a set of objects Set B, However, when the object a and the object B equals equal, then B is not stored into the object, therefore, set add method returns a value of a set of boolean, when an element is not set, then add method can be successfully added to this element, returns true, when set equal to an element containing elements equals, the method can not be added at this time add the element, returns a value of false. Set to take the time element, we can not elaborate on to take the first of several, only to get all the elements in the Iterator interface, and then stepping through each element.

       List represents a sequential set of note, not the kind of age, by size, sorted by price and the like. When we call add (Obje) method multiple times, each time adding an object like a train station ticket queue have the same order, sorted by first come first served. Sometimes, can jump the queue, i.e. calling add (intindex, Obj e) methods, you can specify the current position of the object stored in the collection. An object can be stored into the List repeatedly, each time calling the add method, the object is inserted into the first collection, in fact, this is not the object itself is stored into the set, but with an index variable points in the set this object, when the object is repeatedly add, i.e., corresponding to the set of a plurality of index points of the object, as shown in FIG x. List can be made in addition to all the elements with the Iterator interface, and then stepping through each element, you can also call to get (index i) to explicitly take the first few.

       Map and with a Set List, which is a set of double-row, which put method is defined as follows: put (obj key, obj value), is stored each time to be stored one pair of key / value, not storing the duplicate key, this rule is repeated by equals compare equal. Take appropriate value can be obtained according to the key, i.e., get (Object key) returns a value corresponding to the key value. Further, all can be obtained in conjunction with the key, it may also be combined to obtain the value of all, and also possible to obtain a set of key value pairs into Map.Entry object.

 List specific order to hold the elements may have duplicate elements. Set can not have duplicate elements, internal order. Map save key-value value, value can be multi-valued.

 

26, say the storage performance and characteristics of the ArrayList, Vector, LinkedList of

        ArrayList and Vector data is stored using an array, the array element number is greater than the actual data is stored so as to increase and insert elements, which allow the element directly indexed by serial number, but to insert the element array elements involved in memory operations like move, so the index data fast and slow data insertion, Vector method since a synchronized (thread-safe), more typically ArrayList performance difference. And LinkedList implemented using a doubly linked list stored, indexed by serial number data required to traverse the forward or index slows down, but only before and after the record of this can insert the data item, the insertion speed.

        LinkedList is thread safe, LinkedList provides a number of methods that can be used as LinkedList stack and queue to use.

 

27, the removal of one set of repeated elements Vector

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 a simple way to use the Set does not allow duplicate elements:

HashSetset = new HashSet(vector);

 

28, Collection and Collections of the difference.

        Collection is a collection of superior class interfaces, interfaces inherit his main Set and List.

        Collections help against a class set of classes, he offers a series of static methods to achieve a variety of search collection, sorting, and so thread-safe operations.

 

29, Set in the element is not repeated, then the method used to distinguish whether or not repeat it? Is)? What is the difference between them with == or equals (?

        Set in the elements is not repeated, the elements used or not is repeated equals () methods of determination.

        And the difference is equal == test rotten title, said here about:

        == operator designed to compare two values ​​are equal, i.e. for comparing the variables corresponding to the values ​​stored in the memory are the same, to compare two basic types of data or two reference variables are equal, == operator can only be used.

        equals method is used to compare the contents of two separate objects are the same, like is the same ratio to compare the appearance of two people, it compares two objects are independent. 

        For example: two statements create two new objects, then a / b of these two variables point to one object, which is two different objects, they are different from the first address, i.e., a and b stored values ​​are not the same, therefore, the expression a == b will return false, and the content of these two objects are the same, therefore, the expression a.equals (b) returns true.


30, you know what type of collection? The main method?

        The most common type is the set of List and Map. List specific implementation comprises ArrayList and Vector, which is a list of variable size, more suitable to build, operate and store any type of object list element. List applies to the case of access elements by numeric index.

        Map provides a more general method of storage elements. Map for storing collections of elements (referred to as a "key" and "value"), wherein each key is mapped to a value.

        They are the additions and deletions to change search method.

        For the set, is probably the way to add, remove, contains, etc.

        For map, probably way is to put, remove, contains, etc.

        List class will get (int index) such a method, since it can take in the order of elements, and such methods get (int index) set is not a class. List and set out all the elements can be iterative, first get an iterator object iteration, so, set and list iterator class has a method that returns the iterator object. map can return to the three sets, a return collection of all of the key, the other returns a collection of all value, and then a return of the key and value combination into EntrySet collection of objects, map also get method, parameters are key, the return value is the key corresponding to the value, this free play, nor is the ability to test the method in mind, these programming process will prompt combine all three talk about their different usage on the line.

Guess you like

Origin www.cnblogs.com/mxb0611/p/11904141.html