zoukankan      html  css  js  c++  java
  • 按拼音、首字母搜索手机通讯录,自己定义键盘搜索手机通讯录

    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 < 1000; i++) {
    String phone = getNumber(11);
    if (!cons.containsKey(phone))
    cons.put(phone, new Contact(getChar(6), getChar(10), phone,
    getChar(4)));
    }
    maps.put("0", cons);
    // 初始化,将全部联系人加入到lists
    }


    /**測试**/

    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 <= 10; 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");


    }


    /**
    * 搜索
    * @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);
    }


    /**
    * 获取组合

    * @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() {

    return name;

    }

    public void setName(String name) {

    this.name = name;

    }

    public String getPingyin() {

    return pingyin;

    }

    public void setPingyin(String pingyin) {

    this.pingyin = pingyin;

    }

    public String getPhone() {

    return phone;

    }

    public void setPhone(String phone) {

    this.phone = phone;

    }

    public String getIndex() {

    return index;

    }

    public void setIndex(String index) {

    this.index = index;

    }

    }


  • 相关阅读:
    windows cmd中查看某个命令所在的路径
    linux vi编辑器中,如何通过快捷键上下翻页?
    linux系统中,查看当前系统中,都在监听哪些端口
    下载mysql server安装包的时候,不登录oracle账号,实现下载
    plsql developer中,清除登录历史
    linux环境下,清空history中记录的历史命令
    Linux下搭建hadoop开发环境-超详细
    HDFS架构详解-非官档
    SSH免密码登录配置方法详解
    bin/hdfs dfs命令存在WARN util.NativeCodeLoader问题消除方法
  • 原文地址:https://www.cnblogs.com/yjbjingcha/p/7145662.html
Copyright © 2011-2022 走看看