To collide a string using hashtable

Source: Internet
Author: User

1. In some string arrays, there are often duplicate records, such as cell phone numbers, which we can filter through Hashtable public string[] Checkarray (string[] str) ... {
Hashtable<string, string> hash=new hashtable<string, string> ();

for (int i=0;i<str.length;i++) ... {
if (!hash.containskey (Str[i]))
Hash.put (Str[i], str[i]);
}

Enumeration Enumeration=hash.keys ();
String[] Str_new=new string[hash.size ()];
int i=0;

while (Enumeration.hasmoreelements ()) ... {
Str_new[i]=enumeration.nextelement (). toString ();
i++;
}
return str_new;
}
Example:
String[] mobile={"13811071500", "13811071500", "13811071501", "13811071503", "13811071501"};
Mobile=checkarray (mobile);
for (int i=0;i<mobile.length;i++)
System.out.println (Mobile[i]);
The output results are:
13811071503
13811071501
13811071500
2.a,b are all array of strings to find strings that exist in A and that do not exist in B
Public string[] Comparearray (string[] a,string[] B) {
Hashtable<string, string> hash=new hashtable<string, string> ();
Hashtable<string, string> hash_new=new hashtable<string, string> ();

for (int i=0;i<b.length;i++)
Hash.put (B[i], b[i]);

for (int i=0;i<a.length;i++) {
if (!hash.containskey (A[i]))
Hash_new.put (A[i], a[i]);
}

String[] C=new string[hash_new.size ()];
int i=0;
Enumeration Enumeration=hash_new.keys ();

while (Enumeration.hasmoreelements ()) {
C[i]=enumeration.nextelement (). toString ();
i++;
}
return C;
}
Example:
String[] mobile1={"13811071500", "13811071501", "13811071502", "13811071503", "13811071504"};
String[] mobile2={"13811071500", "13811071505", "13811071502", "13811071506", "13811071504"};
String[] Mobile3=comparearray (mobile1,mobile2);
for (int i=0;i<mobile3.length;i++)
System.out.println (Mobile[i]);
Output results:
13811071503
13811071501
The problems that exist:
Each time is in reverse order, you can change the program slightly, into a positive sequence.

3. Filter out a particular string in an array of strings

/** *//** examines an array of strings and, if it contains a particular string, deletes the string from the array
In addition, returns the remaining array of strings
* @param str_array string array
* @param str_remove The string to be deleted
* @return Filtered string
*/
Public string[] Removestrfromarray (string[] str_array,string
Str_remove) ... {
Hashtable<string, string> hash=new hashtable<string, string> ();
for (int i=0;i<str_array.length;i++) ... {
if (!str_array[i].equals (Str_remove))
Hash.put (Str_array[i], str_array[i]);
}
To generate a new array
String[] Str_new=new string[hash.size ()];
int i=0;
Enumeration Enumeration=hash.keys ();
while (Enumeration.hasmoreelements ()) ... {
Str_new[i]=enumeration.nextelement (). toString ();
i++;
}
return str_new;
}

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.