I. Using the list collection to implement
int[] str = {5,6,6,6,8,8,7,4}; List<Integer> list =NewArraylist<integer>(); for(intI=0; i<str.length; i++) { if(!list.contains (Str[i])) {List.add (str[i]); }} System. out. println ("Remove Duplicate List collection"+list);
The output is:
Remove Duplicate list collection [5, 6, 8, 7, 4]
You can see that duplicate elements can be removed, but no sorting is implemented.
Two. Implement with HashSet or TreeSet
Integer[] Nums = {5,5,6,6,6,8,8,7, One, A, A }; //HashSet hset = new HashSet (arrays.aslist (nums));Treeset<integer> Hset =NewTreeset<integer>(Arrays.aslist (nums)); Iterator I=Hset.iterator (); while(I.hasnext ()) {System. out. println (I.next ()); }
Output results: 5 6 7 8 11 12
You can see that not only the data is duplicated, but the data is sorted as well.
where Arrays.aslist () converts an array into 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 can not only make elements non-repeatable, but also implement a 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.
Three. Using list and set to implement
int 5 6 6 6 8 8 7 }; ListNew arraylist<integer>(); for (int i:nums) numlist.add (i); SetNew hashset<integer>(); Numset.addall (numlist); System. out. println (Numset);
Output results: [5, 6, 7, 8]
As you can see, it also removes duplicate data and implements sorting.
Let's do a comparison of HashSet and TreeSet:
HashSet
HashSet has the following characteristics
1) cannot guarantee the order of the elements, the order may change
2) Not in sync
3) The collection element can be null, but only one null
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 location of the object in hashcode based on the HashSet value.
Simply put, the hashset set determines that two elements are equal by two objects that compare equality by equals method, and two objects with the Hashcode () method return values equal
Note that if you want to put an object in HashSet and override the Equals method of the corresponding class for that object, you should also 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. The object that is added to the treeset should be the same class.
TreeSet determines that two objects are unequal by two objects returning false through the Equals method, or by comparing the CompareTo method without returning 0
Natural sort
Natural sorting uses the CompareTo (Object obj) method to sort the elements to compare the size relationships 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.
The Obj1.compareto (obj2) method returns 0, indicating that the two objects being compared are equal, and if a positive number is returned, the OBJ1 is greater than obj2, and if it is negative, obj1 is less than obj2.
If we always return true for the Equals method of two objects, then the CompareTo method returned by the two objects should return a 0
Custom sorting
Natural sorting is based on the size of the collection elements, in ascending order, if you want to customize the sort, you should use the comparator interface to implement the int compare (T o1,t O2) method.
Most Important:
1, TreeSet is two difference tree implementation, TreeSet in the data is automatically sequenced, not allowed to put null values.
2, HashSet is a hash table implementation, the data in HashSet is unordered, can be put into null, but only a null, the values in both cannot be duplicated, such as the unique constraints in the database.
3, HashSet required to put the object must implement the Hashcode () method, put the object, is the Hashcode code as the identity, and the same content of the string object, Hashcode is the same, so the content can not be duplicated. However, objects of the same class can be placed in different instances.
Ext.: http://blog.csdn.net/u010680097/article/details/52353369
Java Arrays Remove Duplicate data