在 Java 中,如果我不知道总共有多少个键,如何将 String 映射到 Set<String>
in Java, how can I map String to a Set<String> if I do not know how many keys in total
我需要一个HashMap,key是String,value是Set,比如:
键:"a",值:{"a","b","c".....}
键:"b",值:{"a,","d".....}
...
但我不知道总共有多少个键,这取决于其他方法的结果。
基本上,方法如下所示:(地图可以是字段)
public void mapKeyValue(int numbersOfKey, HashMap map){
//some code
}
所以如果我这样写代码:
public void mapKeyValue(int numbersOfKey, HashMap map){
for (int i = 0; i < numbersOfKey; i++){
Set<String> set = new HashSet<String>();
set.add("some strings");// we can add some strings here
map.put("OneString", set);
}
}
该方法后,我将什么也得不到,因为我将丢失该方法创建的所有 Set 对象,所以我无法通过调用 map.get("OneString") 来获取 Set。
那我想得到那个hashMap怎么办呢?
您的代码存在许多问题,但我建议采用以下方法。
在您的例子中,您似乎有一个 Map>,它是字符串键到一组字符串的映射。
如果这就是你想要的,我建议你
- 检查键是否有值。如果没有,则为周围的地图添加一个空集。
- 通过键从地图中获取集合。
- 在集合中添加或删除任何需要的值
请注意,您编写的代码始终会替换使用键 "OneString" 存储的集合,这意味着无论值 "numbersOfKey" 是什么,您实际上只是在单个键 "OneString" numbersOfKey 次。
你可能想做类似的事情
public void addToSet(String setName, String value) {
if (!sets.containsKey(setName)) {
sets.put(setName, new HashSet<String>());
}
Set<String> values = sets.get(setName);
values.add(value);
}
此块假定您在 class 中的某处有一个成员变量,如
private Map<String, Set<String>> sets = new HashMap<>();
请注意,此代码是一个想法,而不是生产代码。在现实世界中,您添加的内容可能最终会在某个时间点被删除。因此,您希望能够在程序执行的某个未来点删除特定值或整组值及其键。
你不能那样做吗?
public HashMap<String, Set<String>> mapKeyValue(int numbersOfKey){
HashMap<String, Set<String>> map = new HashMap<>();
for (int i = 0; i < numbersOfKey; i++){
Set<String> set = new HashSet<String>();
set.add("some strings" + "" + i);// we can add some strings here
map.put("OneString", set);
}
return map;
}
我建议使用 Apache Commons Collection MultiValueMap 而不是每次都创建一个 Set。两者都工作得很好,但是有一个 Map 可以为您完成所有这些工作,并且它基于 HashMap,从而保持您的持续时间访问。此处的 Javadoc:
像这样...
public void someOtherMethod() {
// Assuming the Map is created and used somewhere outside the mapKeyValue method. Otherwise it should be instantiated inside the mapKeyValue method
MultiValueMap<String, String> map = new MultiValueMap<>();
//2 is an arbitrary, made up number that you select somehow
mapKeyValue(2, map);
//Access the values of the map dynamically without knowing how many keys there are
for (String key : map.keySet()) {
System.out.print(key + " : ");
for (String value : map.getCollection(key)) {
System.out.print(value + ", ");
}
}
}
public MultiValueMap<String, String> mapKeyValue(int numbersOfKey, MultiValueMap<String, String> map){
for (int i = 0; i < numbersOfKey; i++){
//We need to create a unique key here, so let's use 'i'
//There are several ways to skin the cat and get the int to String
//Also want to create unique values, but that's up to you, they're not required to be unique
map.put(Integer.toString(i), Integer.toString(i) + "a");
map.put(Integer.toString(i), Integer.toString(i) + "b");
map.put(Integer.toString(i), Integer.toString(i) + "c");
}
//At this point, the map has in it the following key : value pairs
//"0" : ["0a", "0b", "0c"]
//"1" : ["1a", "1b", "1c"]
//"2" : ["2a", "2b", "2c"]
//Not technically required to return the map IFF the map is instantiated outside the method
return map;
}
我需要一个HashMap,key是String,value是Set,比如:
键:"a",值:{"a","b","c".....} 键:"b",值:{"a,","d".....} ...
但我不知道总共有多少个键,这取决于其他方法的结果。
基本上,方法如下所示:(地图可以是字段)
public void mapKeyValue(int numbersOfKey, HashMap map){
//some code
}
所以如果我这样写代码:
public void mapKeyValue(int numbersOfKey, HashMap map){
for (int i = 0; i < numbersOfKey; i++){
Set<String> set = new HashSet<String>();
set.add("some strings");// we can add some strings here
map.put("OneString", set);
}
}
该方法后,我将什么也得不到,因为我将丢失该方法创建的所有 Set 对象,所以我无法通过调用 map.get("OneString") 来获取 Set。 那我想得到那个hashMap怎么办呢?
您的代码存在许多问题,但我建议采用以下方法。
在您的例子中,您似乎有一个 Map
如果这就是你想要的,我建议你
- 检查键是否有值。如果没有,则为周围的地图添加一个空集。
- 通过键从地图中获取集合。
- 在集合中添加或删除任何需要的值
请注意,您编写的代码始终会替换使用键 "OneString" 存储的集合,这意味着无论值 "numbersOfKey" 是什么,您实际上只是在单个键 "OneString" numbersOfKey 次。
你可能想做类似的事情
public void addToSet(String setName, String value) {
if (!sets.containsKey(setName)) {
sets.put(setName, new HashSet<String>());
}
Set<String> values = sets.get(setName);
values.add(value);
}
此块假定您在 class 中的某处有一个成员变量,如
private Map<String, Set<String>> sets = new HashMap<>();
请注意,此代码是一个想法,而不是生产代码。在现实世界中,您添加的内容可能最终会在某个时间点被删除。因此,您希望能够在程序执行的某个未来点删除特定值或整组值及其键。
你不能那样做吗?
public HashMap<String, Set<String>> mapKeyValue(int numbersOfKey){
HashMap<String, Set<String>> map = new HashMap<>();
for (int i = 0; i < numbersOfKey; i++){
Set<String> set = new HashSet<String>();
set.add("some strings" + "" + i);// we can add some strings here
map.put("OneString", set);
}
return map;
}
我建议使用 Apache Commons Collection MultiValueMap 而不是每次都创建一个 Set。两者都工作得很好,但是有一个 Map 可以为您完成所有这些工作,并且它基于 HashMap,从而保持您的持续时间访问。此处的 Javadoc:
像这样...
public void someOtherMethod() {
// Assuming the Map is created and used somewhere outside the mapKeyValue method. Otherwise it should be instantiated inside the mapKeyValue method
MultiValueMap<String, String> map = new MultiValueMap<>();
//2 is an arbitrary, made up number that you select somehow
mapKeyValue(2, map);
//Access the values of the map dynamically without knowing how many keys there are
for (String key : map.keySet()) {
System.out.print(key + " : ");
for (String value : map.getCollection(key)) {
System.out.print(value + ", ");
}
}
}
public MultiValueMap<String, String> mapKeyValue(int numbersOfKey, MultiValueMap<String, String> map){
for (int i = 0; i < numbersOfKey; i++){
//We need to create a unique key here, so let's use 'i'
//There are several ways to skin the cat and get the int to String
//Also want to create unique values, but that's up to you, they're not required to be unique
map.put(Integer.toString(i), Integer.toString(i) + "a");
map.put(Integer.toString(i), Integer.toString(i) + "b");
map.put(Integer.toString(i), Integer.toString(i) + "c");
}
//At this point, the map has in it the following key : value pairs
//"0" : ["0a", "0b", "0c"]
//"1" : ["1a", "1b", "1c"]
//"2" : ["2a", "2b", "2c"]
//Not technically required to return the map IFF the map is instantiated outside the method
return map;
}