Question: For example, I have an array (the number of elements is 0), and I hope that the elements added will not be repeated.
When I get such a question, I may quickly write down the code. The array here uses ArrayList.
private static void testListSet(){ List<String> arrays = new ArrayList<String>(){ @Override public boolean add(String e) { for(String str:this){ if(str.equals(e)){ System.out.println("add failed !!! duplicate element"); return false; }else{ System.out.println("add successed !!!"); } } return super.add(e); } }; arrays.add("a");arrays.add("b");arrays.add("c");arrays.add("b"); for(String e:arrays) System.out.print(e); }
I don’t care about anything here, I only care about what to do when adding elements to the array. Determine (of course, only use the add method to add array elements) whether the same element already exists. If the element does not exist in the array, add it to the array, and vice versa. It may be simple to write like this, but it seems clumsy when faced with a huge array: if there is one element in an array with 100,000 elements, do we need to call equal 100,000 times? Here is the basics.
Question: After adding an array that already has some elements, how to delete the duplicate elements in the array?
We all know that collections in Java can be generally divided into two categories: List and Set. The elements in the collection of List class are required to be ordered but can be repeated, while the elements in the collection of Set class are required to be unordered but cannot be repeated. Then here you can consider whether using the Set feature to delete duplicate elements will achieve the goal. After all, using the existing algorithm in the system is better than the algorithm you write now.
public static void removeDuplicate(List<People> list){ HashSet<People> set = new HashSet<People>(list); list.clear(); list.addAll(set); } ivate static People[] ObjData = new People[]{ new People(0, "a"),new People(1, "b"),new People(0, "a"),new People(2, "a"),new People(3, "c"), };
public class People{ private int id; private String name; public People(int id,String name){ this.id = id; this.name = name; } @Override public String toString() { return ("id = "+id+" , name "+name); } }
The above code uses a custom People class. When I add the same object (referring to the same data content), I call the removeDuplicate method and find that this does not solve the actual problem. Still The same object exists. So how does one determine whether objects are the same in HashSet? Open the HashSet source code and you will find that every time you add data to it, you must call the add method:
@Override public boolean add(E object) { return backingMap.put(object, this) == null; }
The backingMap here is the data maintained by HashSet. It uses a very clever method to add each The Object added this time is regarded as the KEY in the HashMap, and the HashSet object itself is regarded as the VALUE. This takes advantage of the KEY property in Hashmap, and naturally the data of HashSet will not be repeated. But whether there is real duplicate data depends on how to determine whether two KEYs are the same in HashMap.
@Override public V put(K key, V value) { 390 if (key == null) { 391 return putValueForNullKey(value); 392 } 393 394 int hash = secondaryHash(key.hashCode()); 395 HashMapEntry<K, V>[] tab = table; 396 int index = hash & (tab.length - 1); 397 for (HashMapEntry<K, V> e = tab[index]; e != null; e = e.next) { 398 if (e.hash == hash && key.equals(e.key)) { 399 preModify(e); 400 V oldValue = e.value; 401 e.value = value; 402 return oldValue; 403 } 404 } 405 406 // No entry for (non-null) key is present; create one 407 modCount++; 408 if (size++ > threshold) { 409 tab = doubleCapacity(); 410 index = hash & (tab.length - 1); 411 } 412 addNewEntry(key, value, hash, index); 413 return null; 414 }
In general, the idea here is to traverse the elements in the hashmap. If the hashcodes of the elements are equal (in fact, the hashcodes need to be processed once), then judge the eqaul method of KEY. If these two conditions are met, then they are different elements. If the element type in the array is customized, and you want to use the Set mechanism, you have to implement equal and hashmap yourself (the hashmap algorithm will not be introduced in detail here, I can only understand it) method:
public class People{ private int id; // private String name; public People(int id,String name){ this.id = id; this.name = name; } @Override public String toString() { return ("id = "+id+" , name "+name); } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public boolean equals(Object obj) { if(!(obj instanceof People)) return false; People o = (People)obj; if(id == o.getId()&&name.equals(o.getName())) return true; else return false; } @Override public int hashCode() { // TODO Auto-generated method stub return id; //return super.hashCode(); } }
The above is the detailed content of How to remove duplicate elements from array in Java. For more information, please follow other related articles on the PHP Chinese website!