Java Basics-004

Source: Internet
Author: User
Tags comparable

---restore content starts---

basic interface of the 14.Java collection class framework

  The collection class interface specifies a set of objects called elements. Each specific implementation class of the collection class interface can optionally save and sort the elements in its own way. Some collection classes allow duplicate keys, and some do not.

The Java collection provides a set of well-designed interfaces and classes that support the manipulation of a set of objects. The most basic interfaces within the Java Collection class are:

I> Collection: Represents a group of objects, each of which is its child element

Ii> Set: Cllection that does not contain duplicate elements

iii> List: Sequential collection, and can contain repeating elements

Iv> Map: You can map a key (key) to an object with a value, and the key cannot be duplicated

The collection class does not implement the cloneable and serializable interfaces:

The semantics and meanings of cloning (cloning) or serialization (serialization) are related to specific implementations. Therefore, the specific implementation of the collection class should determine how to be cloned or serialized.

15. Iterators (iterator)

The iterator interface provides a number of age-old collection elements for iterative methods. Each collection class contains an iterative method that can return an iterator instance. Iterators can delete elements of the underlying collection during the iteration, but they cannot call the collection's remove (Object obj) directly, and can be removed through the Remove () method of the iterator.

Iterator and Listiterator's area ratio:

The i> iterator can be used to traverse the set and list collection, but Lisriterator can only traverse the list.

Ii> iterator to a collection can only be forward traversal, listiterator can be either forwards or backwards.

Iii> Listiterator implements the iterator interface and includes other functions, such as adding elements, replacing elements, getting the index of the previous element and the next element, and so on.

  16. Fast failure (Fail-fast) and security Failure (Fail_saft)

Iterator's security failure is based on a copy of the underlying collection, so it is not affected by modifications on the source collection. All of the collection classes below the Java.util package are fast failures, and all classes under the Java.util.concurrent package fail safely. A fast-failing iterator throws an concurrentmoddificationexception exception, and a security-failed iterator never throws such an exception.

How the 17.HashMap worksJavathe HashMap in is a key-value pair that stores elements in the form. HashMap requires a hash function that uses the hashcode () and Equals () methods to add and retrieve elements to the collection/collection. When the put () method is called, HashMap calculates the hash value of the key, and then stores the key-value pair on the appropriate index in the collection if the key already exists, value will be updated to the new value. Some of the important features of HashMap are its capacity (capacity), load factor (payload factor), and expansion limit (thresh old resizing). 18.hashCode () and Equals () methodsin JavaThe HashMap uses the hashcode () and Equals () methods to determine the index of a key-value pair, which is used when the value is obtained by key. If these two methods are not implemented correctly, two different keys may have the same hash value, and therefore may be considered equal by the collection. Moreover, these two methods are also used to discover duplicate elements. So the realization of these two methods is very important to the accuracy and correctness of hashmap. The difference between 19.HashMap and Hashtable

HashMap and Hashtable both implement the map interface, so many features are very similar, but there are different points:

I> HashMap allows keys and values to be null, and Hashtable does not allow keys or values to be null.

Ii> Hashtable is synchronous, and HashMap is not. As a result, HashMap is more suited to single-threaded environments, while Hashtable is suitable for multi-threaded environments.

Iv> HashMap provides a collection of keys that are available for application iterations, so HashMap is a quick failure. On the other hand, Hashtable provides an enumeration of keys (enumeration)

20. Differences between arrays (array) and list (ArrayList)

The i> array can contain basic data types and object types, and ArrayList can only contain object types.

The ii> array size is fixed and the size of the ArrayList is dynamically variable.

Iii> Arrarylist provides more methods and features. For example: AddAll (), RemoveAll (), iterator () and so on. For basic data types, collections use automatic boxing to reduce the amount of coding effort. However, this approach is relatively slow when dealing with fixed-size base data types.

  

21. The difference between ArrayList and Linklists

Both ArrayList and linklist implement the list interface, which has a different point:

I> ArrayList is an index-based data interface, and its underlying is an array. It can randomly access elements with an O (1) time complexity. In contrast, linklist is a list of elements that stores its data, and each element is connected to its previous and subsequent elements, in which case the time complexity of finding an element is O (n).

Ii> is relative to ArrayList. linklist Insert, add, and delete operations are faster because when an element is added anywhere in the collection, it is not necessary to recalculate or update the index as if it were an array.

iii> linklist accounts for more memory than ArrayList, because Linklist stores two references for each node, one to the previous element and one to the latter.

22.Comparable and Comparator interface

Java provides a comparable interface that contains only one CompareTo () method. This method can be used to sort two of objects. Specifically, it returns a negative number, 0, a positive number to indicate that the input object is less than, equal to, greater than the already existing object.

Java provides a comparetor interface that contains two methods of compare () and Equals (). The Compare () method is used to sort two input parameters, return a negative number, 0, positive number to indicate that the first parameter is less than, equal to, greater than the second argument. The Equals () method requires an object as a parameter that determines whether the input parameters are equal to comparator. This method returns true only if the input parameter is also a comparator and the input parameter is the same as the current comparator's ordering result.

23.Java Priority queues (priorty queue)

The Priorty queue is a class of unbounded queues based on the priority heap, and its elements are sorted in natural order. At the time of creation, we can provide him with a comparator that is responsible for ordering the elements. Priortyqueue does not allow null values. Because they don't have a natural order, or they don't have any associated comparators. Finally, Priortyqueue is not thread-safe, and the complexity of the queue and the team's book Swords is O (log (n)).

24. Ordered arrays and unordered arrays

The greatest benefit of an ordered array is that the time complexity of the lookup is O (log (n)), and the unordered array is O (n). The disadvantage of an ordered array is that the time complexity of the insert operation is O (n), because the elements with large values need to be moved backward to make the new element free, instead, the insertion time complexity of the unordered array is constant O (1).

differences between the 25.Enumeration interface and the iterator interface

The enumeration is twice times faster than iterator and consumes less memory at the same time. However, iterator is far more secure than enumeration because other threads are not able to modify objects in the collection that is being traversed by iterator. At the same time, iterator allows the caller to delete elements from the underlying collection, which is not possible for enumeration.

the difference between 26.HashSet and TreeSet

HashSet is implemented by a hash table, so its elements are unordered. The time complexity of the add (), remove (), contains () method is O (1).

TreeSet is implemented by a tree-shaped structure in which the elements are ordered. Therefore, the time complexity of the add (), remove (), contains () method is O (log (n)).

Java Basics-004

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.