有人能帮我简化这个字符串列表吗?
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类中提取字符串并启用正确布尔值的简单方法吗?即如果oneOfTheStringInThisList.matches(possibleEmail) mIsOld = true。
我不太熟悉字符串列表,很抱歉我的问题!谢谢!
发布于 2013-12-21 23:40:48
您可以通过反射获得这些值:
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存储在内存中,因为反射不是很好的表现。
https://stackoverflow.com/questions/20724218
复制相似问题