The list object list in Java implements the method of de duplication or retrieval and sorting

preface

Because I have encountered several list de duplication and sorting during the interview, I feel it necessary to summarize the specific methods and share them for your learning and reference. I don't have much to say. Let's take a look at one of the methods introduced below:

1、 List de duplication

1.1 entity class student

List < student > the capacity is more than 10k, and it is required to repeat. The repetition criteria of students here are the same attributes, so the equals and hashcode methods need to be rewritten. I don't know how many can be written by hand.

Student's equals method:

Here, just remember that the purpose is to compare the attributes of students. If the attributes are the same, they are equal. First consider address equality, and then type matching instanceof. The following are various attributes. The int attribute is directly compared with the double equal sign. The string type needs to judge whether it is null. If it is null, it is null and returns true. If it is not null, compare equals.

Student's hashcode method:

Hashcode is used to assist in hash table calculation and facilitate fast search. Therefore, the result of hash algorithm should be hashed as much as possible. 31 is used here. The reason for seeing this 31 in other blogs is as follows: Obj * 31 = = obj < < 5-obj Moving 5 bits to the left is equivalent to multiplying 2 to the 5th power, which is 32 Null hashcode is null.

Through the implementation of equals and hashcode, it can be found that if equals is true, all attributes are the same, and if the attributes are the same, the calculated hashcode must be the same. However, if the hashcodes are the same, the attributes may not be the same, that is, equals may not be true.

The value of hashcode does not lie here, but in the implementation of HashMap. HashMap is implemented internally through the hash structure of linked list array. Hashcode is used here.

Here is the complete student code:

1.2 weight removal through HashSet

If you think you can hold a perfect hash algorithm, you can implement it yourself. Here, the HashSet provided by JDK is used to complete repeated acquisition.

First release code:

The principle and simplicity of de duplication, whether you just want to throw away the repetition or take it out. What is removed here is the object encountered for the second time, and what is taken out is also the object encountered for the second time. The add method in the HashSet will return a Boolean value. If the inserted value already exists, it will directly return false. The source code of HashSet will be studied later. Generally speaking, it is implemented through the key of HashMap, and HashMap has changed greatly in 1.8. It is said that it is implemented with red black tree, which improves the time complexity of get.

2、 List object sorting

Similarly, the student object is stored in the list, and I need a rule to sort. The sorting rule is defined here as the comparison size of ID. Reference: list sorting in Java

2.1 Student object implements comparable interface

The comparable interface provides a CompareTo (object o) method for comparison. The size is compared by returning values > 0, = 0, < 0. Here, because ID is only used as a method to compare sizes, ID is directly used for subtraction. If you want to compare objects, it is recommended to apply this property. compareTo(o.property) .

Via collections Sort (list) sort:

2.2 overload the sort method and pass in a comparator

The student class is still before the comparable interface is implemented:

Add sorting rule in the sorting Code:

summary

The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>