Lagou Java Employment Training Camp

How to implement LinkedList and ArrayList at the bottom and how to choose to use

The internal implementation of ArrayList is based on the internal array Object[], so conceptually, it is more like an array, but the internal implementation of LinkedList is based on a set of connected records, so it is more like a linked list structure, so they are in performance There is a big difference: From the above analysis, you can see that when inserting data before or in the middle of the ArrayList, you must move all the data after it back accordingly, which will inevitably take more time, so when you operate When adding data after a column of data rather than in the front or the middle, and when you need to access the elements in it randomly, using ArrayList will provide better performance; and when accessing an element in the linked list, you must start from one end of the linked list Start looking up element by element along the connection direction until you find the element you need. Therefore, when your operation is to add or delete data before or in the middle of a column of data, and access the elements in order, you should Use LinkedList.

How do members in TreeSet determine the comparison algorithm

A: Natural sorting: implement the Comparerable interface in the custom class and override the compareTo method

B: Comparator sorting: implement the Comparetor interface in a custom class and override the compare method

Hashtable & HashMap

The performance of Hashtable and HashMap is similar to Vector and ArrayList. For example, the method of Hashtable is synchronous, while the method of HashMap is not.

HashSet cannot add duplicate elements. When the add(Object) method is called,

First, the hashCode method of Object will be called to determine whether the hashCode already exists, if not, the element will be inserted directly;

If it already exists, call the equals method of the Object object to determine whether it returns true, if it is true, the element already exists, if it is false, insert the element.

What is Entry and how to understand

The interface Entry is the internal interface of Map<K,V>. The interface is used to represent a key-value pair. One such instance contains K-type keys and V-type values. Contains methods to get Key, get value, and set value. At the same time, you need to override the equals and hashCode methods.

The interface has four ways to obtain the comparator:

1: What is polymorphism and what is the premise?

Polymorphism is divided into compile-time polymorphism and runtime polymorphism. Among them, polymorphism at compile time is static, which mainly refers to the overloading of methods, which distinguishes different methods according to different parameter lists. After compilation, it will become two different methods, and there is no talk of polymorphism at runtime. While runtime polymorphism is dynamic, it is achieved through dynamic binding, which is commonly referred to as polymorphism.

Inheritance: In polymorphism, there must be subclasses and parent classes that have inheritance relationships.

Rewrite: The subclass redefines some methods in the parent class, and when calling these methods, it will call the method of the subclass.

Up-casting: In polymorphism, the reference of the subclass needs to be assigned to the parent class object. Only in this way can the reference be able to call the method of the parent class and the method of the subclass.

2: The characteristics of member access in polymorphism?

Member variables

Member method

Static method

Features of member access in polymorphism:

A: member variables

Look at the left for compilation, and look at the left for running.

B: Construction method

创建子类对象的时候,访问父类的构造方法,对父类的数据进行初始化。

Guess you like

Origin blog.csdn.net/weixin_52772147/article/details/111870333