Detailed explanation of the method of removing duplicate data from arrays in Java

This paper describes the method of removing duplicate data from arrays in Java. Share with you for your reference, as follows:

Some time ago, I was asked in an interview: if there are duplicate elements in an array, what method can I use to remove them? I thought of using one method at a time, but later, after consulting the data, I found that there are many methods that can be realized. Now I'll summarize the simpler ones.

I Implemented with list collection

The output is:

You can see that duplicate elements can be removed, but the sorting function is not implemented.

II Implemented with HashSet or TreeSet

Output results:

You can see that not only the duplicate data is removed, but also the data is sorted.

Where arrays Aslist () converts an array into a list object. This method will return an ArrayList object, which is not Java util. ArrayList class, but the static inner class of arrays class!

TreeSet can not only make the elements not repeated, but also realize the collection of sorting and other functions. When the object elements are added to the collection, it will automatically insert them into the ordered object sequence according to some comparison rules.

III Implemented with list and set

Output results:

It can be seen that the duplicate data is also removed and sorting is realized.

Let's make a comparison between HashSet and TreeSet:

@H_ 403_ 71@HashSet

HashSet has the following features

1) The arrangement order of elements cannot be guaranteed, and the order may change. 2) it is not synchronous. 3) the collection element can be null, but only one null can be put in

When an element is stored in the HashSet set, the HashSet will call the hashcode () method of the object to obtain the hashcode value of the object, and then determine the storage location of the object in the HashSet according to the hashcode value.

To put it simply, the criteria for judging the equality of two elements in the HashSet set is that the two objects are equal through the equals method, and the return values of the hashcode () method of the two objects are equal

Note that if you want to put an object into a HashSet and override the equals method of the corresponding class of the object, you should also override its hashcode () method. The rule is that if two objects return true through the equals method comparison, their hashcodes should also be the same. In addition, the attributes used as equals comparison criteria in the object should be used to calculate the value of hashcode.

@H_ 403_ 71@TreeSet class

TreeSet is the only implementation class of sortedset interface. TreeSet can ensure that collection elements are in sorting state. TreeSet supports two sorting methods, natural sorting and custom sorting. Natural sorting is the default sorting method. Objects of the same class should be added to TreeSet.

The way TreeSet judges that two objects are not equal is that the two objects return false through the equals method, or the comparison does not return 0 through the CompareTo method

Natural sorting

Natural sorting uses the CompareTo (object obj) method of the elements to be sorted to compare the size relationship between the elements, and then sorts the elements in ascending order.

Java provides a comparable interface, in which a CompareTo (object obj) method is defined. This method returns an integer value, and the size of the object that implements the interface can be compared.

obj1. If the CompareTo (obj2) method returns 0, it indicates that the two objects being compared are equal. If a positive number is returned, it indicates that obj1 is greater than obj2. If a negative number is returned, it indicates that obj1 is less than obj2.

If we always return true in the equals method of two objects, the CompareTo method of these two objects should return 0

Custom sorting

Natural sorting is arranged in ascending order according to the size of set elements. If you want to customize sorting, you should use the comparator interface to implement the int compare (t O1, t O2) method.

Most important:

1. TreeSet is implemented by a binary difference tree. The data in TreeSet is automatically ordered and null values are not allowed.

2. HashSet is implemented by hash table. The data in HashSet is unordered and can be put into null, but only one null can be put in. The values in both cannot be repeated, just like the unique constraint in the database.

3. HashSet requires that the objects put in must implement the hashcode () method. The objects put in are identified by the hashcode code, while string objects with the same content have the same hashcode, so the contents put in cannot be repeated. However, objects of the same class can be placed in different instances.

PS: there are two simple and practical online text de duplication tools on this site, which are recommended for everyone:

Online de duplication tool: http://tools.jb51.net/code/quchong

Online text de duplication tool: http://tools.jb51.net/aideddesign/txt_quchong

Readers interested in more Java related content can view the topics on this site: summary of Java array operation skills, summary of Java character and string operation skills, summary of Java mathematical operation skills, tutorial on Java data structure and algorithm, and summary of Java DOM node operation skills

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
分享
二维码
< <上一篇
下一篇>>