10 classic Java Face test set (reprint)

Source: Internet
Author: User

How does the 1.Java hashmap work?

HashMap is a key value for the data structure, each key will have a corresponding value, the key is to identify such a value.

HashMap based on the hashing principle, we store and retrieve objects through the put () and get () methods. When we pass a key-value pair to the put () method, it calls the Hashcode () method of the Key object to calculate the hashcode, allowing the bucket position to be found to store the value object. When the object is fetched, the correct key-value pair is found by the Equals () method of the Key object, and then the value object is returned. HASHMAP uses LinkedList to solve the collision problem, and when a collision occurs, the object is stored in the next node of the LinkedList. HashMap stores key-value pairs of objects in each LinkedList node.

2. What is a fail-safe iterator for fast failure?

A fast-failing Java iterator may cause concurrentmodifcationexception to be modified during the iteration of the underlying collection. Fail-Safe as an iteration of a replica that occurs in an instance, no exception is thrown. The fast-failing fail-safe paradigm defines how the system reacts when a fault is encountered. For example, a fast iterator ArrayList for failure and an iterator for fail-safe concurrenthashmap.

What is 3.Java blockingqueue?

Java Blockingqueue is part of a concurrent collection util package. The Blockingqueue queue is a support operation that waits for an element to become available to retrieve, and also to store elements when the space is available.

4. When do I use Concurrenthashmap?

In question 2 We see that Concurrenthashmap is used as an instance of a fail-safe iterator that allows full concurrent retrieval and update. When there is a large number of concurrent updates, Concurrenthashmap can be used at this time. This is very similar to Hashtable, but Concurrenthashmap does not lock the entire table to provide concurrency, so concurrenthashmap performance seems better from this point. So concurrenthashmap should be used when there is a lot of updates.

5. Which list implements the fastest insertion?

LinkedList and ArrayList are another implementation of a different list of variables. The advantage of ArrayList is that the dynamic growth array is well suited for use in situations where the total length is unknown at initial time. The advantage of LinkedList is that it is inserted and deleted in the middle position, and the speed is the fastest.

The LinkedList implements a list interface that allows null elements. Additionally LinkedList provides an additional Get,remove,insert method at the first or the tail of the LinkedList. These operations make the LinkedList available as a stack (stack), queue, or two-way queue (deque).

ArrayList implements a variable-size array. It allows all elements, including null. Each ArrayList instance has a capacity (capacity), which is the size of the array used to store the elements. This capacity automatically increases as new elements are added, but the growth algorithm is not defined. When you need to insert a large number of elements, you can call the Ensurecapacity method before inserting to increase the capacity of the ArrayList to improve insertion efficiency.

the difference between 6.Iterator and Listiterator
    • Listiterator has the Add () method, you can add objects to the list, and iterator cannot.
    • Both Listiterator and iterator have the Hasnext () and Next () methods, which can be traversed sequentially, but Listiterator have the hasprevious () and previous () methods to enable reverse (sequential forward) traversal. Iterator is not allowed.
    • Listiterator can locate the current index position, Nextindex () and Previousindex () can be implemented. Iterator does not have this feature.
    • Can implement the Delete object, but Listiterator can implement the object modification, the set () method can be implemented. Iierator can only traverse and cannot be modified.
7. What is copyonwritearraylist, and how does it differ from ArrayList?

Copyonwritearraylist is a thread-safe variant of ArrayList, where all mutable operations (add, set, and so on) are implemented by a new copy of the underlying array. Compared to ArrayList it is slower to write because it requires a snapshot of the instance.

Copyonwritearraylist write operations require large-area copy arrays, so performance must be poor, but the read operation because the object and write operation is not the same object, read between the need to lock, the synchronization between read and write only after writing through a simple "=" To point a reference to a new array object, which hardly takes time, so that the read operation is very safe, suitable for use in multi-threading, never concurrentmodificationexception, So copyonwritearraylist is suitable for scenarios where read operations are much larger than write operations, such as caching.

8. The difference between iterators and enumerations

If the interviewer asks this question, his intentions must be to differentiate iterator from the two aspects of enumeration:

    • Iterator allows the removal of elements from the underlying collection.
    • The method name of the iterator is standardized.
How does 9.Hashmap sync?

When we need a synchronized hashmap, there are two options:

    • Use Collections.synchronizedmap (.. ) to synchronize the HashMap.
    • Using the Concurrenthashmap

The preferred choice between these two options is to use Concurrenthashmap, because we do not need to lock the entire object and get the lock through the Concurrenthashmap partition map.

the difference between 10.IdentityHashMap and HashMap

Identityhashmap is the implementation of the map interface. Different from HashMap, the reference equality is used here.

    • In HashMap if two elements are equal, then Key1.equals (Key2)
    • In Identityhashmap if two elements are equal, then key1 = = Key2

10 classic Java Face test set (reprint)

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.