1

i have this arrayalist:

 ArrayList<HashMap<String, String>> contactListad

it contains:

[{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}]

My question iy ifs how can i get the values and store them in another arraylist i want to specify to store only the values having a certain homeAddress equals paris for example. i hope my question is clear enough

for example if i specify the homeAddress to be paris, i want the output to be an arraylist containing

[{emailAddress=samir, lastName=samir, contactId=4, phoneNumber=6449494, firstName=samir,     
homeAddress=paris},{emailAddress=, lastName=, contactId=9, phoneNumber=, firstName=joe,    
homeAddress=paris}]

so without the hasmap that has homeAddress equals las vegas

Thank you

1
  • I'm not sure I'm following. Can you share the output you're trying to get? Commented Oct 9, 2014 at 11:31

2 Answers 2

4
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);
    }

}

now you have what you want in newArray

Sign up to request clarification or add additional context in comments.

2 Comments

+1.But bad practice to name a variable as newArray which is actually a list.
@TAsk you are 100% right, but i just gave him an example
0
    for(HashMap<String, String> hashMap : contactListad) {
        String homeAdress = hashMap.get("homeAdress");
        if (homeAdress.equals("Paris")) {
            //write your code here
        }
    }

1 Comment

no i did not. i had the edit window open and did not see your answer. and hey - what should the solution look like other than the 2 answers posted ?? anyway thanks for the downvote

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.