This question already has an answer here:
这个问题在这里已有答案:
- Java Equivalent to Python Dictionaries 4 answers
- Java相当于Python Dictionaries 4个答案
I have a Dictionary in Python say
我在Python中有一个字典说
{ SERVER1 : [ (list1),(list2),....
SERVER2 : [ (list2a),(list2b) ] }
Is it same can be implemented in Java ? I need to check each key if exist then I need to add the list to that key like appending the existing value without overriding Also need to read the key and value and traverse through each value of list
它是否可以用Java实现?我需要检查每个密钥是否存在然后我需要将列表添加到该密钥,如添加现有值而不覆盖还需要读取密钥和值并遍历列表的每个值
Kindly assist. Sorry to ask these question as it is my first program in Java
请帮助。很抱歉问这些问题,因为这是我的第一个Java程序
2 个解决方案
#1
3
In java, dictionaries are defined with the Map
interface. The common implementations are TreeMap
and HashMap
:
在java中,字典是使用Map接口定义的。常见的实现是TreeMap和HashMap:
Map<String,List<String>> servers = new HashMap<>();
servers.put("SERVER1", new ArrayList<String>());
servers.get("SERVER1").add("list1");
servers.get("SERVER1").add("list2");
...
Similarly, ArrayList
and LinkedList
are common implementations of the List
interface.
类似地,ArrayList和LinkedList是List接口的常见实现。
#2
0
Because you want to save each value associated with a key, you need a HashMap that contains ArrayLists for values. This object should do what you need:
因为要保存与键关联的每个值,所以需要包含值的ArrayLists的HashMap。这个对象应该做你需要的:
public class HashOfLists<K, V> {
HashMap<K, ArrayList<V>> map = new HashMap<>();
public ArrayList<V> get(K key) {
return map.get(key);
}
public void put(K key, V value) {
ArrayList<V> existingValues = get(key);
if (existingValues == null) existingValues = new ArrayList<>();
existingValues.add(value);
map.put(key, existingValues);
}
}
To use it you would do something a bit like this:
要使用它,你会做一些像这样的事情:
HashOfLists<String, String> servers = new HashOfLists<>();
servers.put("SERVER3", "PROCESS A");
servers.put("SERVER3", "PROCESS B");
//Getting a list containing "PROCESS A" and "PROCESS B"
ArrayList<String> processes = servers.get("SERVER3");
#1
3
In java, dictionaries are defined with the Map
interface. The common implementations are TreeMap
and HashMap
:
在java中,字典是使用Map接口定义的。常见的实现是TreeMap和HashMap:
Map<String,List<String>> servers = new HashMap<>();
servers.put("SERVER1", new ArrayList<String>());
servers.get("SERVER1").add("list1");
servers.get("SERVER1").add("list2");
...
Similarly, ArrayList
and LinkedList
are common implementations of the List
interface.
类似地,ArrayList和LinkedList是List接口的常见实现。
#2
0
Because you want to save each value associated with a key, you need a HashMap that contains ArrayLists for values. This object should do what you need:
因为要保存与键关联的每个值,所以需要包含值的ArrayLists的HashMap。这个对象应该做你需要的:
public class HashOfLists<K, V> {
HashMap<K, ArrayList<V>> map = new HashMap<>();
public ArrayList<V> get(K key) {
return map.get(key);
}
public void put(K key, V value) {
ArrayList<V> existingValues = get(key);
if (existingValues == null) existingValues = new ArrayList<>();
existingValues.add(value);
map.put(key, existingValues);
}
}
To use it you would do something a bit like this:
要使用它,你会做一些像这样的事情:
HashOfLists<String, String> servers = new HashOfLists<>();
servers.put("SERVER3", "PROCESS A");
servers.put("SERVER3", "PROCESS B");
//Getting a list containing "PROCESS A" and "PROCESS B"
ArrayList<String> processes = servers.get("SERVER3");