繁体   English   中英

如何从ArrayList检索值 <String> 如果后者是Map中的Key?

[英]How to retrieve values from ArrayList<String> if the latter is a Key in Map?

我是Java新手。 现在我正在尝试使用类似的结构

 Map<List<String>,String>    

..并且很难从列表中检索字符串。

详细信息:我正在制作一个简单的程序,该程序可以接收电话号码并检查哪个国家/地区的特定号码所属。 它通过与正则表达式匹配来检查它们(基础位数和国际前缀:因为前缀(因此-正则表达式)可能不止一个-选择了List)。

因此,正则表达式的结构为:

"^(%s)\\d{%d}", prefix, digitAmount    

有一些数字的基本数据(芬兰语,英语),但用户也可以添加新的数字(例如:贡杜拉斯-前缀:849,数字位数:8,贡都拉斯(相同)-前缀49,数字位数:7等。)因此,当您输入像893143045839这样的数字时-它应该查找它是否与正则表达式匹配。 如果是,则返回国家(值)。 这就是为什么选择Map。

这里的代码是:

static Map <List<String>, String> data = new HashMap<List<String>, String>();
static List<String> newdata = new ArrayList<String>();

void addBasicData ()  // here I add some predefined parameters
{
List<String> finnNumbers = new ArrayList<String>();
finnNumbers.add("^(3589)\\d{6}");
finnNumbers.add("^(003589)\\d{6}");
finnNumbers.add("^(09)\\d{6}");
data.put(finnNumbers, "Finland");
}


void addNewData (String pattern, List<String> newdata)   //here I add new data from user
{
pattern();
data.put(newdata, pattern);
}    

该问题进一步发生。 当我想检查一些数字时,我必须做一个模式-应该从数据中获取正则表达式。 好吧,看来我做不到。 这是方法

void numberCheck (String inputNumber)
{

// maybe I'll use Set values = data.keySet(); ? ...nah, doesn't work either :(


for (int i = 0; i<= data.size(); i++)
{
    Object c = data.get(i);
    List<String> check = (List<String>)c;
    for (int j = 0; j <check.size(); j++)   
    {

        Pattern pat = Pattern.compile(check.get(j));
        Matcher matcher = pat.matcher(inputNumber);
        if (matcher.find())
        {
            // some useful code
        }

    }


}

..,这不起作用,并给出NullPointerException。 当我想取一个密钥,然后将每个正则表达式作为模式尝试时,它给了我一个对象,而不是ArrayList。 这就是为什么我问。 我试图像反向使用它(Map>),但是随后我需要迭代值,并且存在相同的问题。

这就是重点

    Object c = data.get(i);
    List<String> check = (List<String>)c;

,一切都崩溃了。 也许,我这样做完全错误,在我的情况下应该使用其他类。

如果有人可以提出建议,我将很高兴。 谢谢!

我想那就是你想做的

Map<String, List<String>> map = new HashMap<String, List<String>>();
// ...some more code...
for (List<String> regexNumbers : map.values()) {
    // regexNumbers is the value
}

由于您不使用方法中的密钥。

但是,如果您创建一个类,那么您的代码将更具可读性-那就是您的结构,那就是您想要的,并且您知道(!)其中的内容(即使几年后):

class CountryNumbers {
    String country;
    List<String> numbers;
}

将每个号码打印到一个国家的示例代码:

List<CountryNumbers> list = fillListSomehow();
for (CountryNumbers countryNumbers : list) {
    String country = countryNumbers.country;
    System.out.println("The regexes for country " + country + " are: ");
    for (String regexNumber : countryNumbers.numbers) {
        System.out.println("  " + regexNumber);
    }
}

代替

static Map <List<String>, String> data = new HashMap<List<String>, String>();

你应该做

static Map <String, List<String>> data = new HashMap<String, List<String>>();

然后,当您尝试遍历这些值时

void numberCheck (String inputNumber)
{


    String[] keys = (String[]) data.keySet().toArray();
for (int i = 0; i<= keys.length; i++)
{
    List<String> check = (List<String>)data.get(keys[i]);
    for (int j = 0; j <check.size(); j++)   
    {

        Pattern pat = Pattern.compile(check.get(j));
        Matcher matcher = pat.matcher(inputNumber);
        if (matcher.find())
        {
            // some useful code
        }

    }


}
}

它实际上将返回一个列表。 通过调用data.get(i)您正在请求Map中索引i处的数据集的VALUE。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM