Java 如何更新映射中的一个值
我有一个map对象Java 如何更新映射中的一个值,java,map,Java,Map,我有一个map对象testMap声明为HashMap Test是一个简单的类,它包含对对象和两个字符串值的引用 即 我只想更新散列映射“testMap”中的“name”。如何更新?您需要将name属性的可见性更改为public或向其添加setter: Test test = testMap.get("key"); if (test != null) { test.name = "new name"; } public class Test { private String name
testMap
声明为HashMap
Test是一个简单的类,它包含对对象
和两个字符串
值的引用
即
我只想更新散列映射“testMap”中的“name”。如何更新?您需要将
name
属性的可见性更改为public
或向其添加setter:
Test test = testMap.get("key");
if (test != null) {
test.name = "new name";
}
public class Test {
private String name;
private String id;
private Object val;
public Test(Object val,String name, String id){
this.val =val;
this.id = id;
this.name = name;
}
public void setName(String name) {
this.name = name;
}
然后,要更改您的姓名,您需要
Test test = testMap.get("key");
if (test != null) {
test.setName("new name");
}
如果还想更新映射键,则需要
Test test = testMap.remove("oldKey");
if (test != null) {
test.setName("newKey");
test.put("newKey", test);
}
您需要将
名称
属性的可见性更改为公共
或向其添加setter:
public class Test {
private String name;
private String id;
private Object val;
public Test(Object val,String name, String id){
this.val =val;
this.id = id;
this.name = name;
}
public void setName(String name) {
this.name = name;
}
然后,要更改您的姓名,您需要
Test test = testMap.get("key");
if (test != null) {
test.setName("new name");
}
如果还想更新映射键,则需要
Test test = testMap.remove("oldKey");
if (test != null) {
test.setName("newKey");
test.put("newKey", test);
}
由于名称是一个私有字段,因此当前实现的
Test
无法实现此功能。您可以将其公开或将getter/setter方法添加到Test
类中(当前Test
类似乎完全无用,除非您没有遗漏一些代码)
之后,您可以将所需的Test
实例替换为具有必要字段的新实例,或者更新名称。代码:
public class Test {
public String name;
public String id;
public Object val;
public Test(Object val,String name.String id){
this.val =val;
this.id=id;
this.name = name;
}
}
Map<String, Test> testMap = new HashMap<String, Test();
...
Test test = testMap.get(key);
test.name = newName;
testMap.put(key, test);
// or
Test test2 = testMap.get(key);
testMap.put(key, new Test(test2.val, newName, test2.id));
公共类测试{
公共字符串名称;
公共字符串id;
公共对象val;
公共测试(对象值、字符串名称、字符串id){
this.val=val;
这个.id=id;
this.name=名称;
}
}
Map testMap=new HashMap您当前的Test
实现是不可能的,因为名称是一个私有字段。您可以将其公开或将getter/setter方法添加到Test
类中(当前Test
类似乎完全无用,除非您没有遗漏一些代码)
之后,您可以将所需的Test
实例替换为具有必要字段的新实例,或者更新名称。代码:
public class Test {
public String name;
public String id;
public Object val;
public Test(Object val,String name.String id){
this.val =val;
this.id=id;
this.name = name;
}
}
Map<String, Test> testMap = new HashMap<String, Test();
...
Test test = testMap.get(key);
test.name = newName;
testMap.put(key, test);
// or
Test test2 = testMap.get(key);
testMap.put(key, new Test(test2.val, newName, test2.id));
公共类测试{
公共字符串名称;
公共字符串id;
公共对象val;
公共测试(对象值、字符串名称、字符串id){
this.val=val;
这个.id=id;
this.name=名称;
}
}
Map testMap=new HashMap同样,要使其工作,您应该将Test
classpublic
的name
成员设置为public,而不是private
。附加说明:它将更改Test的实例,因此任何其他引用也将看到新值。此外,要使其工作,您应该将name
成员设置为Test
类public
而不是private
。附加说明:它将更改Test的实例,因此任何其他引用也将看到新值。“name”是指testMap中存储测试实例的字符串键吗?ie-如果我做了testMap.put(“myKey”,new Test(o,“str1”,“str2”);
,你是说你想更改“myKey”
值吗?通过“name”,你是指测试实例存储在testMap中的字符串键吗?ie-如果我做了testMap.put(“myKey”,new Test(o,“str1”,“str2”);
,您是说要更改“myKey”
值吗?