About Java Set frame face questions (including answers) under _java

Source: Internet
Author: User
Tags comparable

What's the difference between 21.HashMap and Hashtable?

(1) HashMap allows key and value to be null, while Hashtable is not allowed.

(2) Hashtable is synchronous, and HashMap is not. So HashMap is suitable for single-threaded environment, Hashtable is suitable for multi-threaded environment.

(3) A subclass of Linkedhashmap,hashmap is introduced into the Java1.4, and if you want to traverse the order, you can easily move from HashMap to Linkedhashmap, but Hashtable is not, and its order is unpredictable.

(4) HashMap provides a set for the key to traverse, so it is fail-fast, but Hashtable provides the enumeration of the key to traverse, it does not support fail-fast.

(5) Hashtable is considered a legacy class, and if you seek to modify the map at iteration time, you should use Cocurrenthashmap.

22. How to decide to choose HashMap or TreeMap?

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

What are the similarities and differences between 23.ArrayList and vector?

ArrayList and vectors are very similar in many cases.

(1) Both are indexed and internally supported by an array.

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

(3) The implementation of ArrayList and Vector iterators is fail-fast.

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

The following are the differences between ArrayList and vectors.

(1) vector is synchronous, and ArrayList is not. However, if you are looking for a change to the list at the time of the iteration, you should use Copyonwritearraylist.

(2) ArrayList is faster than vector, because it is synchronized, it will not overload.

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

What's the difference between 24.Array and ArrayList? When is the best time to use array?

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

The array is of the specified size, and the ArrayList size is fixed.

Array does not provide ArrayList so many functions, such as AddAll, RemoveAll and iterator. Although ArrayList is obviously a better choice, there are times when the array is more useful.

(1) If the size of the list is already specified, most of the cases are stored and traversed.

(2) for traversing the base data type, although collections uses automatic boxing to mitigate the encoding task, it can also become slow on the list of basic types of the specified size.

(3) If you want to use a multidimensional array, it is easier to use [] [] than list<list<>>.

What's the difference between 25.ArrayList and LinkedList?

ArrayList and LinkedList both 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 an O (1) complexity, but LinkedList stores a series of node data, each node connected to the previous and next node. So, despite the method of using an index to get elements, the internal implementation starts at the starting point, traverses the indexed node and then returns the element, with a time complexity of O (n), slower than ArrayList.

(2) inserting, adding, and deleting an element in the LinkedList is faster 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 a reference to the front and back nodes.

26. Which collection classes provide random access to elements?

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

What's 27.EnumSet?

Java.util.EnumSet is implemented using a collection of enumeration types. When a collection is created, all elements in the enumeration collection must come from a single specified enumeration type, either displayed or implied. Enumset is not synchronized and does not allow elements with null values. It also provides some useful methods, such as copyof (Collection c), of (E first,e...rest) and Complementof (Enumset s).

28. Which collection classes are thread-safe?

Vectors, HashTable, properties, and stack are synchronous classes, so they are thread-safe and can be used in multithreaded environments. The Java1.5 concurrency API includes collection classes that allow iterations to be modified because they all work on the clones of the collection, so they are safe in a multithreaded environment.

29. What are concurrent collection classes?

The Java1.5 and contract (java.util.concurrent) contains a thread-safe collection class that allows you to modify the collection at iteration time. The iterator is designed to be fail-fast and throws concurrentmodificationexception. Some of the classes are: Copyonwritearraylist, Concurrenthashmap, Copyonwritearrayset.

What's 30.BlockingQueue?

Java.util.concurrent.BlockingQueue is a queue that waits for the queue to become non-null when retrieving or removing an element, and waits for free space in the queue when an element is added. The Blockingqueue interface is part of the Java Collection framework that is used primarily to implement producer-consumer patterns. We do not need to worry about waiting for the producer to have available space, or the consumer has an object available because it is processed in the Blockingqueue implementation class. Java provides a centralized blockingqueue implementation, such as Arrayblockingqueue, Linkedblockingqueue, Priorityblockingqueue, Synchronousqueue, and so on.

31. What are the queues and stacks, listing their differences?

Both stacks and queues are used to store data in advance. Java.util.Queue is an interface whose implementation classes are in Java and in the contract. Queues allow advanced first Out (FIFO) retrieval elements, but this is not always the case. The Deque interface allows elements to be retrieved from both ends.

Stacks are similar to queues, but it allows for the retrieval of elements in LIFO (LIFO).

A stack is a class that extends from a vector, while a queue is an interface.

What is the 32.Collections class?

Java.util.Collections is a tool class that contains only static methods that manipulate or return collections. It contains a polymorphic algorithm for the operation set, which returns a new collection and some other content supported by the specified collection. This class contains methods for the collection framework algorithm, such as binary search, sorting, mixing, and reverse order.

What is the 33.Comparable and comparator interface?

If we want to use array or collection sorting methods, we need to implement Java in the custom class to provide the comparable interface. The comparable interface has a CompareTo (T OBJ) method, which is used by the sorting method. We should rewrite this method to return a negative integer, 0, or positive integer if the "This" object is smaller, equal, or larger than the passed object argument. However, in most cases, we want to sort by different parameters. For example, as a CEO, I want to sort employees based on salary, an HR wants to sort them based on age. This is the scenario where we need to use the comparator interface, because the Comparable.compareto (object o) method implementation can only be sorted based on one field, and we cannot select fields based on the needs of the object sort. The implementation of the compare (object O1, Object O2) method of the comparator interface needs to pass two object parameters, and if the first argument is smaller than the second, a negative integer is returned; if the first is equal to the second, return 0; If the first is larger than the second, returns a positive integer.

What is the difference between 34.Comparable and comparator interfaces?

The comparable and comparator interfaces are used to sort object collections or arrays. The comparable interface is used to provide a natural ordering of objects, which we can use to provide a sort based on a single logic.

The comparator interface is used to provide a different sort algorithm, and we can select the comparator to use to sort the given set of objects.

35. How do we sort a group of objects?

If we need to sort an array of objects, we can use the Arrays.sort () method. If we need to sort an object list, we can use the Collection.sort () method. Two classes have overloaded method sort () for natural sorting (using comparable) or standards-based sorting (using comparator). Collections internally uses array sorting methods, all of which have the same performance, except that collections takes the time to convert the list to an array.

36. When a collection is passed as an argument to a function, how can it be ensured that the function cannot modify it?

Before passing as a parameter, we can create a read-only collection using the collections.unmodifiablecollection (Collection C) Square method. This will ensure that any operation that changes the collection throws Unsupportedoperationexception.

37. How do we create a synchronized collection from a given collection?

We can use Collections.synchronizedcollection (Collection C) to obtain a synchronized (thread-safe) collection based on the specified collection.

38. What are the common algorithms implemented in the set framework?

The Java Collection Framework provides commonly used algorithm implementations, such as sorting and searching. The collections class contains these method implementations. Most of the algorithms are operations list, but some are available for all types of collections. Some algorithms are sorted, searched, mixed, and the maximum minimum value.

39. What is the capital O? Give me a few examples?

Uppercase O describes the performance of an algorithm in terms of a series of elements in a data structure. The collection class is the actual data structure, and we typically use uppercase O to select a collection implementation based on time, memory, and performance. For example, the instance 1:arraylist get (index i) 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 iterate through all the elements to find the required elements.

40. What are the best practices associated with the Java collection framework?

(1) Select 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, 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 could use it to avoid a hash or resize.

(3) Based on interface programming, rather than on implementation programming, it allows us to easily change implementations later.

(4) Always use type-safe generics to avoid classcastexception at run time.

(5) using the immutable class provided by JDK as the key of the map, you can avoid the realization of hashcode () and Equals ().

(6) Use the Collections tool class whenever possible, or get a read-only, synchronized, or empty collection rather than writing your own implementation. It will provide code reusability, which has better stability and maintainability.

All of the above is for the Java collection of questions, think a good friend Resolute collection bar, we can also be combined with a study: on the Java set frame face test questions (including the answer) on

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.