Java Collection interview questions

Source: Internet
Author: User

  The Java Collection Framework is the most frequently asked question of Java interviewing, and it is necessary to understand the powerful features of Java technology to master the set framework. Here are some practical questions, often asked in the core Java interview.

1. What is the Java Collection API

The Java Collection Framework API is a unified framework for representing and manipulating collections, which contains interfaces, implementation classes, and algorithms that help programmers complete some programming. In short, the API does the following on the top:

More labor-saving programming to improve City program speed and code quality

Non-associative APIs for improved interoperability

Save Learning with new API costs

Save time in designing new APIs

Encourage and promote software reuse

Specifically, there are 6 sets of interfaces, the most basic is the collection interface, by three interface set, List, SortedSet inheritance, the other two interfaces are map, SortedMap, these two interfaces do not inherit collection, represents a mapping rather than a true collection.

2. What is iterator

Some collection classes provide a function of content traversal through the Java.util.Iterator interface. These interfaces allow iterating over the collection of objects. Manipulate each element object in turn. When iterators is used, a collection snapshot is included when the iterator is obtained. It is usually not recommended to modify the collection province when traversing a iterator.

3. What is the difference between iterator and listiterator?

Iterator: The collection can only be traversed forward, and is suitable for getting removed elements. Listierator: Inherits the iterator, can traverse the bidirectional list, also supports the element modification.

4. What is Haspmap and map?

Map is an interface, a part of the Java collection framework, used to store key-value pairs, and HashMap is a class that implements a map using a hashing algorithm.

5. What is the difference between HashMap and Hashtable? Compare Hashtable VS HashMap

Both are used to obtain data in a key-value way. Hashtable is one of the original collection classes (also known as The Legacy Class). HashMap is added as part of the new collection framework in version 1.2 of JAVA2. There is a difference between them:

HashMap and Hashtable are roughly equivalent except for non-synchronous and null values (HASHMAP allows null values as key and value, while Hashtable cannot).

HashMap cannot guarantee that the order of the mappings is constant, but as a subclass of HashMap Linkedhashmap, if you want to predict the sequential iterations (by default in order of insertion), you can easily replace the HashMap, if you use Hashtable is not so easy.

The HashMap is not synchronous, and Hashtable is synchronous.

Iterative HashMap uses a fast failure mechanism, and Hashtable is not, so this is the design consideration.

6. What does synchronization mean in the context of Hashtable?

Synchronization means that only one thread can modify the hash table at a point in time, and any thread needs to acquire an object lock before executing the Hashtable update operation, while other threads wait for the lock to be released.

7. What is called fast failure feature

Rapid failure from a high-level hierarchy is a response of a system or software to its failure. A fast-failing system is designed to instantly report any failure condition that could lead to failure, and it is often used to stop normal operations rather than trying to continue with potentially flawed work . When a problem occurs, the system quickly fails to make a false error alarm immediately. In Java, quick failures are related to iterators. If a iterator is created on the collection object, other threads are "structured" to modify the collection object, and the concurrent Modification exception (Concurrentmodificationexception) is thrown.

8, how to make HashMap synchronization?

The HASHMAP can be synchronized by using map m = Collections.synchronizedmap (HASHMAP).

9, when to use Hashtable, when to use HashMap

The basic difference is that Hashtable synchronous HashMap is not, so whenever there are multiple threads accessing the same instance, the Hashtable should be used instead of hashmap. Non-thread-safe data structures provide better performance.

If there is a possibility in the future-you need to get a scheme for key-value pairs in order, HashMap is a good choice because there is a subclass of HashMap Linkedhashmap. So if you want predictable sequential iterations (by default in the order of insertions), you can easily replace HashMap with Linkedhashmap. It is not so easy to use the hashtable of the opposite view. At the same time if there are multiple threads accessing Hashmap,collections.synchronizedmap () can be substituted, generally hashmap more flexible.

10. Why is the vector considered to be obsolete or unofficially deprecated? Or why we should always use ArrayList instead of vectors.

You should use ArrayList instead of vectors because by default you are non-synchronous, vectors synchronize each method, you almost never do that, and usually you want to synchronize the entire sequence of operations. Synchronizing a single operation is also unsafe (if you iterate over a vector, you still need to lock it to prevent other threads from changing the collection at the same time). and more efficient. Of course there is also the cost of locking even if you don't need it, this is a bad way to access by default. You can always use Collections.sychronizedlist to decorate a collection.

In fact, vectors combine a set of "mutable arrays" and synchronize the implementation of each operation. This is another design flaw. Vector also has some legacy methods in enumerations and elements to get methods that are different from the list interface, if these methods in the code programmers tend to want to use it. Although enumerations are faster, they cannot be checked if the collection is modified during iteration, which can cause problems. For many of these reasons, Oracle has never claimed to abandon vectors.

Top
-
Step

Java Collection interview questions

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.