[英]ArrayList<HashMap<String,String>> keys iteration/output to arrays
If I have a type 如果我有类型
ArrayList<HashMap<String,String>> keys = functionWhichReturnsThisType();
How can I iterate through so that I end up with all <1st string of hashmap> in a string array and likewise <2nd string of hashmap> into another string array. 我该如何遍历,以便最后将所有<hashmap的第一字符串>放在一个字符串数组中,同样将<hashmap的第二字符串>放入另一个字符串数组中。
I have tried to use the iterator but the hierarchy in the data type is confusing me. 我尝试使用迭代器,但是数据类型中的层次结构使我感到困惑。
appPrefs = new AppPreferences(context.getApplicationContext());
ArrayList<HashMap<String,String>> keys = appPrefs.getDownloadUrls();
ArrayList<String> urls = new ArrayList<String>();
ArrayList<String> filenames = new ArrayList<String>();
Iterator myIterator = keys.keySet().iterator();
while(myIterator.hasNext()) {
urls.add((String)myIterator.next());
filenames.add((String)keys.get(myIterator.next()));
}
If the order doesn't matter, you can try 如果顺序无关紧要,您可以尝试
for (HashMap<String, String> map : keys) {
urls.addAll(map.keys());
filenames.addAll(map.values());
}
If you want to keep the order, you can try 如果您想保留订单,可以尝试
for (HashMap<String, String> map : keys) {
for (Map.Entry<String, String> entry : map.entrySet()) {
urls.add(entry.getKey());
filenames.add(entry.getValue());
}
}
OK, I'll walk through your sample code and show where you're running into issues, and suggest how you can get it to work. 好的,我将遍历您的示例代码,并向您显示遇到问题的地方,并提出如何使它工作的建议。
ArrayList<HashMap<String,String>> keys = appPrefs.getDownloadUrls();
This (above) is fine - but remember keys
is an ArrayList
. (上面)很好-但是记住
keys
是ArrayList
。 It's a list of HashMap
objects, but it's still a list 这是
HashMap
对象的列表,但仍然是列表
ArrayList<String> urls = new ArrayList<String>();
ArrayList<String> filenames = new ArrayList<String>();
These are good, but in typical Java, it would be better to have List<String> urls = new ArrayList<String>();
这些很好,但是在典型的Java中,最好使用
List<String> urls = new ArrayList<String>();
to try and keep your variables using interfaces instead of concrete implementations. 尝试使用接口而不是具体的实现来保留变量。
Iterator myIterator = keys.keySet().iterator();
while(myIterator.hasNext()) {
This won't work, because keys
is an ArrayList
, and a list does not have a keySet()
you want to do: 这是行不通的,因为
keys
是ArrayList
,并且列表没有要执行的keySet()
:
Iterator<HashMap<String,String> listIterator = keys.iterator();
while(listIterator.hasNext()) {
HashMap<String,String> map = listIterator.next();
Iterator<String> myIterator = map.keySet().iterator();
while(myIterator.hasNext()) {
Or, even better would be to use the Java 1.5 for(each) loop: 或者,甚至更好的方法是使用Java 1.5 for(each)循环:
for( Map<String,String> map : keys ) {
for( String url : map.keySet() ) {
-- -
urls.add((String)myIterator.next());
The above would work, once you get myIterator
to be an iterator over the map, rather than the list. 一旦使
myIterator
成为地图(而不是列表)的迭代器,上述方法就可以工作。
filenames.add((String)keys.get(myIterator.next()));
But this won't for 2 reasons 但这不会有两个原因
keys
is still a list. keys
仍然是列表。 next
on an iterator twice then you get 2 different objects. next
,那么您将获得2个不同的对象。 You need to have: 您需要具备:
String url = myIterator.next();
urls.add(url);
filenames.add(map.get(url));
Or, if you use the for(each) loop I suggested above, then you can skip that first line. 或者,如果使用上面建议的for(each)循环,则可以跳过第一行。
Hope that helps - if something's unclear please add a comment. 希望有帮助-如果不清楚,请添加评论。
Note: solilo
's solution is a lot simpler and is a good way to do it, my answer is here to help you see where you were running into trouble. 注意:
solilo
的解决方案非常简单,并且是解决问题的一种好方法,我的回答是在这里帮助您查看遇到麻烦的地方。
This method will work for you extract first and second strings 此方法适用于您提取第一和第二个字符串
private void getFirstAndSecondStrings(ArrayList<HashMap<String,String>> keys){
ArrayList<String> firstStrings = new ArrayList<String>();
ArrayList<String> secondStrings = new ArrayList<String>();
for (HashMap<String, String> map : keys) {
Iterator iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry pairs = (Map.Entry)iterator.next();
firstStrings.add((String)pairs.getValue());
secondStrings.add((String)pairs.getKey());
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.