[英]How to retrieve values from ArrayList<String> if the latter is a Key in Map?
I am new to Java. 我是Java新手。 Now I'm trying to work with a structure like
现在我正在尝试使用类似的结构
Map<List<String>,String>
..and there are difficulties with retrieving Strings from the List. ..并且很难从列表中检索字符串。
Details: I am making a simple program which takes phone numbers and checks to which country specific number belongs. 详细信息:我正在制作一个简单的程序,该程序可以接收电话号码并检查哪个国家/地区的特定号码所属。 It checks them via matching with regular expressions (underlying digit amount and international prefix: because prefixes (and thus - regular expressions) may be more then one - List is chosen).
它通过与正则表达式匹配来检查它们(基础位数和国际前缀:因为前缀(因此-正则表达式)可能不止一个-选择了List)。
So, the structure of regex is: 因此,正则表达式的结构为:
"^(%s)\\d{%d}", prefix, digitAmount
There are some basic data for some numbers (Finnish, English), but user can also add new ones (example: Gonduras - prefix: 849, number of digits: 8, Gonduras (same) - prefix 49, number of digits: 7 etc.) So when you will enter a number like 893143045839 - it should find whether it matches regular expression. 有一些数字的基本数据(芬兰语,英语),但用户也可以添加新的数字(例如:贡杜拉斯-前缀:849,数字位数:8,贡都拉斯(相同)-前缀49,数字位数:7等。)因此,当您输入像893143045839这样的数字时-它应该查找它是否与正则表达式匹配。 And if it is, return the country (a value).
如果是,则返回国家(值)。 That's why the Map is chosen.
这就是为什么选择Map。
The code here is: 这里的代码是:
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);
}
The problem occurs further. 该问题进一步发生。 When I want to check some number, I have to make a pattern - which should take the regex from the data.
当我想检查一些数字时,我必须做一个模式-应该从数据中获取正则表达式。 Well, it appears that I can't.
好吧,看来我做不到。 Here is the method..
这是方法
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
}
}
}
.., which do not work and gives NullPointerException. ..,这不起作用,并给出NullPointerException。 When I want to take a key and then try each regex as a pattern, it instead gives me an Object, not an ArrayList.
当我想取一个密钥,然后将每个正则表达式作为模式尝试时,它给了我一个对象,而不是ArrayList。 That's why I am asking.
这就是为什么我问。 I tried to use it reverse-like ( Map> ), but then I need to iterate values, and there is the same problem.
我试图像反向使用它(Map>),但是随后我需要迭代值,并且存在相同的问题。
This is the point 这就是重点
Object c = data.get(i);
List<String> check = (List<String>)c;
, where everything collapses. ,一切都崩溃了。 Perhaps, I'm doing it entirely wrong and should use another classes in my case.
也许,我这样做完全错误,在我的情况下应该使用其他类。
I would be glad, if anyone can give an advice. 如果有人可以提出建议,我将很高兴。 Thank you!
谢谢!
I guess thats what you want to do 我想那就是你想做的
Map<String, List<String>> map = new HashMap<String, List<String>>();
// ...some more code...
for (List<String> regexNumbers : map.values()) {
// regexNumbers is the value
}
Since you dont use the key in your method. 由于您不使用方法中的密钥。
But your code whould be much more readable if you create a class - thats your structure, thats what you want, and you know (!) whats in it (even in a few years): 但是,如果您创建一个类,那么您的代码将更具可读性-那就是您的结构,那就是您想要的,并且您知道(!)其中的内容(即使几年后):
class CountryNumbers {
String country;
List<String> numbers;
}
Examplecode to print every number to a country: 将每个号码打印到一个国家的示例代码:
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);
}
}
Instead of 代替
static Map <List<String>, String> data = new HashMap<List<String>, String>();
You should do 你应该做
static Map <String, List<String>> data = new HashMap<String, List<String>>();
Then, down when you are trying to go through the values 然后,当您尝试遍历这些值时
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
}
}
}
}
It will actually return a List. 它实际上将返回一个列表。 By calling
data.get(i)
you are asking for the VALUE of the data set at index i
in the Map. 通过调用
data.get(i)
您正在请求Map中索引i
处的数据集的VALUE。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.