10 classic Java interview questions and classic java questions

Source: Internet
Author: User

10 classic Java interview questions and classic java questions

1. How does Java's HashMap work?


HashMap is a key value for the data structure. Each key has a corresponding value. The key is to identify such a value.


Based on the hashing principle, HashMap stores and obtains objects through 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, and then finds the bucket location to store the value object. When obtaining an object, find the correct key-value pair through the equals () method of the key object, and then return the object. HashMap uses the merge list to solve the collision problem. When a collision occurs, the object will be stored in the next node of the merge list. HashMap stores key-Value Pair objects in each worker list node.


2. What is a failed fault Security iterator?


A Java iterator that fails quickly may cause ConcurrentModifcationException to be modified during the underlying collection iteration process. As a replica iteration in an instance, fault security does not throw any exceptions. The quick failure Security example defines how the system responds when a fault occurs. For example, ArrayList, a fast iterator used for failure, and ConcurrentHashMap, an iterator used for fault security.


3. What is Java BlockingQueue?


Java BlockingQueue is a part of a concurrent collection util package. BlockingQueue queue is a type of support for operations. It waits for elements to be retrieved when they become available and stores elements when the space is available.


4. When to use ConcurrentHashMap?


In question 2, we can see that ConcurrentHashMap is used as an instance of the fault Security iterator, which allows complete concurrent retrieval and update. ConcurrentHashMap can be used when a large number of concurrent updates exist. This is very similar to Hashtable, but ConcurrentHashMap does not lock the entire table to provide concurrency. Therefore, ConcurrentHashMap has better performance. Therefore, ConcurrentHashMap should be used when there are a large number of updates.


5. Which List implements the fastest insert?


The list of variables and ArrayList are different. The advantage of ArrayList is the dynamic growth of arrays, which is very suitable for initial use when the total length is unknown. The advantage of the sort list is that the insert and delete operations in the middle are the fastest.


The listlist interface allows null elements. In addition, the values list provides additional get, remove, and insert methods at the beginning or end of the values list. These operations enable the queue list to be used as a stack, queue, or two-way queue (deque ).


ArrayList implements an array of variable sizes. It allows all elements, including null. Each ArrayList instance has a Capacity, that is, the size of the array used to store elements. This capacity can automatically increase with the addition of new elements, but the growth algorithm is not defined. When a large number of elements need to be inserted, you can call the ensureCapacity method before insertion to increase the ArrayList capacity to improve the insertion efficiency.


6. Differences between Iterator and ListIterator


● ListIterator has the add () method. You can add objects to the List, but Iterator cannot.

● Both ListIterator and Iterator have hasNext () and next () methods to implement sequential backward traversal. However, ListIterator has hasPrevious () and previous () methods to implement reverse (sequential forward) traverse. Iterator cannot.


● ListIterator can locate the current index location, which can be achieved by nextIndex () and previousIndex. Iterator does not have this function.

● All objects can be deleted, but the ListIterator can modify objects and the set () method can. Iierator can only be traversed and cannot be modified.


7. What is CopyOnWriteArrayList? What is the difference between it and ArrayList?


CopyOnWriteArrayList is a thread-safe variant of ArrayList. All variable operations (add, set, and so on) are implemented by performing a new copy on the underlying array. Compared with ArrayList, its write operations are slower because it requires instance snapshots.


The write operation in CopyOnWriteArrayList needs to copy an array in a large area, so the performance is definitely poor. However, because the operation object and the write operation are not the same object, no locks are required between the read operations, the synchronization process between read and write operations only points the reference to a new array object through a simple "=" after the writing, which requires almost no time, in this way, read operations are very fast and secure, and are suitable for multithreading. ConcurrentModificationException will never occur. Therefore, CopyOnWriteArrayList is suitable for scenarios where read operations are much larger than write operations, such as caching.


8. Differences between iterator and enumeration


If the interviewer asks this question, his intention must be to distinguish Iterator from Enumeration in two ways:


● Iterator allows you to remove elements from the underlying set.

● The Iterator method names are standardized.


9. How to synchronize Hashmap?


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


● Use Collections. synchronizedMap (...) to synchronize HashMap.

● Use ConcurrentHashMap's


The first choice between the 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.


10. Differences between IdentityHashMap and HashMap


IdentityHashMap is the implementation of the Map interface. Unlike HashMap, the reference equality is used here.


● In HashMap, if the two elements are equal, then key1.equals (key2)


● In IdentityHashMap, if the two elements are equal, key1 = key2

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.