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 advanced interview questions in Java

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 advanced interview questions in Java". The content of the explanation 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 advanced interview questions are in Java.

List and Set comparison, comparison of their respective subclasses

Comparison 1: the comparison between Arraylist and LinkedList

1. ArrayList implements the data structure based on dynamic array, because the address is continuous, once the data is stored, the query operation will be more efficient (connected in memory).

2. Because the address is continuous and the ArrayList has to move the data, the insert and delete operations are relatively inefficient.

3. LinkedList is based on the linked list data structure, and the address is arbitrary, so it is not necessary to wait for a continuous address when opening up memory space, and it has an advantage for adding and deleting add and remove,LinedList.

4. Because LinkedList has to move the pointer, the performance of query operation is relatively low.

Applicable scenario analysis:

ArrayList is selected when the data needs to be accessed, and LinkedList is used when the data needs to be added and deleted many times.

Comparison 2: the comparison between ArrayList and Vector

1. The methods of Vector are synchronous and thread-safe, while those of ArrayList are not, because the synchronization of threads must affect the performance. Therefore, the performance of ArrayList is better than Vector.

2. When the element in Vector or ArrayList exceeds its initial size, Vector doubles its capacity, while ArrayList only increases its size by 50%, so. ArrayList helps to save memory space.

3. Vector is not used in most cases because of poor performance, but it supports thread synchronization, that is, only one thread can write Vector at a time to avoid inconsistencies caused by multiple threads writing at the same time.

4. Vector can set the growth factor, but ArrayList cannot.

Applicable scenario analysis:

1. Vector is thread-synchronous, so it is thread-safe, while ArrayList is thread-asynchronous and unsafe. If thread safety is not taken into account, it is generally more efficient to use ArrayList.

2. If the number of elements in the collection is greater than the length of the current collection array, using a large amount of data in the collection, using Vector has a certain advantage.

Comparison 3: the comparison between HashSet and TreeSet

1.TreeSet is implemented by binary tree, the data in Treeset is sorted automatically, and null values are not allowed.

2.HashSet is implemented in a hash table, and the data in HashSet is unordered and can be put into null, but only one null, and the values in both cannot be repeated, just like the only constraint in the database.

3.HashSet requires that the object placed must implement the HashCode () method, and the object is identified by the hashcode code, while the hashcode of the String object with the same content is the same, so the content cannot be repeated. But objects of the same class can be placed in different instances.

Applicable scenario analysis:

HashSet is implemented based on Hash algorithm, and its performance is usually better than TreeSet. We should usually use HashSet, and we only use TreeSet when we need sorting capabilities.

The general answer is as above. For similar articles, please move:

Detailed explanation of List,Set and Map and their differences and scenarios in which they are applicable

The difference between HashMap and ConcurrentHashMap

1. HashMap is not thread safe, while ConcurrentHashMap is thread safe.

2. ConcurrentHashMap uses lock segmentation technology to segment the entire Hash bucket into segment, that is, the large array is divided into several small fragments segment, and each small fragment segment has a lock, so when inserting elements, you need to find which fragment segment should be inserted, and then insert it on this fragment, and here you also need to obtain the segment lock.

3. ConcurrentHashMap makes the lock granularity finer and the concurrency performance better.

The general answer is as above. For similar articles, please move:

HashMap detailed explanation

As for the underlying implementation of both, if you want to understand through an article, then too young, find some blog posts + look at the source code.

The difference between HashTable and ConcurrentHashMap

They can all be used in multithreaded environments, but when the size of the Hashtable increases to a certain size, performance degrades sharply because iterations need to be locked for a long time. Because ConcurrentHashMap introduces segmentation, no matter how large it becomes, only one part of the map needs to be locked, while other threads don't have to wait until the iteration is complete to access the map. In short, during the iteration, ConcurrentHashMap locks only one part of the map, while Hashtable locks the entire map.

The general answer is as above. For similar articles, please move:

What's the difference between HashMap and HashTable?

The difference between String,StringBuffer and StringBuilder

1. The running speed, or execution speed, in this respect is: StringBuilder > StringBuffer > String.

2. In terms of thread safety, StringBuilder is not thread safe, while StringBuffer is thread safe.

Applicable scenario analysis:

String: suitable for situations with a small number of string operations

StringBuilder: suitable for situations where a large number of operations are performed in a character buffer in a single thread

StringBuffer: suitable for multithreading to perform a large number of operations in the character buffer

The general answer is as above. For similar articles, please move:

Introduction to String, StringBuffer and StringBuilder

The difference between wait and sleep

1. The sleep () method belongs to the Thread class, while the wait () method belongs to the Object class.

2. The sleep () method causes the program to suspend execution for a specified time and give cpu to other threads, but its monitoring state is still maintained, and it will automatically resume the running state when the specified time is up. So during the call to the sleep () method, the thread does not release the object lock.

3. When the wait () method is called, the thread will abandon the object lock and enter the waiting lock pool waiting for the object. Only after calling the notify () method on this object will the thread enter the object lock pool and get ready to acquire the object lock and enter the running state.

Thank you for your reading, the above is the content of "what are the advanced interview questions in Java". After the study of this article, I believe you have a deeper understanding of what advanced interview questions are in Java, 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.

Share To

Internet Technology

Wechat

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

12
Report