需要帮助来简化字符串列表

时间:2013-12-21 22:30:53

标签: android string list

有人可以帮我解决这个字符串列表:

public class MyList {

    // New customers
    public static final String mNew1 = "email1";
    public static final String mNew2 = "email2";
    public static final String mNew3 = "email3";
    public static final String mNew4 = "email4";

    // Old customers
    public static final String mOld1 = "email1";
    public static final String mOld2 = "email2";
    public static final String mOld3 = "email3";
}

public class App extends Application {
    public static boolean mIsNew = false;
    public static boolean mIsOld = false;

Pattern emailPattern = Patterns.EMAIL_ADDRESS;
        Account[] accounts = AccountManager.get(context).getAccounts();
        for (Account account : accounts) {
            if (emailPattern.matcher(account.name).matches()) {
                String possibleEmail = account.name;
                if (MyList.mNew1.matches(possibleEmail) || MyList.mNew2.matches(possibleEmail) ||
                        MyList.mNew3.matches(possibleEmail) || MyList.mNew4.matches(possibleEmail)) {
                    mIsNew = true;
                }
                if (MyList.mOld1.matches(possibleEmail) || MyList.mOld2.matches(possibleEmail) || MyList.mOld3.matches(possibleEmail)) {
                    mIsOld = true;
                }
            }
        }

由于旧客户的电子邮件将在不到一周的时间内超过10.000,您能否建议我从MyList类中选择字符串并启用正确的布尔值? I.E如果oneOfTheStringInThisList.matches(possibleEmail)mIsOld = true。

我对字符串列表并不熟悉,对不起我的菜鸟问题!三江源!

1 个答案:

答案 0 :(得分:1)

您可以通过反射获取值:

private void Something()
{
    MyList list = new MyList();
    HashSet<String> oldMails = new HashSet<String>();
    HashSet<String> newMails = new HashSet<String>();
    try {
        GetAllMails(list, oldMails, newMails);
    } catch (IllegalAccessException e) {
        // TODO
    }

    boolean mIsOld = oldMails.contains("email4");
    boolean mIsNew = newMails.contains("email4");
}

private void GetAllMails(MyList list, HashSet<String> oldMails, HashSet<String> newMails) throws IllegalAccessException
{
    Field[] allFields = MyList.class.getDeclaredFields();   
    for(Field f : allFields)
    {
        if (f.getName().startsWith("mNew"))
        {
            newMails.add(f.get(list).toString());
        }
        else if (f.getName().startsWith("mOld"))
        {
            oldMails.add(f.get(list).toString());
        }
    }
}

您应该将HashSet存储在内存中,因为反射效果不是很好。