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 most common container interview questions for 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 most common container interview 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 most common container interview questions in Java.

Container

18. What are the java containers?

Catalogue of commonly used containers: 19. What's the difference between Collection and Collections?

Java.util.Collection is a collection interface (a top-level interface of a collection class). It provides a general interface method to perform basic operations on collection objects. The Collection interface has many concrete implementations in the Java class library. The meaning of Collection interface is to provide a maximum unified operation mode for various specific collections, and its direct inheritance interfaces are List and Set.

Collections is a utility class / helper class of the collection class, which provides a series of static methods for sorting, searching, thread-safe, and other operations on the elements in the collection.

20. What is the difference between List, Set, and Map?

21. What's the difference between HashMap and Hashtable?

HashMap removes HashTable's contains method, but adds the containsValue () and containsKey () methods.

HashTable is synchronized, while HashMap is asynchronous and more efficient than hashTable.

HashMap allows null key values, but hashTable does not.

twenty-two。 How do I decide to use HashMap or TreeMap?

HashMap is the best choice for operations such as inserting, deleting, and locating elements in Map. However, if you need to traverse an ordered key collection, TreeMap is the better choice. Depending on the size of your collection, it may be faster to add elements to HashMap, and replace map with TreeMap for orderly key traversal.

23. Tell me about the implementation principle of HashMap?

HashMap Overview: HashMap is an asynchronous implementation of the hash table-based Map interface. This implementation provides all optional mapping operations and allows the use of null values and null keys. This class does not guarantee the order of the mapping, especially it does not guarantee that the order is permanent.

HashMap data structure: in the java programming language, the most basic structure is two, one is an array, the other is an analog pointer (reference), all data structures can be constructed with these two basic structures, HashMap is no exception. HashMap is actually a "linked list hash" data structure, that is, a combination of arrays and linked lists.

When we put the element in Hashmap, we first recalculate the hash value according to the hashcode of key, and eliminate the hash value to get the position of the element in the array (subscript). If the array has already stored other elements in this position, then the elements in this position will be stored in the form of a linked list, the newly added will be placed at the head of the chain, and the first to be added at the end of the chain. If there is no element at that position in the array, place the element directly at that location in the array.

It should be noted that the implementation of HashMap is optimized in Jdk 1.8. when there are more than eight node data in the linked list, the linked list will be converted to a red-black tree to improve query efficiency, from O (n) to O (logn).

24. Tell me about the implementation principle of HashSet?

The underlying layer of HashSet is implemented by HashMap

The value of HashSet is stored on the key of HashMap

The value of HashMap is unified as PRESENT

25. What is the difference between ArrayList and LinkedList?

The most obvious difference is that the underlying data structure of ArrrayList is an array, which supports random access, while the underlying data structure of LinkedList is a two-way cyclic linked list, which does not support random access. Using the subscript to access an element, the time complexity of ArrayList is O (1), while LinkedList is O (n).

twenty-six。 How to implement the conversion between array and List?

Convert List to an array: call the toArray method of ArrayList.

Convert the array to List: call the asList method of Arrays.

twenty-seven。 What is the difference between ArrayList and Vector?

Vector is synchronous, while ArrayList is not. However, if you seek to make changes to the list during iterations, you should use CopyOnWriteArrayList.

ArrayList is faster than Vector because it doesn't overload because it has synchronization.

ArrayList is more generic because we can easily get synchronized lists and read-only lists using the Collections utility class.

twenty-eight。 What is the difference between Array and ArrayList?

Array can hold basic types and objects, while ArrayList can only hold objects.

The Array is immutable after the specified size, while the ArrayList size is variable.

Array does not provide as many features as ArrayList, such as addAll, removeAll, and iterator.

twenty-nine。 What is the difference between poll () and remove () in Queue?

Both poll () and remove () fetch an element from the queue, but poll () returns null when it fails to get the element, but remove () throws an exception when it fails.

thirty。 Which collection classes are thread safe?

Vector: there is one more synchronization mechanism (thread safety) than arraylist, which is no longer recommended because of its low efficiency. In web applications, especially foreground pages, efficiency (page response speed) is often a priority.

Statck: stack class, first in, then out.

Hashtable: there is one more thread safety than hashmap.

Enumeration: enumeration, equivalent to an iterator.

thirty-one。 What is the iterator Iterator?

An iterator is a design pattern that is an object that can traverse and select objects in a sequence without the developer knowing the underlying structure of the sequence. An iterator is often called a "lightweight" object because it is less expensive to create.

thirty-two。 How to use Iterator? What are the characteristics?

The Iterator function in Java is relatively simple and can only be moved in one direction:

(1) use the method iterator () to require the container to return an Iterator. The first time you call the next () method of Iterator, it returns the first element of the sequence. Note: the iterator () method is the java.lang.Iterable interface and is inherited by Collection.

(2) use next () to get the next element in the sequence.

(3) use hasNext () to check if there are any elements in the sequence.

(4) use remove () to delete the new elements returned by the iterator.

Iterator is the simplest implementation of the Java iterator, and ListIterator designed for List has more functionality to traverse the List in both directions, or to insert and delete elements from the List.

thirty-three。 What's the difference between Iterator and ListIterator?

Iterator can be used to traverse Set and List collections, but ListIterator can only be used to traverse List.

Iterator can only traverse the collection forward, and ListIterator can be either forward or backward.

ListIterator implements the Iterator interface and includes other functions, such as adding elements, replacing elements, getting the index of the previous and second elements, and so on.

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