In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what are the common set class test questions in Java". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the common collection class test questions in Java".
How does 21.HashMap work?
HashMap is internally implemented through an array, but this array is special. The element stored in the array is an Entry entity (jdk 8 is Node). This Entry entity mainly contains key, value and a next pointer to itself. HashMap is based on hashing. When we do put operation, we get its hashcode according to the key value passed, and then use this hashcode to perform modular operation with the length of the array to get an int value, that is, the Entry is to be stored in the location of the array (subscript). When the value of the specified key is obtained by the get method, its hash value (array subscript) is calculated based on the key, the Entry corresponding to the array subscript is obtained according to the hash value, and then determine whether the key and hash value in the Entry is the same as what you are looking for through equals (). If so, return value, otherwise, traverse the linked list (there may be only one hash, and return null directly at this time) until you find it Otherwise, null is returned.
The reason why HashMap stores a linked list in each array element is to solve the problem of hash conflicts. When the hash values of two objects are equal, then there must be no room for two values in one place, so hashmap uses linked lists to resolve this conflict, and two elements with equal hash values form a linked list.
What is the difference between 22.HashMap and HashTable?
1.HashTable is based on the Dictionary class, while HashMap is based on AbstractMap. Dictionary is the abstract parent of any class that maps keys to corresponding values, while AbstractMap is an implementation based on the Map interface that minimizes the work required to implement this interface. When I look at the source code in java 8, I find that Hashtable does not inherit Dictionary, and there is no synchronization method in it. Is it true that Hashtable is not synchronized in java 8? Can anyone explain it? )
2. Both key and value of HashMap are allowed to be null, while key and value of Hashtable are not allowed to be null. When HashMap encounters key as null, it calls the putForNullKey method for processing, but does not handle value; Hashtable encounters null and returns NullPointerException directly.
3. Hashtable is synchronous, while HashMap is asynchronous, but we can also synchronize it through Collections.synchronizedMap (hashMap).
How does 23.CorrentHashMap work?
Jdk version 1.6: ConcurrenHashMap can be said to be an upgraded version of HashMap. ConcurrentHashMap is thread-safe, but it is implemented in a different way than Hashtablea. Hashtable is blocked by locking the hash table structure, and when one thread occupies the lock, other threads must block and wait for it to release the lock. ConcurrentHashMap uses separate locks, it does not lock the entire hash table, but local locking, that is, when a thread occupies the local lock, it does not affect other threads' access to other parts of the hash table.
Implementation: there is an array of Segment inside ConcurrentHashMap, and the Segment object can act as a lock. There is an HashEntry array inside the Segment object, so each Segment can guard several HashEntry, and each bucket may be a linked list connected by HashEntry, storing the elements that have collided.
Each ConcurrentHashMap creates an array of 16 Segment objects under the default concurrency level, and each array has several buckets. When we implement the put method, we use the hash method to calculate the key, get the hash value, find the corresponding segment, then lock the segment, and then call the put method of segment for storage operation. In this case, other threads cannot access the current segment, but can access other segment objects without blocking and waiting.
Jdk version 1.8 in jdk 8, ConcurrentHashMap no longer uses Segment split locks, but uses an optimistic lock CAS algorithm to implement synchronization problems, but the underlying layer is still the implementation of "array + linked list-> red-black tree".
24. What are the different ways to traverse a List? List strList = new ArrayList (); / / for-each for (String str:strList) {System.out.print (str);} / use iterator try to use this more secure (fail-fast) Iterator it = strList.iterator (); while (it.hasNext) {System.out.printf (it.next ());} what's the difference between 25.fail-fast and fail-safe?
The fail-fast property of Iterator works with the current collection, so it is not affected by any changes in the collection. All collection classes in the Java.util package are designed to be fail- > fast, while collection classes in java.util.concurrent are fail-safe. When a change in the structure of the collection being traversed is detected, the Fail-fast iterator throws ConcurrentModificationException, while the fail-safe iterator never throws ConcurrentModificationException.
What is the difference between 26.Array and ArrayList? When is it more appropriate to use Array?
Array can hold basic types and objects, while ArrayList can only hold objects.
The Array is the specified size, while the ArrayList size is fixed
twenty-seven。 Which collection classes provide random access to elements?
The ArrayList, HashMap, TreeMap, and HashTable classes provide random access to elements.
What is the underlying implementation of 28.HashSet?
By looking at the source code, we know that the implementation of HashSet depends on HashMap, and the values of HashSet are stored in HashMap. In the construction of HashSet, a HashMap object is initialized, and HashSet does not allow duplicate values, so the value of HashSet is stored in HashMap as the key of HashMap and returns false when the stored value already exists.
The implementation principle of 29.LinkedHashMap?
LinkedHashMap is also implemented based on HashMap, except that it defines an Entry header, this header is not placed in the Table, it is extra independent. LinkedHashMap implements sorting by insertion order or access order by inheriting Entry in hashMap and adding two attributes Entry before,after, which are combined with header to form a two-way linked list. LinkedHashMap defines the sort mode accessOrder, which is a Boolean variable, or true; for the access order, or false for the insertion order. In general, it is not necessary to specify a sort mode, and its iterative order is the default insertion order.
What is the difference between 30.LinkedList and ArrayList?
ArrayList is based on array implementation, LinkedList is based on linked list implementation
ArrayList is fast in searching, and LinkedList has more advantages in inserting and deleting.
Thank you for your reading, the above is the content of "what are the common Java collection test questions?" after the study of this article, I believe you have a deeper understanding of what the common Java collection class test questions have, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.