开发者

Java, drilldown an ArrayList of HashMaps

开发者 https://www.devze.com 2023-02-11 03:00 出处:网络
I have an ArrayList<HashMap<String, String>>. I\'d like to quickly extract from this a new ArrayList<String> comprisi开发者_如何学编程ng all the keys.

I have an ArrayList<HashMap<String, String>>. I'd like to quickly extract from this a new ArrayList<String> comprisi开发者_如何学编程ng all the keys.

How do I do this?


I suggest you do

List<String> allKeys = new ArrayList<String>();

for (Map<String, String> map : yourListOfMaps)
    allKeys.addAll(map.keySet());

If you're not interested in duplicate keys (i.e., if you don't want two identical entries in allKeys just because it exists as key in two maps) I would suggest you let allKeys be of type HashSet<String> instead.

After traversal you could do allKeysList = new ArrayList<String>(allKeys); if order is important to you in the end.

0

精彩评论

暂无评论...
验证码 换一张
取 消