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 face-to-face questions about the Java set

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what are the interview questions about the Java collection", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the interview questions about the Java collection"?

What is the difference between 1.HashMap and HashTable?

(1) HashMap allows key and value to be null, but HashTable does not.

(2) HashTable is synchronous, but HashMap is not. Therefore, HashMap is suitable for single-threaded environment and HashTable is suitable for multithreaded environment.

(3) A subclass of LinkedHashMap,HashMap is introduced in Java1.4. If you want to traverse the order, you can easily change from HashMap to LinkedHashMap, but HashTable is not like this, and its order is unpredictable.

(4) HashMap provides traversal of key's Set, so it is fail-fast, but HashTable provides traversal of key's Enumeration, which does not support fail-fast.

(5) HashTable is considered to be a legacy class, and if you seek to modify Map during iteration, you should use CocurrentHashMap.

two。 How to decide whether to choose 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.

What are the similarities and differences between 3.ArrayList and Vector?

ArrayList and Vector are very similar in many cases.

(1) both are index-based and internally supported by an array.

(2) both maintain the insertion order, and we can get the elements according to the insertion order.

(3) the iterator implementation of ArrayList and Vector is fail-fast.

(4) both ArrayList and Vector allow null values, or you can use index values to randomly access elements.

Here are the differences between ArrayList and Vector.

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

(2) ArrayList is faster than Vector because it will not overload because of synchronization.

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

What is the difference between 4.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. Array does not provide as many features as ArrayList, such as addAll, removeAll, and iterator. Although ArrayList is obviously a better choice, there are times when Array is easier to use.

(1) if the size of the list has been specified, in most cases it is to store and traverse them.

(2) for traversing basic data types, although Collections uses autoboxing to ease coding tasks, it can be slow to work on lists of primitive types of specified size.

(3) if you want to use multidimensional arrays, it is easier to use [] than List.

What is the difference between 5.ArrayList and LinkedList?

Both ArrayList and LinkedList implement the List interface, but there are some differences between them.

(1) ArrayList is an index-based data structure supported by Array, so it provides random access to elements with a complexity of O (1), but LinkedList stores a series of node data, each node is connected to the previous and next nodes. So, although there is a way to get an element using an index, the internal implementation is to traverse from the starting point, traverse to the node of the index, and then return the element, with a time complexity of O (n), which is slower than ArrayList.

(2) it is faster to insert, add, and delete an element in LinkedList than ArrayList, because when an element is inserted in the middle, it does not involve changing the size of the array or updating the index.

(3) LinkedList consumes more memory than ArrayList because each node in LinkedList stores references to front and back nodes.

6. Which collection classes provide random access to elements?

The ArrayList, HashMap, TreeMap, and HashTable classes provide random access to elements.

What is 7.EnumSet?

Java.util.EnumSet is a collection implementation that uses enumerated types. When the collection is created, all elements in the enumeration collection must come from a single specified enumeration type, either explicit or implicit. EnumSet is out of sync and elements with a value of null are not allowed. It also provides some useful methods, such as copyOf (Collection c), of (E first,E... Rest) and complementOf (EnumSet s).

8. Which collection classes are thread safe?

Vector, HashTable, Properties, and Stack are synchronous classes, so they are thread-safe and can be used in a multithreaded environment. Java1.5 concurrent API includes collection classes that allow modification during iteration, because they all work on clones of the collection, so they are safe in a multithreaded environment.

9. What is a concurrent collection class?

The Java1.5 concurrent package (java.util.concurrent) contains thread-safe collection classes that allow the collection to be modified during iteration. The iterator is designed to be fail-fast and throws a ConcurrentModificationException. One is classified as CopyOnWriteArrayList, ConcurrentHashMap and CopyOnWriteArraySet.

What is 10.BlockingQueue?

Java.util.concurrent.BlockingQueue is a queue that waits for the queue to become non-empty when retrieving or removing an element; when an element is added, it waits for free space in the queue. The BlockingQueue interface is part of the Java collection framework and is mainly used to implement the producer-consumer pattern. We don't need to worry about waiting for producers to have free space or consumers to have available objects, because it's all handled in BlockingQueue's implementation class. Java provides implementations of centralized BlockingQueue, such as ArrayBlockingQueue, LinkedBlockingQueue, PriorityBlockingQueue, SynchronousQueue, and so on.

11. What are queues and stacks and list their differences?

Both the stack and the queue are used to pre-store data. Java.util.Queue is an interface, and its implementation class is in the Java concurrent package. Queues allow FIFO to retrieve elements, but this is not always the case. The Deque interface allows you to retrieve elements from both sides.

A stack is similar to a queue, but it allows last-in, first-out (LIFO) retrieval of elements.

Stack is a class that extends from Vector, while Queue is an interface.

What is the 12.Collections class?

Java.util.Collections is a utility class that contains only static methods that manipulate or return collections. It contains polymorphic algorithms for manipulating sets and returns a new collection and other content supported by the specified set. This class contains methods for set framework algorithms, such as half search, sorting, mixing, and reverse.

What is the interface between 13.Comparable and Comparator?

If we want to use the sorting method of Array or Collection, we need to implement Java to provide Comparable interface in the custom class. The Comparable interface has a compareTo (T OBJ) method, which is used by sorting methods. We should override this method so that if the "this" object is smaller, equal, or larger than the passed object parameter, it returns a negative integer, 0, or positive integer. However, in most practical cases, we want to sort by different parameters. For example, as a CEO, I want to rank employees based on salary, and a HR wants to rank them by age. This is the scenario where we need to use the Comparator interface, because the Comparable.compareTo (Object o) method implementation can only sort based on one field, and we cannot select fields according to the needs of object sorting. The implementation of the compare (Object o1, Object O2) method of the Comparator interface needs to pass two object parameters. If the first parameter is smaller than the second, it returns a negative integer; if the first is equal to the second, it returns 0; if the first is larger than the second, it returns a positive integer.

What is the difference between 14.Comparable and Comparator interfaces?

The Comparable and Comparator interfaces are used to sort collections or arrays of objects. The Comparable interface is used to provide natural sorting of objects, and we can use it to provide sorting based on a single logic. The Comparator interface is used to provide different sorting algorithms, and we can choose which Comparator to use to sort a given set of objects.

15. How do we sort a set of objects?

If we need to sort an array of objects, we can use the Arrays.sort () method. If we need to sort a list of objects, we can use the Collection.sort () method. Both classes have an overloaded method sort () for natural sorting (using Comparable) or standards-based sorting (using Comparator). Collections uses array sorting internally, and both of them have the same performance, except that Collections takes time to convert lists to arrays.

16. When a collection is passed to a function as an argument, how can you ensure that the function cannot modify it?

Before passing as an argument, we can use the Collections.unmodifiableCollection (Collection c) method to create a read-only collection, which ensures that any operation that changes the collection throws a UnsupportedOperationException.

17. How do we create a collection of synchronized from a given collection?

We can use Collections.synchronizedCollection (Collection c) to get a synchronized (thread-safe) collection based on the specified collection.

18. What are the general algorithms implemented in the set framework?

The Java collection framework provides common algorithm implementations, such as sorting and searching. The Collections class contains these method implementations. Most algorithms operate on List, but some are available for all types of collections. Some algorithms include sorting, searching, mixing, maximum and minimum values.

19. What is the capital O? How many examples?

The uppercase O describes the performance of an algorithm in terms of a series of elements in the data structure. The Collection class is the actual data structure, and we usually use uppercase O to select the collection implementation based on time, memory, and performance. For example, the get (index I) of the example 1:ArrayList is a constant time operation that does not depend on the number of elements in the List. So its performance is O (1). Example 2: the performance of a linear search for an array or list is O (n), because we need to traverse all the elements to find the elements we need.

20. What are the best practices related to the Java collection framework?

(1) choose the correct collection type as needed. For example, if the size is specified, we will choose Array instead of ArrayList. If we want to traverse a Map based on the insertion order, we need to use TreeMap. If we don't want to repeat it, we should use Set.

(2) some collection classes allow the initial capacity to be specified, so if we can estimate the number of storage elements, we can use it to avoid re-hashing or resizing.

(3) programming based on the interface, not the implementation, which allows us to easily change the implementation later.

(4) always use type-safe generics to avoid ClassCastException at runtime.

(5) using the immutable classes provided by JDK as the key of Map, you can avoid implementing hashCode () and equals () yourself.

(6) use Collections utility classes whenever possible, or get read-only, synchronized, or empty collections instead of writing your own implementation. It will provide code reusability, which is more stable and maintainable.

At this point, I believe you have a deeper understanding of "what are the interview questions about the Java collection". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Development

Wechat

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

12
Report