如何在 Map 中使用 StringBuilder
How to use StringBuilder with Map
我有以下地图
Map<String, String> map = new HashMap<>();
和dto的集合
List<MyDto> dtoCollection = new ArrayList<>();
class MyDto {
String type;
String name;
}
for(MyDto dto : dtoCollection) {
map.compute(dto.getType(), (key,value) -> value + ", from anonymous\n"());
}
问题是如何将 Map 替换为 Map 并在循环内追加?
您可以简单地将 value + ", from anonymous\n"
替换为 value == null ? new StringBuilder(dto.getName()) : value.append(", from anonymous\n"))
。
插图:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
class MyDto {
String type;
String name;
public MyDto(String type, String name) {
this.type = type;
this.name = name;
}
public String getType() {
return type;
}
public String getName() {
return name;
}
}
public class Main {
public static void main(String[] args) {
Map<String, StringBuilder> map = new HashMap<>();
List<MyDto> dtoCollection = new ArrayList<>();
for (MyDto dto : dtoCollection) {
map.compute(dto.getType(), (key, value) -> value == null ? new StringBuilder(dto.getName())
: value.append(", from anonymous\n"));
}
}
}
我是不是漏掉了什么?
Map::merge
或要映射的集合等方法需要创建额外的 StringBuilder 实例,然后将其连接:
map.merge(
dto.getType(),
new StringBuilder(dto.getName()).append(" from anonymous\n"), // redundant StringBuilder
(v1, v2) -> v1.append(v2) // merging multiple string builders
);
可以使用 computeIfAbsent
只创建一个 StringBuilder
的实例,当它在地图中丢失时,然后调用 append
到已经存在的值:
Map<String, StringBuilder> map = new HashMap<>();
List<MyDto> dtoCollection = Arrays.asList(
new MyDto("type1", "aaa"), new MyDto("type2", "bbb"),
new MyDto("type3", "ccc"), new MyDto("type1", "aa2"));
for (MyDto dto : dtoCollection) {
map.computeIfAbsent(dto.getType(), (key) -> new StringBuilder()) // create StringBuilder if needed
.append(dto.getName()).append(" from anonymous\n");
}
System.out.println(map);
输出:
{type3=ccc from anonymous
, type2=bbb from anonymous
, type1=aaa from anonymous
aa2 from anonymous
}
我有以下地图
Map<String, String> map = new HashMap<>();
和dto的集合
List<MyDto> dtoCollection = new ArrayList<>();
class MyDto {
String type;
String name;
}
for(MyDto dto : dtoCollection) {
map.compute(dto.getType(), (key,value) -> value + ", from anonymous\n"());
}
问题是如何将 Map
您可以简单地将 value + ", from anonymous\n"
替换为 value == null ? new StringBuilder(dto.getName()) : value.append(", from anonymous\n"))
。
插图:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
class MyDto {
String type;
String name;
public MyDto(String type, String name) {
this.type = type;
this.name = name;
}
public String getType() {
return type;
}
public String getName() {
return name;
}
}
public class Main {
public static void main(String[] args) {
Map<String, StringBuilder> map = new HashMap<>();
List<MyDto> dtoCollection = new ArrayList<>();
for (MyDto dto : dtoCollection) {
map.compute(dto.getType(), (key, value) -> value == null ? new StringBuilder(dto.getName())
: value.append(", from anonymous\n"));
}
}
}
我是不是漏掉了什么?
Map::merge
或要映射的集合等方法需要创建额外的 StringBuilder 实例,然后将其连接:
map.merge(
dto.getType(),
new StringBuilder(dto.getName()).append(" from anonymous\n"), // redundant StringBuilder
(v1, v2) -> v1.append(v2) // merging multiple string builders
);
可以使用 computeIfAbsent
只创建一个 StringBuilder
的实例,当它在地图中丢失时,然后调用 append
到已经存在的值:
Map<String, StringBuilder> map = new HashMap<>();
List<MyDto> dtoCollection = Arrays.asList(
new MyDto("type1", "aaa"), new MyDto("type2", "bbb"),
new MyDto("type3", "ccc"), new MyDto("type1", "aa2"));
for (MyDto dto : dtoCollection) {
map.computeIfAbsent(dto.getType(), (key) -> new StringBuilder()) // create StringBuilder if needed
.append(dto.getName()).append(" from anonymous\n");
}
System.out.println(map);
输出:
{type3=ccc from anonymous
, type2=bbb from anonymous
, type1=aaa from anonymous
aa2 from anonymous
}