Access object from hash map
My Hashmap as declared as HashMap<String, ArrayList<SortableContactList>>
where SortableContactList
list is a POJO class as
public class SortableContactList {
private long id;
private String displayName;
private String homePhone;
private String workPhone;
private String mobilePhone;
private String primaryEmail;
private String tags;
// Getters and Setters
}
Initializing my hashmap as
myHash.put(keyChar, arrayOfSortableContactList_objects)
My need 开发者_运维问答is to get each attribute of an object which stored in arraylist. How can I make this.
Thank you!
If I understand the question correctly, you would need sth in the lines of myHash.get("myKeyString").get(x).getHomePhone()
for individual attributes. The question remains, how to determine x, so maybe you should consider accepting the advice of MarcoS, implementing the HashMap
as follows : HashMap<String, SortableContactList>
And you should definitely consider implementing hashCode for your SortableContactList to ensure correct hashing , i.e. storing and retrieving the objects in a HashMap. For details please consult this great advice from Effective Java
EDIT : added MarcoS' advice of implemeting the HashMap as HashMap<String, SortableContactList>
could not understand your question. please elaborate some more.
Well there are 2 ways you can achieve sorting:
Implement Comparable interface in your SortableContactList class; implement the functioning for the method
compareTo()
. This interface helps you to change the natural ordering forsort()
method.If you want to sort using different attributes then you should implement
Comparator
interface containing thecompare(Object1,Object2)
here you can provide on what what basis you want to sort your object.
hope this is useful
精彩评论