Search phone contacts by pinyin, first letter, custom keyboard search phone Address Book

Source: Internet
Author: User

Package contacters;


Import java.util.ArrayList;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map;
Import Java.util.Map.Entry;
Import Java.util.Random;


public class Test {
Static map<string, hashmap<string, contact>> maps = new hashmap<string, hashmap<string, Contact> > ();
static {
Hashmap<string, contact> cons = new hashmap<string, contact> ();
for (int i = 0; i <; i++) {
String phone = getnumber (11);
if (!cons.containskey (phone))
Cons.put (phone, new Contact (GetChar (6), GetChar (ten), phone,
GetChar (4)));
}
Maps.put ("0", cons);
Initialize, add all contacts to lists
}


/** Test **/

public static void Main (string[] args) {
Search ("3");
Search ("35");
Search ("356");
Search ("3567");
Search ("35678");
Search ("356781");
Search ("3567814");
Search ("35678147");
Search ("356781477");
Search ("3567814779");
Search ("35678147795");

for (int i = 0; I <=; i++) {
System.out.println (Maps.get (i + ""). Size ());
}
Hashmap<string, contact> a = Maps.get ("3");
For (entry<string, contact> entry:a.entryset ()) {
Contact con = Entry.getvalue ();
SYSTEM.OUT.PRINTLN ("------Name:" + con.getname () + "---index:"
+ con.getindex () + "-----pingyin:" + con.getpingyin ());


list<string> contacts = getcombination ("356");
for (String inputcon:contacts) {
if (Con.getname (). Contains (Inputcon)
|| Con.getpingyin (). Contains (Inputcon)
|| Con.getindex (). Contains (Inputcon)) {
System.out.println (Inputcon + ":"
+ Con.getname (). Contains (Inputcon) + "--"
+ Con.getpingyin (). Contains (Inputcon) + "--"
+ Con.getindex (). Contains (Inputcon));
}
}
}
// list<string> contacts = getcombination ("3567");


}


/**
* Search
* @param number
*/
public static void Search (String number) {
hashmap<string, contact> result = new hashmap<string, contact> ();
if (Number.contains ("0") | | Number.contains ("1")) {
Maps.put (Number.length () + "", result);
Return
}
list<string> contacts = getcombination (number);
hashmap<string, contact> currentcantacts = maps
. Get ((Number.length ()-1) + "");


for (String inputcon:contacts) {
For (entry<string, contact> entry:currentCantacts.entrySet ()) {
Contact con = Entry.getvalue ();
if (Con.getname (). Contains (Inputcon)
|| Con.getpingyin (). Contains (Inputcon)
|| Con.getindex (). Contains (Inputcon)) {
Result.put (Con.getphone (), con);
}
}
}
Maps.put (Number.length () + "", result);
}


/**
* Get Combo
*
* @param number
* @return
*/
public static list<string> Getcombination (String number) {
list<string> oldlist = new arraylist<string> ();
list<string> NewList = new arraylist<string> ();
String strs[] = getnumbers (Number.charat (0) + "");
for (String str:strs) {
Oldlist.add (str);
}
if (number.length () = = 1) {
return oldlist;
}
for (int i = 1; i < number.length (); i++) {
STRs = Getnumbers (Number.charat (i) + "");
if (STRs! = null) {
Newlist.clear ();
for (String old:oldlist) {
for (String str:strs) {
Newlist.add (old + str);
}
}
Oldlist.clear ();
Oldlist.addall (NewList);
}
}
return newlist;
}


public static string[] Getnumbers (String str) {
Switch (str) {
Case "2":
return new string[] {"A", "B", "C"};
Case "3":
return new string[] {"D", "E", "F"};
Case "4":
return new string[] {"G", "H", "I"};
Case "5":
return new string[] {"J", "K", "L"};
Case "6":
return new string[] {"M", "N", "O"};
Case "7":
return new string[] {"P", "Q", "R", "s"};
Case "8":
return new string[] {"T", "U", "V"};
Case "9":
return new string[] {"W", "X", "Y", "Z"};
Default
return null;
}


}


public static String getnumber (int num) {
Random random = new random ();
String temp = "";
for (int i = 0; i < num; i++) {
Temp = temp + random.nextint (9);
}
return temp;
}


public static String getChar (int num) {
String MD = "ABCDEFGHIJKMNPQRSTUVWXYZABC";
Random random = new random ();
String temp = "";
for (int i = 0; i < num; i++) {
Temp = temp + Md.charat (Random.nextint (24));
}
return temp;
}


}


Package contacters;


Public class Contact {

Private String name;

Private String Pingyin;

Private String phone;

Private String index;

Public Contact (String name,string pingyin,string phone,string index) {

this. name = name;

this. Pingyin = Pingyin;

this. phone = phone;

this. index = index;

}

Public String GetName () {

Returnname;

}

Public void setName (String name) {

this. name = name;

}

Public String Getpingyin () {

returnpingyin;

}

Public void Setpingyin (String pingyin) {

this. Pingyin = Pingyin;

}

Public String Getphone () {

returnphone;

}

Public void setphone (String phone) {

this. phone = phone;

}

Public String GetIndex () {

Returnindex;

}

Public void setindex (String index) {

this. index = index;

}

}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Search phone contacts by pinyin, first letter, custom keyboard search phone Address Book

Related Article

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.