简体   繁体   English

如何使用其他hashmap的对象在hashmap中定义hashmap

[英]how to define hashmap within hashmap using object of other hashmap

HashMap<String, HashMap<String, String>> hm = new HashMap<String, HashMap<String, String>>();
        hm.put("Title1","Key1");
            for(int i=0;i<2;i++) {
                HashMap<String, String> hm1 = new HashMap<String, String>();
                hm1.put("Key1","Value1");
            }

if i have call Title1 that time they call another hashmap. 如果我有时间调用Title1,他们会调用另一个hashmap。 i want this type of output 我想要这种类型的输出

hm<key,value(object hm1)>
hm<key,value)

first hashmap object call second hashmap key 第一个hashmap对象调用第二个hashmap键

If I correct undestand what you want, use following code 如果我纠正你想要的东西,请使用以下代码

HashMap<String, HashMap<String, String>> hm = new HashMap<>();
            HashMap<String, String> hm1 = new HashMap<>();
            for(int i=0;i<2;i++) {
                hm1.put("Key1","Value1");
            }
        hm.put("Title1", hm1); // save hm

... ...

HashMap<String, String> hm2 = hm.get("Title1"); 
String s = hm2.get("Key1"); // s =  "Value1"

OR you can create new class 或者你可以创建新的类

class HashKey {
 private String title;
 private String key;
 ...
 // getters, setters, constructor, hashcode and equals
} 

and just use HashMap < HashKey, String > hm, for example: 并且只使用HashMap <HashKey,String> hm,例如:

  hm.put(new HashKey("Title1", "Key 1"), "Value");

  ...
  String s = hm.get(new HashKey("Title1", "Key 1")); // Value

you can do something likewise, 你可以做同样的事,

HashMap<String,HashMap<String,String>> hm = new HashMap<String,HashMap<String,String>>();

HashMap<String,String> hm1 = new HashMap<String,String>();
hm1.put("subkey1","subvalue");

hm.put("Title1",hm1);

HashMap<String,String> newhm = hm.get("Title1");
import java.util.HashMap;
import java.util.Map;

public class MapInMap {
    Map<String, Map<String, String>> standards =
 new HashMap<String, Map<String, String>>();

    void addValues() {
        Map<String, String> studentA = new HashMap<String, String>();
        studentA.put("A1", "49");
        studentA.put("A2", "45");
        studentA.put("A3", "43");
        studentA.put("A4", "39");
        standards.put("A", studentA);
        Map<String, String> studentB = new HashMap<String, String>();
        studentB.put("B1", "29");
        studentB.put("B2", "25");
        studentB.put("B3", "33");
        studentB.put("B4", "29");
        standards.put("B", studentB);

    }

    void disp() {
        for (Map.Entry<String, Map<String, String>> entryL1 : standards
                .entrySet()) {
            System.out.println("Standard :" + entryL1.getKey());
            for (Map.Entry<String, String> entryL2 : entryL1.getValue()
                    .entrySet()) {
                System.out.println(entryL2.getKey() + "/" + entryL2.getValue());
            }
        }

    }

    public static void main(String args[]) {
        MapInMap inMap = new MapInMap();
        inMap.addValues();
        inMap.disp();
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM