Java Collection framework grooming (with classic interview questions)

Source: Internet
Author: User
Tags concurrentmodificationexception

The Java Collections Framework is the architecture that Java provides for defining, manipulating, and managing a collection of interfaces, including a set of interface classes.

1. Overall framework

There are two main types of Java Container class libraries: collection and map. The hierarchy is as follows:

The Blue oval box is an interface class (not instantiated), and the black rectangle is the implementation class or subclass.

java.util.Collection [I]+--java.util.list [I] +--java.util.arraylist [c] +--java.util.linkedlist [C] +--java.util.ve ctor [C] +--java.util.stack [C]+--java.util.set [i] +--java.util.hashset [c] +--java.util.sortedset [i] +--j Ava.util.TreeSet [C] java.util.Map [I]+--java.util.sortedmap [I] +--java.util.treemap [c]+--java.util.hashtable [c]+-] -java.util.hashmap [C]+--java.util.linkedhashmap [C]+--java.util.weakhashmap [C] [I]: interface [C]: Class 2. Collection InterfaceCollection is the most basic set interface, and a collection represents a set of objects that are called collection elements. Sub-categories are as follows:
  • List: Ordered, elements are repeatable collections. because the user can use the index to access the elements in the list.
    • ArrayList: The underlying data structure uses an array that implements a variable-sized array. Non-synchronous (unsynchronized). Good at random access, fast query, insert delete moving element is slow. Method: Size (), IsEmpty (), Get (), set (), add ().
    • LinkedList: The linked list data structure used by the underlying. Non-synchronous. Remove and delete the elements quickly, random access (query) poor. Provides an additional Get,remove,insert method at the first or the end of the LinkedList. These operations make the LinkedList available as a stack (stack), queue, or two-way queue (deque).
    • Vector: is similar to ArrayList, but is synchronous (synchronized), when a iterator is created and is being used, and another thread changes the state of the vector, Concurrentmodificationexception is thrown when the iterator method is called, so the exception must be caught.
      • Stack: inherits from Vector, implementing a last-in-first-out stack. The stack provides 5 additional ways to make the vector available as a stack. Basic push () and pop (), and Peek () gets the top element of the stack, empty () test stack is blank, and search () detects the position of an element in the stack. Stack has just been created as an empty stack.
  • Set: unordered, the element is not a duplicate of the collection because there is no index. Set has a maximum of one null element.
    • HashSet: The underlying data structure is a hash table. Non-synchronous. Guaranteed uniqueness of elements: Hashcode and Equals (). Suitable for access and lookup.
      • linkedhashset: linked list + hash table. The insertion order of elements needs to be maintained, so performance is slightly lower than hashset, but when iterating over all elements in the set (traversal) will have good performance (the list is suitable for traversal).
    • TreeSet: The underlying data structure is a two-fork tree. Ordered, sorted by binary tree. Guarantee the uniqueness of the element: CompareTo ().
  • Queue: first in, out of the container. new element insertion (offer) to the end of the queue, the Access Element (poll) operation returns the element in the queue header, and does not allow random access to the elements in the queue.
    • Priorityqueue: priority queue, the order in which the queue elements are saved is not sorted according to the order in which the queues are queued, but by the size of the queue elements.
3. Map InterfaceSave the data with a "mapping relationship." The map collection holds two sets of values, one for storing the key in the map, and the other for storing the value in the map. Both key and value can be data of any reference type. The map key does not allow duplication.
    • HashTable: Sync. NULL is not allowed.
    • HASHMAP: Non-synchronous. Allow NULL, which is null value and null key (up to one).
    • Weekhashmap: An improved hashmap that implements a "weak reference" to key: If a key is no longer referenced externally, the element can be recycled by GC.
    • TreeMap: Orderly.

4. Main implementation class comparison

4.1 Vector and ArrayList

The same point: all implements the list interface, the elements are ordered repeatable, all based on the data structure of the array, all allow the direct ordinal index element, so the randomness is better, suitable for querying, but the deletion of mobile data is relatively slow (with LinkedList);

Different points: vector is thread-synchronized, so it is also thread-safe, and ArrayList is thread-asynchronous, is not secure, vector because of the use of the Synchronized method (thread-safe) so that the performance is worse than ArrayList In the collection, the use of large data volume data, vector has some advantages.

4.2ArrayList and LinkedList
The same point: all implement the list interface, the elements can be ordered and repeatable;

Different points: implementation: ArrayList is the realization of the data structure based on dynamic array, LinkedList data structure based on the linked list; efficiency: For random access get and set,arraylist better than LinkedList, However, add and remove,linedlist are more dominant for new and deleted operations. Because LinkedList uses a doubly linked list for storage, index data by ordinal is required to traverse forward or backward, but when inserting data, you only need to record the item's front and back items, and ArrayList each insertion of data to move the insertion point and all subsequent data. This point depends on the actual situation. If you insert or delete only a single piece of data, the ArrayList speed is better than LinkedList. But if bulk random insert deletes data, LinkedList speed is much better than ArrayList.

4.3 HashMap and TreeMap
All implement the Map interface, which is a mapping relationship of key-value pairs.

HashMap quickly find its content through hashcode, and all elements in TreeMap remain in a fixed order, and if you need to get an ordered result you should use TREEMAP (the order of the elements in HashMap is not fixed).
HashMap is the best choice for inserting, deleting, and locating elements in a map. But if you want to traverse the key in natural order or in a custom order, TreeMap is better.

The implementation of Hashcode () and Equals () is clearly defined by the key class that is required to be added using HashMap.
The elements in the two map are the same, but the order is different, causing hashcode () to be different.
Do the same test:
In HashMap, the same value of the map, the order is different, equals, false;
And in TreeMap, the same value of the map, the order is different, equals, true, the treemap in Equals () is sorted out in order.

4.4 Hashtable and HashMap
All implement the Map interface, which is a mapping relationship of key-value pairs. Similar to the Hash/rehash algorithm, the performance difference is not small.

The HashMap is not synchronous, is thread insecure, and allows a null key and multiple null values. Hashtable are synchronous, thread-safe, and do not allow null keys or values.

Hashtable has a contains () method in which only ContainsKey () and Containsvalue () in HashMap detect if there is a key or value in the hash table, and there is a return of true.

Summary:The implementation of the list interface ArrayList, LinkedList, Vector and other orderly, and realize the sortedxx Treexx order: TreeSet, TreeMap;    Use Hashset,hashmap in addition to treeset,treemap when sorting is required, because they are more efficient.       If it involves operations such as stacks, queues, and so on, you should consider using the list, for quick insertions, for deleting elements, should use LinkedList, and if you need to quickly randomly access elements, you should use ArrayList.    Classes of synchronization: Vector, HashTable If the program is in a single-threaded environment, or if access is done only in one thread, it is more efficient to consider a class that is not synchronized, and if multiple threads might manipulate a class at the same time, the synchronized class should be used.    Pay particular attention to the operation of the hash table, and the object that is the key is to correctly replicate the Equals () and Hashcode () methods. The container class can only hold object references (pointers to objects), rather than copy object information to a sequence of numbers.    Once the object is placed inside the container, the object's type information is lost. Try to return the interface rather than the actual type, such as returning a list instead of ArrayList, so that if you need to change ArrayList to LinkedList later, the client code does not have to be changed. This is for abstract programming. Note:1. Collection does not have a get () method to get an element. Elements can only be traversed by iterator (). 2. Set and collection have identical interfaces. 3, List, you can use the Get () method to remove one element at a time. Use numbers to select one of a bunch of objects, get (0) .... (Add/get) 4, the general use of ArrayList. Use LinkedList to construct stack stacks, queue queues.      5, map with put (K,V)/get (k), you can also use ContainsKey ()/containsvalue () to check if it contains a key/value. HashMap will use the object's hashcode to quickly find the key. 6, the element in the map, you can extract the key sequence, the value sequence alone. Use Keyset () to extract the key sequence and generate a set for all keys in the map. Use values () to extract the value sequence and generate a collection for all values in the map. Why a build set, a Build collection? That's because key is always unique, and value allows repetition. 5. Face Test Set

1. What is the Java Collection API

The Java Collection Framework API is a unified framework for representing and manipulating collections, which contains interfaces, implementation classes, and algorithms that help programmers complete some programming. In short, the API does the following things on the top: more labor-efficient programming, improved City program speed and code quality, non-associative APIs to improve interoperability, saving on learning to use new API costs, saving time to design new APIs, and encouraging and facilitating software reuse.

Specifically, there are 6 sets of interfaces, the most basic is the collection interface, by three interface set, List, SortedSet inheritance, the other two interfaces are map, SortedMap, these two interfaces do not inherit collection, represents a mapping rather than a true collection.

2. What is iterator

Some collection classes provide a function of content traversal through the Java.util.Iterator interface. These interfaces allow iterating over the collection of objects. Manipulate each element object in turn. When iterators is used, a collection snapshot is included when the iterator is obtained. It is usually not recommended to modify the collection province when traversing a iterator.

3. What is the difference between iterator and listiterator?

Iterator: The collection can only be traversed forward, and is suitable for getting removed elements. Listierator: Inherits the iterator, can traverse the bidirectional list, also supports the element modification.

4. What is Haspmap and map?

Map is an interface, a part of the Java collection framework, used to store key-value pairs, and HashMap is a class that implements a map using a hashing algorithm.

5. What is the difference between HashMap and Hashtable? Compare Hashtable VS HashMap

Both are used to obtain data in a key-value way. Hashtable is one of the original collection classes (also known as The Legacy Class). HashMap is added as part of the new collection framework in version 1.2 of JAVA2. There is a difference between them:

HashMap and Hashtable are roughly equivalent except for non-synchronous and null values (HASHMAP allows null values as key and value, while Hashtable cannot).

HashMap cannot guarantee that the order of the mappings is constant, but as a subclass of HashMap Linkedhashmap, if you want to predict the sequential iterations (by default in order of insertion), you can easily replace the HashMap, if you use Hashtable is not so easy.

The HashMap is not synchronous, and Hashtable is synchronous.

Iterative HashMap uses a fast failure mechanism, and Hashtable is not, so this is the design consideration.

6. What does synchronization mean in the context of Hashtable?

Synchronization means that only one thread can modify the hash table at a point in time, and any thread needs to acquire an object lock before executing the Hashtable update operation, while other threads wait for the lock to be released.

7. What is called fast failure feature

Rapid failure from a high-level hierarchy is a response of a system or software to its failure. A fast-failing system is designed to instantly report any failure condition that could lead to failure, and it is often used to stop normal operations rather than trying to continue with potentially flawed work. When a problem occurs, the system quickly fails to make a false error alarm immediately. In Java, quick failures are related to iterators. If a iterator is created on the collection object, other threads are "structured" to modify the collection object, and the concurrent Modification exception (Concurrentmodificationexception) is thrown.

8, how to make HashMap synchronization?

The HASHMAP can be synchronized by using map m = Collections.synchronizedmap (HASHMAP).

9, when to use Hashtable, when to use HashMap

The basic difference is that Hashtable synchronous HashMap is not, so whenever there are multiple threads accessing the same instance, the Hashtable should be used instead of hashmap. Non-thread-safe data structures provide better performance.

If there is a possibility in the future-you need to get a scheme for key-value pairs in order, HashMap is a good choice because there is a subclass of HashMap Linkedhashmap. So if you want predictable sequential iterations (by default in the order of insertions), you can easily replace HashMap with Linkedhashmap. It is not so easy to use the hashtable of the opposite view. At the same time if there are multiple threads accessing Hashmap,collections.synchronizedmap () can be substituted, generally hashmap more flexible.

10. Why is the vector considered to be obsolete or unofficially deprecated? Or why we should always use ArrayList instead of vectors.

You should use ArrayList instead of vectors because by default you are non-synchronous, vectors synchronize each method, you almost never do that, and usually you want to synchronize the entire sequence of operations. Synchronizing a single operation is also unsafe (if you iterate over a vector, you still need to lock it to prevent other threads from changing the collection at the same time). and more efficient. Of course there is also the cost of locking even if you don't need it, this is a bad way to access by default. You can always use Collections.sychronizedlist to decorate a collection.

In fact, vectors combine a set of "mutable arrays" and synchronize the implementation of each operation. This is another design flaw. Vector also has some legacy methods in enumerations and elements to get methods that are different from the list interface, if these methods in the code programmers tend to want to use it. Although enumerations are faster, they cannot be checked if the collection is modified during iteration, which can cause problems. For many of these reasons, Oracle has never claimed to abandon vectors.

Reference Link: http://blog.sina.com.cn/s/blog_a345a8960101k9vx.htmlhttp://www.cnblogs.com/leeplogs/p/5891861.htmlhttp:// blog.csdn.net/zsm653983/article/details/7562324

Java Collection framework grooming (with classic interview questions)

Related Article

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.