Java Array de-weight
First, with the list set to achieve
1 Package com.test;2 3 import java.util.ArrayList;4 import java.util.List;5 6 /*7 * Array de-weight8 */9 Public classDistinct {Ten Public Static voidMain (string[] args) { One int[] Str ={1,5,6,9,5,6,3,5,7}; AList<integer>list =NewArraylist<integer>(); - for(inti =0; i < str.length; i++) { - if(!list.contains (Str[i])) { the List.add (Str[i]); - } - } - +System. out. println (List.tostring ()); - } +}
The code above can be implemented to remove duplicate elements, but no sorting function is implemented
Second, with HashSet or treeset realization
1Integer[] Nums = {5,5,6,6,6,8,8,7, One, A, A };2 //HashSet hset = new HashSet (arrays.aslist (nums));3Treeset<integer> Hset =NewTreeset<integer>(Arrays.aslist (nums));4Iterator i =hset.iterator ();5 while(I.hasnext ()) {6System. out. println (I.next ());7}
Output results: 5 6 7 8 11 12
You can see that not only the data is duplicated, but the data is sorted
where Arrays.aslist () is converting an array to a list object, this method returns an object of type ArrayList, which is not a Java.util.ArrayList class, but a static inner class of arrays class.
TreeSet not only allows elements to be duplicated, but also enables the collection of functions such as sorting, which is automatically inserted into an ordered sequence of objects by a comparison rule when an object element is added to the collection.
Comparison between HashSet and TreeSet
HashSet
Characteristics:
1. The order of elements cannot be guaranteed, and the order may vary
2. Not synchronous
3. The collection element may be null, but only one null can be placed
When an element is deposited into the HashSet collection, HashSet calls the object's Hashcode () method to get the hashcode () value of the object, and then determines the object's storage location in hashcode based on the HashSet value.
Simply put, the hashset set determines that the standard of two elements is equal by the Equals method, and that the Hashcode () method of two objects returns an equal value
Note: If you want to put an object into HashSet, override the Equals method of the corresponding class for that object, and you should override its Hashcode () method. The rule is that if two objects return true through the Equals method, their hashcode should also be the same. In addition, the attribute used in the object as the equals comparison criterion should be used to calculate the value of the hashcode.
TreeSet class
TreeSet is the only implementation class for the SortedSet interface, and TreeSet ensures that the collection elements are in the sorted state. TreeSet supports two sorting methods, natural sorting and custom sorting, in which the natural sort is the default sorting method. An object of a class should be added to the TreeSet.
TreeSet determines that two objects are unequal by two objects returning false through the Equals method, or by comparing the CompareTo method
Natural sort
Natural sorting uses the CompareTo (Object obj) method to sort elements to compare the size relationship between elements and then arranges the elements in ascending order
Java provides a comparable interface that defines a CompareTo (object obj) method that returns an integer value that implements the object of the interface to compare the size.
Natural sorting is based on the size of the collection elements, in ascending order, if you want to customize the sorting, you should use the comparator interface to implement the compare (T o1,t O2) method
Most Important:
1.TreeSet is a two-tree implementation, the data in the TreeSet automatically sequenced, not allowed to put a null value
2.hashSet is a hash table implementation, the data in HashSet is unordered, can be put into null, but only a null, both values can not be duplicated, such as a unique constraint in the database
3.hashSet required to put the object must implement the Hashcode method, the object is placed in the Hashcode code as the identity, and the same content of the string object, Hashcode is the same, so the contents can not be duplicated. But the same class can be put into different instances.
Array de-weight