Android遍历嵌套的哈希图

时间:2019-01-04 15:34:34

标签: java android hashmap

我正在尝试创建嵌套的哈希图以保存用户以前的工作,例如:

HashMap<String, String> firstJob = new HashMap<String, String>();
    firstJob.put("Title","instructor");
    firstJob.put("From","2008");
    firstJob.put("To","2010");
    firstJob.put("At","Company1");

    HashMap<String, String> secondJob = new HashMap<String, String>();
    secondJob.put("Title","PHP developer");
    secondJob.put("From","2010");
    secondJob.put("To","2013");
    secondJob.put("At","Company2");

    HashMap<String, String> thirdJob = new HashMap<String, String>();
    thirdJob.put("Title","PHP developer");
    thirdJob.put("From","2013");
    thirdJob.put("To","2018");
    thirdJob.put("At","Company3");

    HashMap<String, HashMap> myHashMap = new HashMap<String, HashMap>();
    myHashMap.put("first", firstJob);
    myHashMap.put("second", secondJob);
    myHashMap.put("third", thirdJob);

现在我想循环显示它,以第一行然后第二行等的形式显示它。

    String txt = "";

    Iterator it = myHashMap.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry pair = (Map.Entry)it.next();
        TextView d = (TextView) findViewById(R.id.data);
        txt += pair.getKey() + " = " + pair.getValue() + "\n";
        d.setText(txt);
    }

pair.getValue()具有整个哈希图值{From = 2008,At = Company1,To = 2010,Title = instructor},现在我要对其进行迭代以将其显示为列表

Position:instructor
Started:2008
Ended:2010
At:Company1

然后下一份工作,等等...如何遍历这些内部哈希图?

如果这不是最好的方法,那么什么是最好的方法?

谢谢。

1 个答案:

答案 0 :(得分:4)

像这样创建一个类或模型... 我还建议您更改变量名称。请勿使用title, from, to, at,因为使用它们时可能会使您的代码混乱。更有创造力。

public class ModelExample {

        String title;
        String from;
        String to;
        String at;

        public ModelExample(String title, String from, String to, String at) {
            this.title = title;
            this.from = from;
            this.to = to;
            this.at = at;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getFrom() {
            return from;
        }
        public void setFrom(String from) {
            this.from = from;
        }
        public String getTo() {
            return to;
        }
        public void setTo(String to) {
            this.to = to;
        }
        public String getAt() {
            return at;
        }
        public void setAt(String at) {
            this.at = at;
        }
}

创建一个列表并将其插入。

List<ModelExample> model = new ArrayList<ModelExample>();
model.add(new ModelExample("instructor", "2008", "2010", "Company1"));
model.add(new ModelExample("PHP Dev", "2010", "2013", "Company 3"));
model.add(new ModelExample("PHP Dev 2", "2013", "2018", "Company3"));

然后您可以遍历此列表。

for (int i = 0; i < model.size(); i++) {
    System.out.println("Title: " + model.get(i).getTitle());
    System.out.println("From: " + model.get(i).getFrom());
    System.out.println("At: " + model.get(i).getAt());
    System.out.println("To: " + model.get(i).getTo());
    System.out.println("\n------------------\n");
}

这将打印出以下内容。

Title: instructor
From: 2008
At: Company1
To: 2010

------------------

Title: PHP Dev
From: 2010
At: Company 3
To: 2013

------------------

Title: PHP Dev 2
From: 2013
At: Company3
To: 2018

------------------