The latest collection of JAVA interview questions (3)

container

18. What are the java containers?

Catalog of commonly used containers:
Insert picture description here
19. What is the difference between Collection and Collections?

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

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

20. What is the difference between List, Set and Map?
Insert picture description here
21. What is the difference between HashMap and Hashtable?

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

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

  • HashMap allows empty key values, while hashTable does not.

22. How to decide whether to use HashMap or TreeMap?

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

23. Tell me about the implementation principle of HashMap?

HashMap overview: HashMap is an asynchronous implementation of the Map interface based on the hash table. 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 will last forever.

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

When we put an element in the Hashmap, first recalculate the hash value according to the hashcode of the key, and get the position (subscript) of this element in the array by eliminating the hash value. If the array has already stored other elements at that position, then The element at 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 added will be placed at the end of the chain. If there is no element at that position in the array, the element will be placed directly at that position of the array.

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

24. Tell me about the implementation principle of HashSet?

  • The bottom 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 circular linked list, which does not support random access. Using subscripts to access an element, the time complexity of ArrayList is O(1), while LinkedList is O(n).

26. How to realize the conversion between array and List?

  • Convert List to Array: call the toArray method of ArrayList.

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

27. What is the difference between ArrayList and Vector?

  • Vector is synchronous and thread-safe, while ArrayList is not. However, if you are looking to make changes to the list while iterating, you should use CopyOnWriteArrayList.

  • ArrayList is faster than Vector, because it has synchronization, it will not be overloaded, and it will cost a lot.

  • ArrayList is more versatile because we can easily obtain synchronized lists and read-only lists using the Collections utility class.

  • Data capacity growth: Both have an initial capacity size and use linear continuous storage space. When the number of stored elements exceeds the capacity, the storage space of both needs to be increased. Vector doubles the original size, and ArrayList increases the original size. 0.5 times.

28. What is the difference between Array and ArrayList?

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

  • Array has a specified size, while ArrayList has a fixed size.

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

29. What is the difference between poll() and remove() in Queue?

Both poll() and remove() remove an element from the queue, but poll() will return null when it fails to get the element, but it will throw an exception when remove() fails.

30. Which collection classes are thread safe?

  • vector: It has one more synchronization mechanism (thread safety) than arraylist. Because of its low efficiency, it is not recommended to use it now. In web applications, especially the front page, efficiency (page response speed) is often given priority.

  • statck: Stack type, first in, last out.

  • Hashtable: It is more thread-safe than hashmap.

  • enumeration: Enumeration, equivalent to iterator.

31. What is Iterator?

Iterator is a design pattern, it is an object, it can traverse and select objects in the sequence, and the developer does not need to understand the underlying structure of the sequence. Iterators are often referred to as "lightweight" objects because of the low cost of creating them.

32. How to use Iterator? What are the characteristics?

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

(1) Use the iterator() method to require the container to return an Iterator. When the next() method of Iterator is called for the first time, it returns the first element of the sequence. Note: The iterator() method is the java.lang.Iterable interface, which 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 newly returned element of the iterator.

Iterator is the simplest implementation of Java iterators. The ListIterator designed for List has more functions. It can traverse the List in two directions, as well as insert and delete elements from the List.

33. What is 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 forward or backward.

  • ListIterator implements the Iterator interface and contains other functions, such as adding elements, replacing elements, getting the index of the previous and next element, and so on.

Guess you like

Origin blog.csdn.net/weixin_42120561/article/details/114224488