java 中的哪个数据结构可以将 hashmap 作为其元素?
which data structure in java can hold hashmaps as its elements?
public static HashMap<String,Integer> users1 = new HashMap<>();
public static HashMap<String,Integer> users2 = new HashMap<>();
public static HashMap<String,Integer> users3 = new HashMap<>();
public static HashMap<String,Integer> users4 = new HashMap<>();
// Enter code here.
我需要将它们添加到 arrayList 或集合或其他东西中,然后在使用该数据结构索引时应调用它们。请问有什么办法可以解决吗
您可以像 :
List<HashMap<String, Integer>> list = new ArrayList<>();
// And then add them to list
list.add(users1);
list.add(users2);
...
ArrayList<HashMap> list = new ArrayList<>();
list.add(users1);
或者只是简单地使用一个数组:
public static HashMap<String,Integer>[] users = HashMap<>[4];
users[0] = = new HashMap<>();
users[1] = = new HashMap<>();
users[2] = = new HashMap<>();
users[3] = = new HashMap<>();
然后使用它们:
users[0].add( "String", 123 );
public static HashMap<String,Integer> users1 = new HashMap<>();
public static HashMap<String,Integer> users2 = new HashMap<>();
public static HashMap<String,Integer> users3 = new HashMap<>();
public static HashMap<String,Integer> users4 = new HashMap<>();
// Enter code here.
我需要将它们添加到 arrayList 或集合或其他东西中,然后在使用该数据结构索引时应调用它们。请问有什么办法可以解决吗
您可以像 :
List<HashMap<String, Integer>> list = new ArrayList<>();
// And then add them to list
list.add(users1);
list.add(users2);
...
ArrayList<HashMap> list = new ArrayList<>();
list.add(users1);
或者只是简单地使用一个数组:
public static HashMap<String,Integer>[] users = HashMap<>[4];
users[0] = = new HashMap<>();
users[1] = = new HashMap<>();
users[2] = = new HashMap<>();
users[3] = = new HashMap<>();
然后使用它们:
users[0].add( "String", 123 );