![](/img/trans.png)
[英]Retrieving value from key/value in ArrayList HashMap<String, String>
[英]retrieving certain values from ArrayList<HashMap<String, String>>
我有這個數組主義者:
ArrayList<HashMap<String, String>> contactListad
它包含:
[{emailAddress=samir, lastName=samir, contactId=4, phoneNumber=6449494, firstName=samir, homeAddress=paris}, {emailAddress=, lastName=, contactId=6, phoneNumber=, firstName=Rashad, homeAddress=las vegas}, {emailAddress=, lastName=, contactId=9, phoneNumber=, firstName=joe, homeAddress=paris}]
我的問題是,如果我如何獲取這些值並將其存儲在另一個我想指定僅存儲具有特定homeAddress等於巴黎的值的數組列表中。 我希望我的問題很清楚
例如,如果我將homeAddress指定為巴黎,我希望輸出為包含以下內容的arraylist
[{emailAddress=samir, lastName=samir, contactId=4, phoneNumber=6449494, firstName=samir,
homeAddress=paris},{emailAddress=, lastName=, contactId=9, phoneNumber=, firstName=joe,
homeAddress=paris}]
因此,如果沒有具有homeAddress的hasmap等於拉斯維加斯
謝謝
List<HashMap<String,String>> newArray = new ArrayList<HashMap<String, String>>();
for(HashMap<String, String> hm : contactListad){
String val = hm.get("homeAddress");
if("paris".equals(val)){
newArray.add(hm);
}
}
現在您在newArray
擁有了想要的東西
for(HashMap<String, String> hashMap : contactListad) {
String homeAdress = hashMap.get("homeAdress");
if (homeAdress.equals("Paris")) {
//write your code here
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.