A discussion on the method of removing repeated elements from an array in Java _java

Source: Internet
Author: User
Tags addall hash shallow copy stub

Question: For example, I have an array (with an element number of 0), and I want to add elements that cannot be duplicated.

To get such a question, I might quickly write down the code, where the array is ArrayList.

Copy Code code as follows:



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'm not going to do anything here, I'm only interested in making judgments when adding elements to an array (of course adding an array element with the Add method only), whether the same element already exists, adding to the array if it is not present in the array, and vice versa. This may be easy to write, but awkward when confronted with a large array: An array of 100000 elements. Do you want to call 100,000 times equal? Here is a foundation.

Question: Add an array of already some elements, how do you delete the duplicated elements in this array?

You know that there are two main types of collections in Java: List and set. The elements in the list class need to be ordered but repeatable, while elements in the set class require unordered but not repeatable. So here you can consider the use of Set this feature to delete the duplicate elements do not achieve the purpose, after all, using the system in the existing algorithms to better than their own current write algorithm.

Copy Code code as follows:

public static void Removeduplicate (List<people> List) {
hashset<people> set = new hashset<people> (list);
List.clear ();
List.addall (set);
private 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"),
};

Copy Code code as follows:

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, with a custom people class, when I add the same object (meaning that contains the same data content), call the Removeduplicate method to find that this does not solve the actual problem, there is still the same object. So how does hashset judge the same thing as an object? Open HashSet source can be found: every time you add data to the time, you must call the Add method:

Copy Code code as follows:

@Override
Public boolean Add (E object) {
Return Backingmap.put (object, this) = = NULL;
}

Here the Backingmap is the HashSet maintenance of the data, it uses a very clever method, each added object as HashMap inside the key, itself HashSet object as value. This makes use of the key uniqueness in the HashMap, the natural hashset data will not repeat. But whether there are duplicate data, it depends on how the HashMap to determine whether two key is the same.

Copy Code code as follows:



@Override Public V-Put (K key, V value) {


if (key = = null) {


return Putvaluefornullkey (value);


}

int hash = Secondaryhash (Key.hashcode ());


Hashmapentry&lt;k, v&gt;[] tab = table;


int index = hash &amp; (tab.length-1);


For (hashmapentry&lt;k, v&gt; e = Tab[index]; e!= null; e = e.next) {


if (E.hash = = Hash &amp;&amp; key.equals (E.key)) {


Premodify (e);


V oldValue = E.value;


E.value = value;


return oldValue;


}


}

No entry for (Non-null) the key is present; Create one
modcount++;
if (size++ > Threshold) {
tab = Doublecapacity ();
Index = hash & (tab.length-1);
}
Addnewentry (key, value, hash, index);
return null;
}

In general, the idea here is to traverse the elements in the HashMap, if the elements are hashcode equal (in fact, to do the hashcode), and then to judge the Eqaul method of the key. If these two conditions are met, then there are different elements. So here if the element type in the array is custom, to take advantage of the set mechanism, then you have to implement equal and HashMap (here the HASHMAP algorithm is not introduced in detail, I also understand a point) method:

Copy Code code as follows:



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 ();
   }
}

There will not be two identical people in the call Removeduplicate (list) method.

Well, let's test their performance here:

Copy Code code as follows:



public class Removedeplicate {





public static void Main (string[] args) {


TODO auto-generated Method Stub


Testlistset ();


Removeduplicatewithorder (arrays.aslist (data));


arraylist&lt;people&gt; list = new Arraylist&lt;people&gt; (arrays.aslist (objdata));





Removeduplicate (list);





people[] data = Createobjectarray (10000);


arraylist&lt;people&gt; list = new Arraylist&lt;people&gt; (arrays.aslist (data));





Long startTime1 = System.currenttimemillis ();


System.out.println ("Set start time--&gt;" +starttime1);


Removeduplicate (list);


Long endTime1 = System.currenttimemillis ();


System.out.println ("Set End time--&gt;" +endtime1);


System.out.println ("Set total time--&gt;" + (endtime1-starttime1));


System.out.println ("Count:" + people.count);


People.count = 0;





Long starttime = System.currenttimemillis ();


SYSTEM.OUT.PRINTLN ("Efficient Start time--&gt;" +starttime);


Efficientremovedup (data);


Long endtime = System.currenttimemillis ();


SYSTEM.OUT.PRINTLN ("Efficient End Time--&gt;" +endtime);


SYSTEM.OUT.PRINTLN ("Efficient total time--&gt;" + (endtime-starttime));


System.out.println ("Count:" + people.count);




}
public static void Removeduplicate (List<people> List)
{
hashset<people> set = new hashset<people> (list);
List.clear ();
List.addall (set);
}

public static void Removeduplicatewithorder (List&lt;string&gt; arllist)


{


set&lt;string&gt; set = new hashset&lt;string&gt; ();


list&lt;string&gt; NewList = new arraylist&lt;string&gt; ();


for (iterator&lt;string&gt; iter = Arllist.iterator (); Iter.hasnext ();) {


String element = Iter.next ();


if (Set.add (Element))


Newlist.add (Element);


}


Arllist.clear ();


Arllist.addall (NewList);


}








@SuppressWarnings ("Serial")


private static void Testlistset () {


list&lt;string&gt; arrays = new Arraylist&lt;string&gt; () {


@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);


}





private static void Efficientremovedup (people[] peoples) {


Object[] Originalarray; Again, pretend this contains our original data


int count = 0;


New temporary array to hold non-duplicate data


people[] NewArray = new People[peoples.length];


Current index in the new array (also the number of non-dup elements)


int currentindex = 0;

Loop through the original array ...


for (int i = 0; i &lt; peoples.length; ++i) {


Contains =&gt; true IFF NewArray contains originalarray[i]


Boolean contains = false;





Search through NewArray to the if it contains an element equal


To the element in Originalarray[i]


for (int j = 0; J &lt;= currentindex; ++j) {


If the same element is found, don ' t add it to the new array


count++;


if (Peoples[i].equals (Newarray[j])) {





contains = true;


Break


}


}

If we didn ' t find a duplicate, add the new element to the new array


if (!contains) {


Note:you may want to use a copy constructor, or a. Clone ()


Here if the situation warrants than a shallow copy


Newarray[currentindex] = peoples[i];


++currentindex;


}


}





System.out.println ("Efficient Medthod inner count:" + count);

}

private static people[] Createobjectarray (int length) {
int num = length;
people[] data = new People[num];
Random Random = new Random ();
for (int i = 0;i<num;i++) {
int id = random.nextint (10000);
System.out.print (id + "");
Data[i]=new people (ID, "I am a Man");
}
return data;
}

Test results:

Copy Code code as follows:

Set End time--> 1326443326724
Set Total Time--> 26
count:3653
Efficient start time--> 1326443326729
Efficient Medthod Inner count:28463252
Efficient End Time--> 1326443327107
Efficient Total time--> 378
count:28463252

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.