Interview questions _48_to_65_java the face of the set frame

Source: Internet
Author: User

This section also contains data structures, algorithms, and an array of interview questions

The difference between List, Set, Map, and Queue (answer)
List is an ordered set that allows elements to be duplicated. Some of its implementations can provide constant access time based on subscript values, but this is not guaranteed by the List interface. Set is an unordered collection.

The poll () method and the Remove () method differ?
Both poll () and remove () remove an element from the queue, but poll () returns NULL when the element fails, but the remove () throws an exception when it fails.

What is the difference between Linkedhashmap and priorityqueue in Java? Answer
Priorityqueue the element that guarantees the highest or lowest priority is always on the head of the queue, but the order in which Linkedhashmap is maintained is the order in which the elements are inserted. There is no guarantee of order when traversing a priorityqueue, but the Linkedhashmap class guarantees that the traversal order is the order in which the elements are inserted.

ArrayList and LinkedList are not different? Answer
The most obvious difference is that the arrraylist underlying data structure is an array that supports random access, while LinkedList's underlying data structure book list does not support random access. Using the subscript to access an element, the time complexity of the ArrayList is O (1), while the LinkedList is O (n). For more details, see the answer.

52) in which two ways to achieve the sorting of the collection? Answer
You can use an ordered set, such as TreeSet or TREEMAP, or you can use a sequential collection, such as list, and then sort by collections.sort ().

How do I print a group in Java? (Answer answer)
You can use the arrays.tostring () and arrays.deeptostring () methods to print an array. Because the array does not implement the ToString () method, if you pass the array to the SYSTEM.OUT.PRINTLN () method, you will not be able to print the contents of the group, but arrays.tostring () can print each element.

LinkedList in Java is a unidirectional or doubly linked list? Answer
is a doubly linked list, you can check the source code of the JDK. In Eclipse, you can use the shortcut key CTRL + T to open the class directly in the editor.

What is the tree implementation of TREEMAP in Java? Answer
The TREEMAP in Java is implemented using a red-black tree.

What is the difference between Hashtable and HashMap? Answer
These two classes have many different places, some of which are listed below:
A) Hashtable is a class left over from JDK 1, and HashMap is later added.
b) Hashtable is synchronous and slower, but HashMap has no synchronization policy, so it will be faster.
c) Hashtable does not allow an empty key, but HASHMAP allows a null key to appear.
See the answers to more of the differences.

HashSet in Java, how does the interior work? (Answer answer)
The interior of the HashSet is implemented using HashMap. Since the MAP requires key and value, all keys have a default value. Similar to Hashmap,hashset does not allow duplicate keys, only a null key is allowed, meaning that only one null object is allowed in HashSet.

58) Write a piece of code to remove an element while traversing ArrayList? Answer
The key to the problem is whether the interviewer is using the ArrayList remove () or the Iterator remove () method. Here is a sample code that uses the correct way to implement the sample code that removes the element during traversal without the concurrentmodificationexception exception.

59) Can we write a container class by ourselves and then use the For-each loop code?
Yes, you can write a container class of your own. If you want to use the enhanced loops in Java to traverse, you only need to implement the Iterable interface. If you implement the Collection interface, this property is the default.

The default size of ArrayList and HashMap is the majority? Answer

In Java 7, the default size of ArrayList is 10 elements, and the default size of HashMap is 16 elements (must be a power of 2). This is the code snippet for the ArrayList and HashMap classes in Java 7:

12345 //from Arraylist.java JDK 1.7 private static final int default_capacity = 10 ;   //from Hashmap.java JDK 7 static final int default_initial_ Capacity = 1 << 4 //aka

61) is it possible that two unequal objects have the same hashcode?
It is possible that two unequal objects might have the same hashcode value, which is why there is a conflict in the HashMap. The provisions of the equal Hashcode value only say that if two objects are equal, the same hashcode value must be there, but there is no provision for unequal objects.

62) Two of the same objects will have different hash code?
No, according to the hash code, this is not possible.

63) Can we use random numbers in Hashcode ()? Answer
No, because the hashcode value of the object must be the same. See the answer for more knowledge about overriding the Hashcode () method in Java.

In Java, what is the difference between Comparator and comparable? Answer
The comparable interface is used to define the natural order of objects, and comparator is typically used to define the order in which users are customized. Comparable always have only one, but you can have multiple comparator to define the order of the objects.

65) Why do I need to override the Hashcode method when overriding the Equals method? Answer
Because there are mandatory specification designations that require both rewriting hashcode and equal are methods, many container classes, such as HashMap, HashSet, are dependent on the provisions of hashcode and equals.

Interview questions _48_to_65_java the face of the set frame

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.