Java basic interview questions and answers (b)

container

18. java container has what?

Common container catalog:

 

19. Collection and Collections What is the difference?

  • java.util.Collection is a collection of interfaces (the top-level interface to a set of classes). It provides a common set of interface methods objects for basic operations. Collection interface has many specific implementation in the Java class libraries. Significance of the Collection interface is provided to maximize the unified mode of operation for a variety of specific collections that have a direct interface to inherit List and Set.

  • Collections is a collection of tools class / class-help, which provides a series of static methods for the collection of elements to sort, search, and other thread-safe operations.


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

 

21. HashMap and Hashtable What is the difference?

  • hashMap method contains the HashTable removed, but with the containsValue () and containsKey () method.

  • hashTable synchronization, and HashMap is asynchronous forced hashTable higher efficiency.

  • hashMap allow empty keys, and hashTable not allowed.

 

22. How do you decide to use HashMap or TreeMap?

Map of the insertion, deletion and locating elements such operations, the HashMap is the best choice. However, if you need an ordered collection of key traverse, TreeMap is a better choice. Based on the size of your collection, and perhaps add an element to the HashMap will be faster, change the map to traverse ordered key to TreeMap.

 

23. HashMap talk about the realization of the principle?

HashMap Overview: HashMap Map is a non-synchronous interface hash table implementation. This implementation provides all of the optional map operations, and allows the use null null values ​​and keys. This class does not guarantee the order of mapping, in particular, it does not guarantee that the order lasts forever. 

HashMap data structures: the java programming language, the basic structure is the two kinds, one is an array, a further analog pointer (reference), all these data structures can be constructed in two basic structures, but also HashMap No exception. HashMap is actually a "hash list" data structure, i.e., a combination of arrays and lists.

When we go Hashmap in put elements, first of all re-calculated hash values ​​based on key's hashcode, eradicate hash worth to the position of the element in the array (index), if the array at that location has been stored for other elements, then this position of the elements on the list will be stored in the form, the newly added on the head of the chain, the first chain was added into the tail. If this position is not in the array element, the element can be directly put on the position of the array.

Note Jdk 1.8 HashMap in the realization of optimized, when the data node in the list of more than eight, the list will be converted into red-black tree to improve query efficiency, from the original O (n) to O (logn)

 

24. talk about HashSet implementation principle?

  • By the underlying implementation HashSet HashMap

  • HashSet value stored on the key HashMap

  • HashMap value of unified PRESENT

 

What is the difference 25. ArrayList and LinkedList is?

The most obvious difference is the underlying data structure is an array ArrrayList, support random access, while the underlying data structure is a two-way circular linked list LinkedList does not support random access. Subscript an element, ArrayList time complexity is O (1), while LinkedList is O (n).

 

26. How to achieve conversion between arrays and List?

  • List transformed into arrays: toArray method calls the ArrayList.

  • Array converted into List: Arrays of calling asList method.


What is the difference 27. ArrayList and Vector are?

  • Vector is synchronized, but not ArrayList. However, if you are seeking to make changes to the list when iteration, you should use CopyOnWriteArrayList. 

  • Vector faster than ArrayList, because it has synchronized, not overloaded. 

  • ArrayList is more common, because we can use the Collections utility class to easily obtain the synchronization list and read-only list.

 

What is the difference 28. Array and ArrayList?

  • Array basic types and can accommodate objects, while receiving only ArrayList object. 

  • Array is specified size, the size is fixed ArrayList. 

  • Array does not provide ArrayList so versatile, such as addAll, removeAll iterator, and so on.

 

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

poll () and remove () are removed from a queue element, but poll () Gets the element at the time of failure will return empty, but remove () will throw an exception on failure.

 

30. Which collection class thread safe?

  • vector: it is more a synchronization mechanism (thread-safe) than the arraylist, because less efficient, now is not recommended. In web applications, especially the front page, often inefficient (page response rate) is a priority.

  • statck: Stack class, last-out.

  • hashtable: the more secure than the thread hashmap.

  • enumeration: enumeration, the equivalent of iterators.

 

31. What iterator Iterator that?

Iterator is a design pattern, it is an object, and it can traverse the selected target sequence, and the developer does not need to understand the underlying structure of the sequence. Iterator is often referred to as "lightweight" object because it created a small price.

 

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

The Java Iterator function is relatively simple, and can only move one way:

(1) Use iterator () Returns a container requires Iterator. When the first call Iterator next () method, which returns the first element of the sequence. Note: iterator () method is java.lang.Iterable interface is inherited Collection.

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

(3) using hasNext () to check whether there are elements in the sequence.

(4) using remove () will return the iterator new elements removed.

Iterator Java Iterator is the simplest implementation for the design of ListIterator List has more features, it can traverse the List in both directions, you can also insert and delete elements from List.


33. Iterator and ListIterator What is the difference?

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

    • Iterator of the collection can only be traversed forward, ListIterator both before to be backward. 

    • ListIterator implements Iterator interface, and include other features, such as: adding elements to replace elements, obtaining an index before and after an element, and so on.

Guess you like

Origin www.cnblogs.com/donleo123/p/11621027.html