Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the interview questions of the Java programmer collection framework

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what are the interview questions of the Java programmer collection framework?" interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the interview questions of the Java programmer collection framework?"

1. What is the Java collection API

Java collection framework API is a unified framework for representing and manipulating collections. It contains interfaces, implementation classes, and algorithms to help programmers complete programming. In short, API does the following at the top:

● programming saves more effort and improves the speed and code quality of city programs.

● unrelated API improves interoperability

● saves the cost of learning to use the new API

● saves time in designing a new API

● encourages and promotes 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, and the other two interfaces are Map and SortedMap. These two interfaces do not inherit Collection and represent mappings rather than real collections.

2. What is Iterator

Some collection classes provide the ability to traverse content through the java.util.Iterator interface. These interfaces allow you to traverse a collection of objects. Manipulate each element object in turn. When using Iterators, a snapshot of the collection is included when the Iterator is obtained. It is usually not recommended to modify the collection province when traversing an Iterator.

3. What's the difference between Iterator and ListIterator?

Iterator: can only traverse the collection in a forward direction, which is suitable for getting removed elements. ListIerator: inherits Iterator, can traverse the list in both directions, and also supports element modification.

4. What are HaspMap and Map?

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

5. What's the difference between HashMap and HashTable? Contrast Hashtable VS HashMap

Both of them use key-value to obtain data. Hashtable is one of the original collection classes (also known as legacy classes). HashMap was added in version 1.2 of Java2 as part of the new collection framework. There is a difference between them:

● HashMap and Hashtable are roughly equivalent, except for asynchronous and null values (HashMap allows null values as key and value, while Hashtable cannot).

● HashMap cannot guarantee that the order of mapping will remain the same, but as a subclass of HashMap, you can easily replace it with HashMap if you want to iterate in the predicted order (by default, insert order), but it's not so easy if you use Hashtable.

● HashMap is not synchronous, while Hashtable is synchronous.

● iterative HashMap uses a fast failure mechanism, while Hashtable is not, so this is a 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, any thread needs to acquire the object lock before performing the hashtable update operation, and other threads wait for the lock to be released.

7. What is the feature of quick failure

At a high level, quick failure is the response of a system or software to its failure. A quick failure system is designed to immediately report any failure conditions that may lead to failure, and it is usually used to stop normal operations rather than trying to continue work that may be defective. When a problem occurs, the quick failure system immediately and visibly sends an error alarm. In Java, quick failure is related to iterators. If an iterator is created on the collection object, other threads want to "structurally" modify the collection object and throw a concurrent modification exception (ConcurrentModificationException).

8. How to synchronize Hashmap?

HashMap can achieve synchronization by Map m = Collections.synchronizedMap (hashMap).

When to use Hashtable and when to use HashMap

The basic difference is that Hashtable synchronization HashMap is not, so whenever it is possible for multiple threads to access the same instance, you should use Hashtable and vice versa. Non-thread-safe data structures can lead to better performance.

If there is a possibility in the future that you need to get key-value pairs in order, HashMap is a good choice because there is a subclass LinkedHashMap of HashMap. So if you want to iterate predictably sequentially (by default, in the order of insertion), you can easily replace HashMap with LinkedHashMap. On the other hand, if you use Hashtable, it is not so simple. At the same time, if multiple threads access HashMap,Collections.synchronizedMap () instead, HashMap is generally more flexible.

10. Why is the Vector class considered obsolete or unofficially deprecated? Or why we should always use ArrayList instead of Vector

You should use ArrayList instead of Vector because by default you access it asynchronously, Vector synchronizes every method, you almost never do that, and usually you want to synchronize the entire sequence of operations. It is also not safe to synchronize individual operations (if you iterate over a Vector, you still need to lock to prevent other threads from changing the collection at the same time). And it's slower. Of course, there is also lock overhead, even if you don't need it, which is a bad way to synchronize access by default. You can always use Collections.sychronizedList to decorate a collection.

In fact, Vector combines a collection of "variable arrays" with the implementation of synchronizing each operation. This is another design flaw. Vector also has some legacy methods in enumerations and element acquisition methods, which are different from the List interface, if programmers are more likely to use it in code. Although enumerations are faster, they cannot check that if the collection is modified during iteration, this will cause problems. For all the above reasons, oracle has never claimed to abandon Vector.

At this point, I believe you have a deeper understanding of "what are the interview questions of the Java programmer collection framework?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report