开发者

Access object from hash map

开发者 https://www.devze.com 2023-02-14 08:00 出处:网络
My Hashmap as declared as HashMap<String, ArrayList<SortableContactList>> where SortableContactList list is a POJO class as

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:

  1. Implement Comparable interface in your SortableContactList class; implement the functioning for the method compareTo(). This interface helps you to change the natural ordering for sort() method.

  2. If you want to sort using different attributes then you should implement Comparator interface containing the compare(Object1,Object2) here you can provide on what what basis you want to sort your object.

hope this is useful

0

精彩评论

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