Collection of Java Interview 10 questions

What 1.Java collection framework? Some say the advantages Collections Framework?

    Each programming language has a set of the original Java version contains several collections: Vector, Stack, HashTable and Array. With the widespread use of the collection, Java1.2 proposed to include all collection interface, and collections framework algorithm. And concurrent use of generic collection classes in ensuring the thread-safe, Java has experienced for a long time. It also includes Java and contracting, the blocking interface as well as their implementation. Some of the benefits Collections Framework are as follows:

(1) the use of core collections to reduce development costs, rather than to achieve our own collection classes.

(2) With the use of a set of framework classes through rigorous testing, code quality will be improved.

(3) by using the supplied collections JDK, code maintenance costs can be reduced.

(4) reusability and maneuverability.

2. Collections Framework Generics What are the advantages?

    Java1.5 introduces a generic, all the collection interfaces and implementations use a lot of it. Generics allow us to offer a type of object can hold as a collection, so if you add any other type of element, it will complain at compile time. This avoids the appearance ClassCastException at run time, because you will get an error message at compile time. Generic clean also makes the code, we do not use the explicit conversion and instanceOf operator. It also brings benefits to running, because no type checking of bytecode instructions.

Collections Framework interfaces 3.Java basis of what?

    Collection is a collection of root-level interfaces. It represents a group of a set of objects that is, its elements. Java platform does not provide any direct implementations of this interface.

    Set is a collection that can not contain duplicate elements. This interface is a collection of abstract mathematical modeling is used to represent a collection, just like a deck of cards.

    List is an ordered collection may contain duplicate elements. You can access any element by its index. List like dynamic array transformation length.

    Map is a key maps to the value of the object Map can not contain duplicate key:. Each key can map up to a value.

    Some other interfaces Queue, Dequeue, SortedSet, SortedMap and ListIterator.

4. Why does not Collection interface inherits from Cloneable and Serializable?

    Collection Interface specifies a set of objects, the object that is its elements. How to maintain these elements from the Collection of the specific implementation decisions. For example, some implementations allow such Collection List of duplicate elements, while others such as Set is not allowed. Collection has a lot to achieve a public clone method. However, to implement it on all of the collection is of no significance. This is because the Collection is an abstract expressionist. It is important to realize.

    When dealing with a particular realization, the sequence of a cloning or semantic meaning and only play a role. Therefore, specific implementations should it decide how to clone or sequence of, or whether it can be cloned or serialized.

    Cloning and sequencing of the Authorization in all implementations, resulting in less flexibility and more restricted. Specific implementation should decide whether it can be cloned and serialization.

5. Why do not inherit the Map interface Collection interface?

    Despite the Map interface and its implementations are part of a collection of frames, but Map is not a collection, the collection is not Map. Therefore, Map Collection meaningless succession, and vice versa.

    If you inherit Map Collection interface, then the element where to go? Map comprising key-value pairs, or a method of extracting a key that provides a list of the set value, but it is not suitable for "a set of objects" specification.

What 6.Iterator that?

    Iterator interface provides an interface to traverse any Collection. We can get an iterator iterator instance method from a Collection. Iterator replaced the Java Collections Framework Enumeration. Iterators allow the caller to remove elements in an iterative process.

And the difference between 7.Enumeration Iterator interface?

    Enumeration twice as fast as Iterator, but also use less memory. Enumeration is very basic, but also to meet basic needs. However, compared with Enumeration, Iterator more secure, because when a collection is being traversed, it will prevent other threads to modify the collection.

Iterator replaced the Java Collections Framework Enumeration. Iterators allow the caller to remove elements from the collection, and Enumeration can not do. To make it function more clearly, the iterator method name has undergone improvements.

8. Why does not like Iterator.add () this method, add elements to the collection?

    Ambiguities, it is known that, Iterator agreement does not ensure order iteration. Note, however, ListIterator not provide an add operation, it is to ensure that the order of iteration.

9. Why iterator is not a way to get directly to the next element, without moving the cursor?

    It can be at the top of the current implementation of Iterator, but with very little, if it is added to the interface, each inherited have to go to achieve it, it does not make sense.

What is the difference between 10.Iterater and ListIterator?

(1) We can use the Iterator to traverse Set and List collections, ListIterator can only be traversed List.

(2) Iterator can only be traversed forward, LIstIterator traverse in both directions.

(3) ListIterator interface inherits from the Iterator, then add some additional features, such as adding an element, a replacement element, acquires the index position of the front or rear elements.


Original link: https: //blog.csdn.net/u010775025/article/details/79315361

More java learning materials may be concerned about: itheimaGZ get

Published 731 original articles · won praise 3 · Views 110,000 +

Guess you like

Origin blog.csdn.net/u010395024/article/details/104790457