Java interview series an array and collection

Source: Internet
Author: User
Tags array length comparable data structures serialization

What are the basic interfaces of the

Java Collection Class framework. The difference is what.

1. Two common frameworks for collections: The collection interface and the map interface;
The   1.1collection interface has two commonly used sub-interfaces: The list interface and the set interface. The
     1.1.1 has three common implementation classes for the list interface: The ArrayList class and the Vector class, as well as the LinkedList class.
           arraylist class: Orderly repeatable, add, delete slow, lookup fast, not thread safe
           vector class: The difference with ArrayList is that its thread-safe, Both are linear table sets that are implemented using variable-length arrays.
           linkedlist: Orderly repeatable, add, delete fast, find slow, A set of linear tables that is implemented using a two-way circular chain list.
     1.1.2 has a more common implementation class for set interfaces: HashSet classes (Implementation classes under the set interface are not allowed to have duplicate values)
    The         hashset class is implemented using the HashMap class (described below), but it uses only key values. The
  1.2map interface has two common implementation classes: The HashMap class and the Hashtable class (the difference is that the Hashtable class is thread safe
       , the following is mainly about HashMap class.)
            The HashMap class stores value values based on key values, and key is similar to the primary key in the database and cannot be duplicated, but the value can be repeated

What is an iterator (iterator).

(1), List interface and its implementation class

The list interface inherits from the collection interface. The list interface and its implementation class are variable-capacity lists that access elements in the collection by index.

Features: The elements in the set are orderly and repeatable;

Implementation class:

· ArrayList is a list implemented in Arayy mode, allowing fast random access, which is not suitable for inserting and removing element operations relative to LinkedList;

· LinkedList provides optimal sequential access for inserting and removing elements.

(2), set interface and its implementation class

The set interface also inherits from the collection interface.

Features: The elements in the collection are unordered and do not contain duplicate elements.

Implementation class:

· HashSet can quickly locate an element, it should be noted that the object in the HashSet must implement the Hashcode () method;

· TreeSet the elements to be placed in sequence.

(3), map interface and its implementation class

Map is a separate interface that does not inherit from collection. A map is a container for associating a key object with a value object.

Feature: Key objects do not allow duplicates.

Implementation class:

· HASHMAP implements a hash table with key to value mapping, and obtains the value object through the key;

· Properties are generally read in the property file into the stream, the key-value pairs of the form to save to facilitate the reading of the data.

(4), iterator interface

The iterator interface is located in the Java.util package, which is an iterator that iterates through the collection.

Collection containers such as List, Set, map, and so on themselves provide a way to handle element placement and removal, but the method of single selection elements is limited. So we're going to use iterator to select the element in the container, which converts the container into a sequence.

The differences between Collection, set, and list are as follows:

· There is no specified order between the collection objects, allowing for duplicate elements and multiple null element objects; It is the parent of set and list interfaces, and it is the most common type of set interface;

· There is no specified order between the elements of the set, no duplicate elements are allowed, and a maximum of one null element object is allowed;

· List each element object in a specified order, allowing repeating elements and multiple null element objects

 

What is the difference between iterator and listiterator.

The difference between iterator and Listiterator is as follows:
1, iterator is the parent interface of Listiterator.
2, iterator is a single column set (Collection) public to remove elements in the container.
is universal for List,set.
and Listiterator is the unique way to remove elements from the list collection.     
3, iterator features only Hashnext (), Next (), remove ();
In Listiterator, there is a method of adding a check to the elements that are being traversed, and the elements can be traversed in reverse.
this is because the container that the listiterator traverses is indexed.

 

What is the working principle of hashmap in Java?

HashMap in Java store elements in the form of key-value pairs (key-value). HashMap requires a hash function that uses the hashcode () and Equals () methods to add and retrieve elements to the collection/from the collection. When the put () method is invoked, HashMap calculates the hash value of the key and then stores the key value pairs on the appropriate index in the collection. If the key already exists, value is updated to the new value. Some of the key features of HashMap are its capacity (capacity), load factor (loading factor), and expansion limit (threshold resizing).

What's the difference between HashMap and Hashtable?

The problem is old fashioned. The following three aspects are:
1.hashMap removed the Hashtable contains method, but added Containsvalue () and ContainsKey () methods.
2.hashTable synchronization, and HashMap is not synchronized, the efficiency of forcing Hashtable to high.
3.HASHMAP allows null key values, while Hashtable does not allow

 

What is the difference between an array and a list (ArrayList). When you should use array instead of ArrayList.

What's the difference between ArrayList and LinkedList? Delete one of their elements, and what is the underlying principle respectively.

What's the difference between HashSet and TreeSet?

what is Object oriented.

the difference between Collection and collections.

the difference between the Hashmap,linkedmap,treemap

ArrayList the bottom layer is an array, then why is the array length fixed and the set length variable. How to achieve.

 

 

Reference: How to calculate time complexity: http://blog.csdn.net/firefly_2002/article/details/8008987

Collection structure:

What are the basic interfaces of the 1.Java collection class framework?

The collection class interface specifies a set of objects called elements. Each specific implementation class of a collection class interface can choose to save and sort elements in its own way. Some collection classes allow duplicate keys, some of which are not allowed.
The Java Collection class provides a well-designed set of interfaces and classes that support manipulating a set of objects. The most basic interfaces within the Java Collection class are:
Collection: Represents a set of objects, each of which is its child element.
Set: Collection that does not contain duplicate elements.
List: Sequential collection, and can contain duplicate elements.
Map: Keys can be mapped to values (value) of the object, the key can not be repeated.

2. Why the collection class does not implement the cloneable and serializable interfaces.

The semantics and meaning of cloning (cloning) or serialization (serialization) are related to specific implementations. Therefore, it is up to the concrete implementation of the collection class to decide how to be cloned or serialized.

3. What is an iterator (iterator).

The iterator interface provides many ways to iterate over the elements of a collection. Each collection class contains an instance of an iterator that can return instances of the

Iterative methods. Iterators can delete elements of the underlying collection in the course of an iteration, but you cannot call the collection's

Remove (objectobj) can be removed by using the Remove () method of the iterator.

What is the difference between 4.Iterator and listiterator?

Here is a list of their differences:
Iterator can be used to traverse the set and list collection, but Listiterator can only traverse the list.
Iterator to the set can only be forward traversal, listiterator can be both forward and backwards.
Listiterator implements the iterator interface and includes other functions, such as adding elements, replacing elements, getting the index of the previous and last elements, and so on.

5. What is the difference between rapid failure (fail-fast) and security failure (fail-safe).

Iterator 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 the classes underneath the Java.util.concurrent package are security failures. A fast-fail iterator throws a Concurrentmodificationexception exception, and a security-failed iterator never throws such an exception.

What is the working principle of the hashmap in 6.Java?

HashMap in Java store elements in the form of key-value pairs (key-value). HashMap requires a hash function that uses the hashcode () and Equals () methods to add and retrieve elements to the collection/from the collection. When the put () method is invoked, HashMap calculates the hash value of the key and then stores the key value pairs on the appropriate index in the collection. If the key already exists, value is updated to the new value. Some of the key features of HashMap are its capacity (capacity), load factor (loading factor), and expansion limit (threshold resizing).

the importance of the 7.hashCode () and Equals () methods is reflected in where.

HashMap in Java Use the hashcode () and Equals () methods to determine the index of key-value pairs, which are also used when the value is obtained from the key. If the two methods are not implemented correctly, two different keys may have the same hash value, and therefore may be considered equal by the collection. Also, these two methods are used to discover duplicate elements. So the realization of these two methods is very important to the accuracy and correctness of hashmap.

What is the difference between 8.HashMap and Hashtable?

HashMap and Hashtable both implement the map interface, so many features are very similar. However, they have the following different points:
HashMap allows keys and values to be null, while Hashtable does not allow keys or values to be null.
Hashtable is synchronized, and HashMap is not. Therefore, HashMap is more suitable for single-threaded environments, while Hashtable is suitable for multi-threaded environments.
HashMap provides a collection of keys that are available for application iterations, so HashMap is fast failing. On the other hand, Hashtable provides an enumeration of keys (enumeration).
Hashtable is generally considered to be a legacy class.

9. What is the difference between array and list (ArrayList). When you should use array instead of ArrayList.

The different points of array and ArrayList are listed below:
An array can contain basic types and object types, and ArrayList can only contain object types.
The array size is fixed, and the size of the ArrayList is dynamically variable.
ArrayList offers more methods and features, such as: AddAll (), RemoveAll (), iterator (), and so on.
For basic type data, the collection uses automatic boxing to reduce the coding effort. However, this approach is relatively slow when dealing with a fixed sized base data type.

What is the difference between 10.ArrayList and LinkedList?

Both ArrayList and LinkedList have implemented the list interface, and they have the following different points:
ArrayList is an indexed data interface, with an array at the bottom. It can randomly access elements in O (1) time complexity. In this case, LinkedList stores its data in the form of a list of elements, each of which is linked to its previous and subsequent elements, in which cases the time complexity of finding an element is O (n).
Add and delete operations are faster than arraylist,linkedlist inserts, because when an element is added to any location in the collection, you do not need to recalculate the size or update the index as an array.
LinkedList is more memory than ArrayList because LinkedList stores two references for each node, one pointing to the previous element and one to the next.
You can also refer to ArrayList vs. LinkedList.

11.Comparable and comparator interface is what to do. List the differences between them.

Java provides a comparable interface that contains only one CompareTo () method. This method can be sorted for two objects. Specifically, it returns a negative number, 0, and a positive number to indicate that the input object is less than, equal to, greater than the existing object.
Java provides comparator interfaces that contain compare () and Equals () two methods. The Compare () method is used to sort two input parameters, return a negative number, 0, and a positive number indicates that the first argument is less than, equal to, and greater than the second argument. The Equals () method requires an object as a parameter that determines whether the input parameter is equal to the 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 sort result.

12. What is a Java Priority queue (Priority queue).

Priorityqueue is an unbounded queue based on a priority heap whose elements are sorted in natural order (natural orders). At the time of creation, we can give it a comparer that is responsible for sorting the elements. Priorityqueue do not allow null values because they do not have a natural order, or they do not have any associated comparators. Finally, Priorityqueue is not thread-safe, and the time complexity of team and Out is O (log (n)).

13. Do you know the big O sign (big-o notation)? Can you give examples of different data structures?

The Big O notation describes the size of the algorithm, or how good it is in the worst-case scenario, when the elements inside the data structure increase.
The Big O symbol can also be used to describe other behaviors, such as memory consumption. Because the collection class is actually a data structure, we typically use the Big O notation to choose the best implementation based on time, memory, and performance. Large o notation can give a good description of the performance of a large number of data.

14. How to weigh the use of unordered arrays or ordered arrays.

The best thing about 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 value of the element needs to be moved back to place the new element. Instead, the insertion time complexity of unordered arrays is constant O (1).

What are the best practices for the 15.Java collection class framework?

Choosing the type of collection you want to use correctly depends on the performance of your application, such as: if the size of the element is fixed and you know beforehand, we should use an array instead of a ArrayList.
Some collection classes allow the initial capacity to be specified. Therefore, if we can estimate the number of stored elements, we can set the initial capacity to avoid recalculation of hash values or expansion.
For type safety, readability and robustness

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.