Java face question-Set Framework chapter II

Source: Internet
Author: User
Tags repetition set set

21. 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.

The difference between ArrayList and Vector mainly consists of two aspects:.
(1) Synchronization:

The vector is thread-safe, that is, it is thread-synchronized between its methods, and ArrayList is a thread-less program that is not synchronized between its methods. If there is only one thread routines access to the collection, it is best to use ArrayList, because it is more efficient than thread-safe, and if there are multiple lines routines access to the collection, it is best to use vectors, since we do not need to consider and write thread-safe code ourselves.

(2) Data growth:

ArrayList and Vector have an initial capacity size, when the number of elements stored in them exceeds the capacity, you need to increase the storage space of ArrayList and vector, each time to increase storage space, not only add a storage unit, but add multiple storage units, Each increase in the number of storage units in memory space utilization and program efficiency to achieve a certain balance. Vector growth is twice times the default, and ArrayList's growth strategy is not clearly defined in the documentation (from the source code to see the growth of 1.5 times times the original). Both the ArrayList and vector can set the initial space size, and the vector can also set the amount of space to grow, while ArrayList does not provide a way to set the growth space.

Summary: That is, the vector increases the original one times, the ArrayList increases the original 0.5 times times.

22. 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 synchronization.

HashMap and Hashtable are mainly from three aspects.
I. Historical reasons: Hashtable is based on the old dictionary class, HashMap is an implementation of the map interface introduced by Java 1.2
Two. Synchronization: Hashtable is thread-safe, that is, synchronous, and HashMap is a thread program is not secure, not synchronous
Three. Value: Only HashMap can let you use NULL as the key or value of an entry for a table

23. What is the difference between list and map?

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.

24. List,set, does the map inherit from the collection interface?

List,set Yes, map is not

25, List, MAP, set three interfaces, access to elements, what are the characteristics of each?

(Such a question comparative test level, two aspects of the level: first, to really understand these content, the second is to have a strong summary and presentation ability. )

First, the list and set have similarities, both of which are collections of single-column elements, so they have a common parent interface called collection. Duplicate elements are not allowed in the set, that is, there cannot be two equal (note, not just the same) object, that is, assuming that the set set has an A object, and now I want to deposit a B object to the set set, but the B object is equal to equals of 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 not necessary to take the first few points, only to get all the elements in the iterator interface, and then iterate through each element individually.

The list indicates a sequential collection, noting that it is not sort by age, by size, by price, and so on. When we call the Add (Obje) method multiple times, each time the object is added is like a train station to buy tickets in a queued order, sorted by first served order. Sometimes, you can also jump in the queue, called the Add (Intindex,obj e) method, and 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. In addition to using the iterator interface to 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 set of two columns, with the Put method defined as follows: Put (obj key,obj value), each time it is stored, to store a pair of key/value, cannot store duplicate key, this repeating rule is 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.

26, say the 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 due to the use of the Synchronized method (thread-safe), usually performance is worse than ArrayList. While LinkedList uses a doubly linked list for storage, indexing data by ordinal requires a forward or backward traversal, and the index slows down, but inserting the data requires only the front and back of the item to be recorded, so the insertion is faster.

LinkedList is also thread insecure, LinkedList provides methods that enable LinkedList to be used as stacks and queues.

27. 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 an easy way to take advantage of the set that does not allow repeating elements:

Hashsetset = new HashSet (vector);

28. The difference between collection and collections.

Collection is the ancestor interface of the collection class, and inherits his interface mainly 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.

29, the set of elements can not be repeated, then what method to distinguish between repetition or not? = = 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.

= = and equal difference is also a bad question, here say:

The = = operator is specifically used to compare the values of two variables, that is, whether the value stored in the memory used to compare the variables is the same, to compare two basic types of data or two reference variables equal, only with the = = operator.

The Equals method is used to compare the contents of two separate objects, as compared to two people whose looks are the same, compared to the two objects that are independent of each other.

For example: Two new statements create two objects, and then use a/b to point to one of the objects, which is two different objects, their first address is different, that is, the values stored in a and B are not the same, so the expression a==b will return False, The contents of the two objects are the same, so the expression a.equals (b) returns True.


30. 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.

They all have the method of increasing the pruning and checking.

For set, the approximate method is Add,remove, contains, etc.

For map, the approximate method is put,remove,contains, etc.

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 a key corresponding to the value, this free play, nor the ability to test methods, these programming process will be prompted, combined with the differences between the three of them to say the use of the line.

Java face question-Set Framework chapter II

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.