mapping.put("a_name", "name");
时间: 2024-06-17 07:02:00 浏览: 0
`mapping.put("a_name", "name");` 是Java中Map接口的一个常用操作,它用于向映射(Map)中添加或更新键值对。在这个例子中,`mapping` 是一个Map对象,`"a_name"` 是键(key),`"name"` 是值(value)。这个语句的作用是在`mapping`中将键"a_name"关联到值"name",如果该键已经存在,则会更新其对应的值。
具体操作如下:
1. 初始化一个Map对象,例如 `HashMap<String, String> mapping = new HashMap<>();`。
2. 使用 `put` 方法添加键值对,如上所示 `mapping.put("a_name", "name");`。
3. 如果键"a_name"不存在,Map会自动添加;如果已存在,旧值"name"会被新值"new_name"替换。
相关问题
The mapping definition cannot be nested under a type [_doc] unless include_type_name is set to true.
This error message typically occurs when trying to create a mapping for Elasticsearch 7.x or later, which requires the use of the `_doc` type instead of explicit types like `tweet` or `user`. To resolve this error, you can either set `include_type_name` to `true` in your mapping definition, or remove any references to explicit types and use `_doc` as the default type.
For example, if you were previously using a mapping like this:
```
PUT my_index
{
"mappings": {
"tweet": {
"properties": {
"message": {
"type": "text"
}
}
}
}
}
```
You would need to update it to use `_doc` instead:
```
PUT my_index
{
"mappings": {
"_doc": {
"properties": {
"message": {
"type": "text"
}
}
}
}
}
```
Alternatively, you could add `include_type_name: true` to your mapping definition:
```
PUT my_index
{
"mappings": {
"include_type_name": true,
"tweet": {
"properties": {
"message": {
"type": "text"
}
}
}
}
}
```
优化这段代码 vMap.put("tags", v.getTags().stream().map(TagDTO::mapping).collect(Collectors.toList())); v.getTags().forEach(t -> { if (t.getId() == 2 || t.getId() == 208) { vMap.put("earTag", t.getName()); } else if (t.getId() == 5) { vMap.put("iflytekTag", t.getName()); } }); if (valueByVersionMap.containsKey(v.getId())) { final Map<Integer, ValueEntity> valueMap = valueByVersionMap.get(v.getId()).stream() .collect(Collectors.toMap(ValueEntity::getPropertyId, value -> value)); valueMap.keySet().forEach(p -> { if (this.propertyMap.containsKey(p)) { final String value = this.getValue(valueMap.get(p).getVal(), this.propertyMap.get(p).getCategory()); if (StringUtils.isNotBlank(value)) { vMap.put(String.valueOf(p), value); } } if (p.equals(11)) { final String value = this.getValue(valueMap.get(p).getVal(), this.propertyMap.get(p).getCategory()); if (StringUtils.isNotBlank(value)) { vMap.put("license", value); } } }); }
这是给定的代码段:
```java
vMap.put("tags",
v.getTags().stream().map(TagDTO::mapping).collect(Collectors.toList()));
v.getTags().forEach(t -> {
if (t.getId() == 2 || t.getId() == 208) {
vMap.put("earTag", t.getName());
} else if (t.getId() == 5) {
vMap.put("iflytekTag", t.getName());
}
});
if (valueByVersionMap.containsKey(v.getId())) {
final Map<Integer, ValueEntity> valueMap = valueByVersionMap.get(v.getId()).stream()
.collect(Collectors.toMap(ValueEntity::getPropertyId, value -> value));
valueMap.keySet().forEach(p -> {
if (this.propertyMap.containsKey(p)) {
final String value = this.getValue(valueMap.get(p).getVal(),
this.propertyMap.get(p).getCategory());
if (StringUtils.isNotBlank(value)) {
vMap.put(String.valueOf(p), value);
}
}
if (p.equals(11)) {
final String value = this.getValue(valueMap.get(p).getVal(),
this.propertyMap.get(p).getCategory());
if (StringUtils.isNotBlank(value)) {
vMap.put("license", value);
}
}
});
}
```
为了优化这段代码,我们可以进行以下几个步骤:
1. 使用方法引用来简化 `v.getTags().stream().map(TagDTO::mapping)` 为 `v.getTags().stream().map(TagDTO::mapping)`。
2. 使用 `forEach` 循环替代多个 `if` 语句来处理 `tags`。
3. 将 `valueByVersionMap` 的值缓存到一个变量中,以提高性能。
4. 将 `valueMap.keySet().forEach` 替换为 `valueMap.forEach`,以简化代码。
以下是优化后的代码:
```java
vMap.put("tags",
v.getTags().stream().map(TagDTO::mapping).collect(Collectors.toList()));
v.getTags().forEach(t -> {
int id = t.getId();
String name = t.getName();
if (id == 2 || id == 208) {
vMap.put("earTag", name);
} else if (id == 5) {
vMap.put("iflytekTag", name);
}
});
if (valueByVersionMap.containsKey(v.getId())) {
Map<Integer, ValueEntity> valueMap = valueByVersionMap.get(v.getId()).stream()
.collect(Collectors.toMap(ValueEntity::getPropertyId, value -> value));
valueMap.forEach((p, value) -> {
if (propertyMap.containsKey(p)) {
String propertyCategory = propertyMap.get(p).getCategory();
String propertyValue = getValue(value.getVal(), propertyCategory);
if (StringUtils.isNotBlank(propertyValue)) {
vMap.put(String.valueOf(p), propertyValue);
}
}
if (p.equals(11)) {
String licenseValue = getValue(value.getVal(), propertyMap.get(p).getCategory());
if (StringUtils.isNotBlank(licenseValue)) {
vMap.put("license", licenseValue);
}
}
});
}
```
这样,代码就更简洁、可读性更高,并且在一些地方进行了性能优化。请注意,这只是一个基本的优化示例,具体的优化策略可能会因上下文而异。
相关推荐
![.zip](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)