Java Collection interview question Summary

Source: Internet
Author: User

Java Collection interview question Summary
1. What is a Java set API?
Java Collection framework API is a unified framework used to represent and operate a set. It contains interfaces, implementation classes, and programming algorithms that help programmers complete. In short, the API completes the following tasks at the upper layer:

● More effort-saving programming, improving the speed and code quality of city programs
● Non-associated APIs improve interoperability
● Saving learning and using new APIs
● Saving time for designing new APIs
● Encourage and promote Software Reuse

Specifically, there are six Collection interfaces, the most basic of which is the Collection interface, which is inherited by three interfaces: Set, List, And SortedSet. The other two interfaces are Map and SortedMap, these two interfaces do not inherit Collection, indicating that the ing is not a real set.

2. What is Iterator?
Some collection classes provide the content traversal function through the java. util. Iterator interface. These interfaces allow you to traverse the collection of objects. Operate on each element object in sequence. When Iterators is used, a set snapshot is included when Iterator is obtained. When traversing an Iterator, we do not recommend that you modify the collection province.

3. What is the difference between Iterator and ListIterator?
Iterator: it can only traverse the set forward. It is suitable for obtaining and removing elements. ListIerator: inherits Iterator and supports bidirectional list traversal. It also supports element modification.

4. What are HaspMap and Map?
Map is an interface, a part of the Java Collection framework, used to store key-value pairs. HashMap is a class that implements Map using hash algorithms.

5. What is the difference between HashMap and HashTable? Comparison of Hashtable VS HashMap
Both methods use the key-value method to obtain data. Hashtable is one of the original collection classes (also known as legacy classes ). As part of the new collection framework, HashMap is added to Java 2 version 1.2. There is a difference between them:
● HashMap and Hashtable are roughly the same except for non-synchronous and null values (HashMap allows null values as keys and values, but Hashtable does not ).
● HashMap does not guarantee that the ing sequence remains unchanged. However, as a subclass of HashMap, LinkedHashMap can easily replace it with HashMap if you want to predict the sequential iteration (by default, it is inserted in order, it would be easier to use Hashtable.
● HashMap is not synchronous, but Hashtable is synchronous.
● Iterative HashMap uses a fast failure mechanism, but Hashtable is not. This is the design consideration.

6. What does synchronization mean in the Hashtable context?
Synchronization means that only one thread can modify the hash table at a time point. Any thread needs to obtain the object lock before performing the update operation of hashtable. Other threads are waiting for the lock to be released.

7. What is the fast failure feature?
At a high level, quick failure is a system or software response to its faults. A Fast failure system is designed to instantly report any failures that may cause failures. It is usually used to stop normal operations rather than trying to continue the work that may be defective. When a problem occurs, the system immediately sends an error alarm. In Java, quick failure is related to iterators. If an iterator is created on a collection object and other threads want to "structurally" Modify the collection object, ConcurrentModificationException is thrown.

8. How to synchronize Hashmap?
HashMap can achieve synchronization through Map m = Collections. synchronizedMap (hashMap.

9. When to use Hashtable and HashMap
The basic difference is that Hashtable does not synchronize HashMap. Therefore, whenever multiple threads access the same instance, Hashtable should be used. Otherwise, HashMap should be used. A non-thread-safe data structure can provide better performance.
If there is a possibility in the future-when you need to obtain the key-value pairs in order, HashMap is a good choice, because a subclass of HashMap has LinkedHashMap. Therefore, if you want to predict the sequential iterations (by default, in the order of insertion), you can easily replace HashMap with LinkedHashMap. On the other hand, Hashtable is not that easy to use. At the same time, if multiple threads access HashMap, Collections. synchronizedMap () can be replaced. In general, HashMap is more flexible.

10. Why is the Vector class considered obsolete or unofficial and not recommended? Or why should we always use ArrayList instead of Vector?
You should use ArrayList instead of Vector because by default, you do not have synchronous access. Vector synchronizes every method, and you almost never want to do that. Generally, you want to synchronize the entire operation sequence. It is not safe to synchronize a single operation (if you iterate a Vector, you still need to lock it to avoid other threads changing the set at the same time), and the efficiency is slower. Of course, there is also a lock overhead, even if you don't need it, this is a bad way to synchronize access by default. You can always use Collections. sychronizedList to decorate a set.
In fact, Vector combines the set of "variable arrays" and the implementation of synchronizing each operation. This is another design defect. Vector also has some legacy methods in enumeration and element acquisition methods. These methods are different from the List interface, if these methods tend to be used by programmers in code. Although enumeration is faster, they cannot check that if the set is modified during iteration, this will cause problems. Despite the above reasons, oracle never claimed to discard the Vector.

 

Related Article

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.