Interview the hardest hit area! The Java collection framework is all sorted out for you!

1. Why does the Map interface not inherit the Collection interface?

· Set is an unordered collection and does not allow duplicate elements
· List is an ordered collection and allows duplicate elements
· Map is a key-value pair
· It is regarded as a combination
of a set of keys and a set of values · Map It is designed as a collection of key-value pairs, so there is no need to inherit the Collection interface

2. The difference between HashMap and Hashtable?

· Synchronization or thread safety
· Null key and Null value
· Iteration value
· Default capacity

3. What is the difference between comparable and comparator?

The comparable interface is actually from the java.lang package
. It has a compareTo(Object obj) method to sort objects
. The comparator interface is actually from the java.util package
. It has a compare(Object obj1, Object obj2) method to Sort objects

If you need more interview materials from major companies, you can also click to enter directly and get it for free! Password: CSDN

4. How to sort the list of Objects?

· To sort the array of objects, we can use the Arrays.sort() method.
· If you want to sort the collection of objects, you need to use the Collections.sort() method

5. What is the difference between fail-fast and fail-safe?

· Fail fast quickly reports any failures. Whenever any of the problems will cause the fail fast system failures
· In the Java Fail fast iterator, iterating over the collection of objects sometimes causes concurrent modification exceptions. There are two reasons for this situation
· If a thread is iterating a collection and another A thread tries to modify the collection at the same time
. After calling the remove() method, how can we still try to modify the collection object?

6. What is the difference between Iterator, ListIterator and Enumeration?

· Enumeration interface is available in Java 1.2 version, so Enumeration is a legal and standardized interface
· Enumeration uses elements() method
· Iterator is implemented for all Java collection classes
· Iterator uses iterator method
· Iterator can only go in one direction
· ListIterator Only implemented for classes of type List
· ListIterator uses the listIterator() method

7. What is the difference between Set and List in Java?

· Set is a collection that does not allow duplicate elements
· Set has no index
· Set only allows one null value
· Set has classes: HashSet, LinkedHashMap, TreeSet
· List has index
· List allows N null values
· List can be displayed in the order of insertion
· List has classes: Vector, ArrayList, LinkedList

8. What is the difference between arraylist and vector?

· Vector was introduced in the first version of Java, which means that vector is a legally standardized class
· ArrayList was introduced in Java 1.2 and is part of the Java collection framework
· Vector is synchronized
· ArrayList is not synchronized of

9. What class implements the List interface?

· ArrayList
· LinkedList
· Vector

10. What class implements the Set interface?

· HashSet
· LinkedHashSet
· TreeSet

11. How to ensure the safety of a collection thread?

· Vector, Hashtable, Properties and Stack are all synchronized classes, so they are all thread-safe and can be used in a multi-threaded environment
· Use Collections.synchronizedList(list)) method to ensure that the list class is thread-safe
· Use The java.util.Collections.synchronizedSet() method can ensure that the set class is thread-safe

If you need more interview materials from major companies, you can also click to enter directly and get it for free! Password: CSDN

12. Is it possible to add null elements to TreeSet or HashSet?

· You can add a null to the hashset
· TreeSet also allows a null value

13. Which design pattern does Iterator conform to?

· Iterator design pattern

14. Which data structure does HashSet implement?

· HashSet implements hashmap internally

15. Why can't Collection inherit Cloneable and Serializable?

· List and Set only inherit the Collection interface
· SortedMap inherits the Map interface

16. What is the difference between array and arraylist?

· Array is similar to object collection type and has a fixed size.
· Arraylist is a collection of homogeneous and heterogeneous elements

17. What is the Properties class?

· Properties is a subclass of Hashtable. It is used to maintain a list of values, where their keys and values ​​are of String type

18.How to convert a string to an arraylist?

· Use arrayList.toArray() method

Reader benefits

Thank you for seeing here!
I have compiled a lot of 2021 latest Java interview questions (including answers) and Java study notes here, as shown below
Insert picture description here

Insert picture description here

The answers to the above interview questions are organized into document notes. As well as interviews also compiled some information on some of the manufacturers & interview Zhenti latest 2021 collection (both documenting a small portion of the screenshot) free for everyone to share, in need can click to enter signal: CSDN! Free to share~

If you like this article, please forward it and like it.

Remember to follow me!
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_49527334/article/details/114222030