This article mainly introduces the method of removing duplicate data from arrays in Java. It analyzes in detail several common methods, implementation principles and related precautions for removing duplicate data from Java arrays in the form of examples. Friends who need it can refer to it
The example in this article describes the method of removing duplicate data from arrays in Java. I would like to share it with you for your reference. The details are as follows:
I was asked in an interview some time ago: If there are duplicate elements in an array, what method can be used to remove the duplicates? For a while, I thought of using one method, but later after checking the information, I found that there are many methods to achieve it. Now I will summarize some of the simpler ones.
1. Use List collection to implement
int[] str = {5, 6, 6, 6, 8, 8, 7,4}; List<Integer> list = new ArrayList<Integer>(); for (int i=0; i<str.length; i++) { if(!list.contains(str[i])) { list.add(str[i]); } } System.out.println("去除重复后的list集合"+list);
The output result is:
去除重复后的list集合[5, 6, 8, 7, 4]
You can see that duplicate elements can be removed, but the sorting function is not implemented.
2. Use hashSet or TreeSet to implement
Integer[] nums = { 5, 5, 6, 6, 6, 8, 8, 7, 11, 12, 12 }; // HashSet hset = new HashSet(Arrays.asList(nums)); TreeSet<Integer> hset = new TreeSet<Integer>(Arrays.asList(nums)); Iterator i = hset.iterator(); while(i.hasNext()){ System.out.println(i.next()); }
Output result:
5 6 7 8 11 12
You can see that not only the duplicate data is removed, but also the data is sorted.
Arrays.asList()
converts an array into a List object. This method will return an ArrayList type object. This ArrayList class is not the java.util.ArrayList class, but It is a static inner class of Arrays class!
TreeSet can not only prevent elements from being repeated, but also can implement collections with functions such as sorting. When object elements are added to the collection, they will automatically be inserted into an ordered sequence of objects according to certain comparison rules.
3. Use List and set to implement
##
int[] nums = { 5, 6, 6, 6, 8, 8, 7 }; List<Integer> numList = new ArrayList<Integer>(); for (int i : nums) numList.add(i); Set<Integer> numSet = new HashSet<Integer>(); numSet.addAll(numList); System.out.println(numSet);
[5, 6, 7, 8]
Let’s compare HashSet and TreeSet:
HashSet
HashSet has the following characteristics1) The order of the elements cannot be guaranteed, and the order may change2) It is not synchronized
3) The set elements can be null, but they can only be placed Enter a null
TreeSet class
TreeSet is the only implementation class of the SortedSet interface. TreeSet can ensure that the collection elements are in a sorted state. TreeSet supports two sorting methods, natural sorting and customized sorting, of which natural sorting is the default sorting method. What is added to TreeSet should be objects of the same class. The way TreeSet determines that two objects are not equal is that the two objects return false through the equals method, or the comparison through the CompareTo method does not return 0Natural sorting
Natural sorting uses the CompareTo (Object obj) method of the elements to be sorted to compare the size relationship between elements, and then arrange the elements in ascending order. Java provides a Comparable interface, which defines a compareTo(Object obj) method, which returns an integer value. Objects that implement this interface can be compared in size. If the obj1.compareTo(obj2) method returns 0, it means that the two objects being compared are equal. If it returns a positive number, it means that obj1 is greater than obj2. If it is a negative number, it means that obj1 is less than obj2. If we always return true to the equals method of the two objects, the compareTo method of the two objects should return 0Customized sorting
Natural sorting is based on the size of the collection elements, arranged in ascending order. If you want to customize the sorting, you should use the Comparator interface and implement the int compare(T o1, T o2) method.The most important:
1. TreeSet is implemented by a two-difference tree. The data in the Treeset is automatically sorted, and null values are not allowed. 2. HashSet is implemented by a hash table. The data in HashSet is unordered. You can put nulls, but you can only put one null. The values in both cannot be repeated, just like in the database. the only constraint. 3. HashSet requires that the object put in must implement the HashCode() method. The object put in is identified by the hashcode code. String objects with the same content have the same hashcode, so the put in Content cannot be repeated. But objects of the same class can be put into different instances.The above is the detailed content of Detailed explanation of the implementation method of removing duplicate data from Java arrays. For more information, please follow other related articles on the PHP Chinese website!