Finishing the basics of the Java Collections Framework

Collection Interfaces

Collection Interface

Collection is the most basic set of interfaces, a Collection represents a group of Object, java does not provide directly inherited from the Collection classes provide only sub-interfaces inherited (such as List and Set).

Collection interface storing a set of non-unique, unordered objects.

 

List Interface

List interface is an ordered Collection, to precisely control the inserted location of each element can be accessed by indexing the List elements, the first element index is 0, and allows the same elements.

Storing a set of Interface List is not unique, ordered (inserted sequence) object.

 

Set Interface

Set Collection has exactly the same interface, but on different behaviors, Set duplicate elements are not saved.

Set interface to store a set of unique, unordered objects.

 

SortedSet

Inherited Set, save ordered set.

 

Map Interface

Map interface storing a set of key objects, there is provided the key to value mappings.

 

Map.Entry

Element describes a (key / value pairs) in a Map. Map is an internal class.

 

SortedMap

Inherited Map, so Key held in ascending order.

 

The difference between Set and List of

1. Set interface instance store things disorderly, not duplicate data. Things orderly, repeatable elements of the List interface instance storage.

2. Set the low retrieval efficiency, high efficiency insert delete, insert, and delete elements will not cause a change in position.

3. List arrays and the like, can grow dynamically, automatically increase the length of the actual data stored in accordance with List. Find elements of high efficiency, low insertion and deletion of elements of efficiency, because it will cause changes in the position of other elements.

 

Common collection implementation class

ArrayList

List class implements the class interface, an array of variable size, random access and traverse the elements that provide better performance.

This class is asynchronous, do not use in the case of multi-threaded. ArrayList increase the length of the current 50%, low insertion and deletion efficiency.

 

LinkedList

This class implements the List interface allows null elements. The main data structure used to create the list, the class is not synchronized method, if multiple threads access a List colleagues, you must implement their own access synchronization solution is to create a structure List is synchronized List. E.g:

Listlist=Collections.syncronizedList(new LinkedList(....));

LinkedList low query efficiency.

 

HashSet

Set class implements the interface does not allow duplicate elements, does not guarantee the order of elements in the set, allowing the element contains a null value, but a maximum of.

 

LinkedHashSet

It has predictable iteration order of the Set interface hash table and linked list implementation.

 

HashMap

It is a hash table, which stores the contents of key-value mapping.

Map interface implementation class class, according to the key value storage data HashCode has fast access speed, a maximum of one record key is null, thread synchronization is not supported.

 

LinkedHashMap

Inherited from HashMap, using the elements of the natural order of the elements in order.

 

 

 

1. new object inside a java what had been done?

From a static point of view, new object is to create an object instance of a class. From jvm operation angle of view, when the new jvm bytecode execution, first looks class has not been loaded into memory and initialization, if the class is the first time, the first load the class. Will heap memory allocation in the object instance memory space after loading is complete, the application virtual machine's memory stack allocated object instance.

2. Does the abstract class defines a constructor? If so, whether a new abstract class?

An abstract class may also define a constructor, but not a new abstract class.

3. Since it is not a new abstract class that defines a constructor that what is the point?

Abstract class constructor can only call the constructor chain, which is called from another class constructor initializes its effect may be some initial values ​​of the abstract class.

4. String is whether the basic data types? It StringBuilder, StringBuffer What is the difference?

String is not a primitive data type.

After the definition of String is immutable, although it can assign and stitching several times in the program, but in fact each assignment is to re-open up a memory space in memory.

StringBuilder and StringBuffer are variable, many times they will not open up a space assignment in memory, StringBuilder is not thread safe, StringBuffer is thread-safe.

What internal 5. StringBuilder and StringBuffer implementation principle?

Difference is that StringBuffer StringBuffer and SpringBuilder in the append method adds the synchronized keyword, it is thread safe.

They are inherited from AbstractBuilder, the internal implementation is a leg cuff array, the initial length of the array 16. When calling append string concatenation method, which is actually called internal System.arraycopy string copied into the array variable.

What 6. StringBuilder expansion mechanism?

StringBuilder within an array of strings, a default size is 16, when the capacity exceeds 16, the expansion will be performed, prior to the new size of the array is twice the size of the array +2, i.e. the first expansion of the size of 34. The previous expansion of the array will be copied to the new array.

7. String str = "a" and the String str = new String ( "b") What is the difference?

String str = "a", will first have to find out whether the constant pool "a" string, if there is to it directly, then why not create a constant pool and point to it.

String str = new String ( "b"), will be created in heap memory in a String object instance, and point to it, and it will also create a "b" objects in the constant pool.

8. == and equals the difference?

== referenced address, if the value is a matter of comparing the basic type of comparison.

equals, if the object of comparison does not override the equals method, it is more of a reference address things. If you compare the object overrides the equals method, the contents of an object to compare whether the same.

9. What override equals method need to pay attention?

When rewriting the equals method, must override hashCode method, hashCode method is to calculate the hash value of the object.

In java provides that:

scheduled equals true, then their hashCode necessarily be equal;

equals equals false, then they may be equal hashCode may not be equal, that is, hashCode equal, equals not necessarily equal.

The reason why the override hashCode method, mainly used in the set is determined.

If the class does not override hashCode method, a method to rewrite only when two objects equals equals equal to true, they are not equal hashCoefficient. At this time, if they are set as a key into the Map, because of their hash values ​​are not equal, so that they are not equal Map of Key, at this time there will be two identical key value in a Map logically, not in line with our expectations for the program. So override equals method must override hashCode methods.

10. What override the hashCode method need to pay attention?

In the design of the hash function, you should try to avoid conflict. If you frequently generate a hash collision, as a key target in the Map is stored in a different hash value will be key to a position, have an impact on the Map performance.

 

Original link: https://www.cnblogs.com/yulinfeng/p/11371189.html

Guess you like

Origin www.cnblogs.com/763977251-sg/p/11367501.html