You need to iterate over your list, here is an example
public static void main(String[] args){
List<LinkedHashMap<String,String>> list = new ArrayList<LinkedHashMap<String, String>>();
LinkedHashMap<String, String> test = new LinkedHashMap<String, String>();
test.put("a","1");
test.put("b","2");
list.add(test);
for(Map<String, String> map : list){
for(String key : map.keySet()){
System.out.println("key: " + key + " value " + map.get(key));
}
}
}
Answer from Patrick M on Stack OverflowNo - you're storing them as a list, which isn't designed to be accessed by "key" - it doesn't have any notion of a key.
It sounds like really you want something like a Map<String, String> - assuming you only need to store a single value for each key.
If you're stuck with the ArrayList (and I'd at least change the parameter type to List, if not Iterable given that that's all you need) then what you've got is fine.
If you can use a Map instead, they work like this:
Map<String,String> myMap = new HashMap<>();
myMap.put("key1","value1");
myMap.put("key2","value2");
myMap.put("key3","value3");
String str = myMap.get("key1");
//equivalent to your
String str = getValueByKey(myList,"key1");
With a HashMap the keys are not stored as a list, so this will be faster and you won't have to use a function to iterate the container.
You can then access all the values with myMap.keySet() and myMap.values(), see the documentation.
I don't understand why you are creating a List of maps. A Map will let you put several key value pairs. Here is a way that would work:
protected Map<String, String> yaml_parse(BufferedReader filename) throws IOException{
String result;
Map<String, String> map = new HashMap<String, String>();
while ((result = filename.readLine()) != null) {
//keyValue[0] = key, keyValue[1] = value
String[] keyValue = result.split(": ");
map.put(keyValue[0], keyValue[1]);
}
return map;
}
And you would use it like this:
Map<String, String> map = yaml_parse("myFile.yaml");
String key1Value = map.get("key1"); //Stores key1's value into key1Value
I think you might be using the wrong data structure. From your question, it seems like you want a Map only, not a List of Maps.
You should look at changing your List<Map> to a Map. You can do this using:
Map<String, String> map = list.stream()
.flatMap(m -> m.entrySet().stream())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
If you want to work with your current data structure, you can get the required value like this:
private Optional<String> getValue(List<Map<String, String>> list, String key) {
return list.stream()
.filter(m -> m.containsKey(key))
.map(m -> m.get(key))
.findFirst();
}
and use it as follows:-
Optional<String> value = getValue(list, "key2");
System.out.println(value.orElse(null));
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.