我有下面的代码。我需要根据键值替换地图的值。
定义类具有以下变量:
private Map<String, String> rows;
private String tableName;
Run Code Online (Sandbox Code Playgroud)
我试过下面的代码。可以用其他更简单的方法来实现吗?
/*Definitions*/
Definition definitionObj = new Definition();
definitionObj.setTableName("TABLE1");
Map map = new HashMap<String,String>();
map.put("1","one");
map.put("2","two");
map.put("3","threee");
definitionObj.setRows(map);
Definition definitionObj1 = new Definition();
definitionObj1.setTableName("TABLE2");
Map map1 = new HashMap<String,String>();
map1.put("4","one");
map1.put("5","two");
map1.put("6","threee");
definitionObj1.setRows(map1);
String key1 = "2";
String value1 = "modifiedvalue"; /*the value that i need to set*/
List<Definition> myList = Arrays.asList(definitionObj,definitionObj1);
Definition result1 = myList.stream()
.filter(x -> "TABLE1".equals(x.getTableName()))
.findAny()
.orElse(null);
result1.getRows().replace(key1,value1);
System.out.println(result1);
Run Code Online (Sandbox Code Playgroud)