首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在简化字符串列表方面需要帮助

在简化字符串列表方面需要帮助
EN

Stack Overflow用户
提问于 2013-12-21 22:30:53
回答 1查看 54关注 0票数 0

有人能帮我简化这个字符串列表吗?

代码语言:javascript
复制
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";
}

代码语言:javascript
复制
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类中提取字符串并启用正确布尔值的简单方法吗?即如果oneOfTheStringInThisList.matches(possibleEmail) mIsOld = true。

我不太熟悉字符串列表,很抱歉我的问题!谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-21 23:40:48

您可以通过反射获得这些值:

代码语言:javascript
复制
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());
        }
    }
}

您应该将HashSets存储在内存中,因为反射不是很好的表现。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20724218

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档