每个人的思路不一样,写出的代码也不一样,我在工作中就遇到了一个简单的例子,与你分享下,你的写法是哪种呢?欢迎回复!
(不考虑缓存方案)根据ids和字典表OilDicVo获取期望的字符串: name
用 /
间隔的字符串。
List<String> ids = Lists.newArrayList("A001", "A002", "A004", "A007");
List<OilDicVo> oilDicVos = ...;
复制代码
oilDicVos
结构
[
{
"oilsDicId": "A001",
"name": "89#"
},
{
"oilsDicId": "A002",
"name": "92#"
},
{
"oilsDicId": "A003",
"name": "95#"
},
{
"oilsDicId": "A004",
"name": "98#"
},
{
"oilsDicId": "A005",
"name": "87#"
},
{
"oilsDicId": "A006",
"name": "86#"
},
{
"oilsDicId": "A007",
"name": "85#"
},
{
"oilsDicId": "A008",
"name": "84#"
}
]
复制代码
89#/92#/98#/85#
复制代码
List<String> ids = ...;
List<OilDicVo> oilDicVos = ...;
StringBuilder stringBuilder = new StringBuilder();
for (String id : ids) {
for (OilDicVo oilDicVo: oilDicVos) {
if (ids.contains(oilDicVo.getOilsDicId())) {
stringBuilder.append("/").append(oilDicVo.getName());
}
}
}
return stringBuilder.toString();
复制代码
增加 break
,跳过内层循环
List<String> ids = ...;
List<OilDicVo> oilDicVos = ...;
StringBuilder stringBuilder = new StringBuilder();
for (String id : ids) {
for (OilDicVo oilDicVo: oilDicVos) {
if (ids.contains(oilDicVo.getOilsDicId())) {
stringBuilder.append("/").append(oilDicVo.getName());
break;
}
}
}
return stringBuilder.toString();
复制代码
适用Map代替循环.
List<String> ids = ...;
List<OilDicVo> oilDicVos = ...;
Map<String,String> map = new HashMap<>();
for (OilDicVo oilDicVo: oilDicVos) {
map.put(oilDicVo.getOilsDicId(), oilDicVo.getName());
}
StringBuilder stringBuilder = new StringBuilder();
for (String id : ids) {
stringBuilder.append("/").append(map.get(id));
}
return stringBuilder.toString();
复制代码
增加Map的初始大小
List<String> ids = ...;
List<OilDicVo> oilDicVos = ...;
Map<String,String> map = new HashMap<>(oilDicVos.size());
for (OilDicVo oilDicVo: oilDicVos) {
map.put(oilDicVo.getOilsDicId(), oilDicVo.getName());
}
StringBuilder stringBuilder = new StringBuilder();
for (String id : ids) {
stringBuilder.append("/").append(map.get(id));
}
return stringBuilder.toString();
复制代码
减少Map的大小
List<String> ids = ...;
List<OilDicVo> oilDicVos = ...;
Map<String,String> map = new HashMap<>(ids.size());
for (OilDicVo oilDicVo: oilDicVos) {
if (ids.contains(oilDicVo.getOilsDicId())) {
map.put(oilDicVo.getOilsDicId(), oilDicVo.getName());
}
}
StringBuilder stringBuilder = new StringBuilder();
for (String id : ids) {
stringBuilder.append("/").append(map.get(id));
}
return stringBuilder.toString();
复制代码
直接使用Map的Values进行循环.
List<String> ids = ...;
List<OilDicVo> oilDicVos = ...;
Map<String,String> map = new HashMap<>(ids.size());
for (OilDicVo oilDicVo: oilDicVos) {
if (ids.contains(oilDicVo.getOilsDicId())) {
map.put(oilDicVo.getOilsDicId(), oilDicVo.getName());
}
}
StringBuilder stringBuilder = new StringBuilder();
for (String name : map.values()) {
stringBuilder.append("/").append(name);
}
return stringBuilder.toString();
复制代码
使用lambda表达式
使用lambda表达式直接转换成Map
List<String> ids = ...;
List<OilDicVo> oilDicVos = ...;
Map<String,String> map = oilDicVos.stream()
.collect(Collectors.toMap(OilDicVo::getOilsDicId, OilDicVo::getName))
StringBuilder stringBuilder = new StringBuilder();
for (String id : ids) {
stringBuilder.append("/").append(map.get(id));
}
return stringBuilder.toString();
复制代码
使用lambda表达式筛选可用结果后,转换成Map
List<String> ids = ...;
List<OilDicVo> oilDicVos = ...;
Map<String,String> map = oilDicVos.stream()
.filter(oilDicVo -> strings.contains(oilDicVo.getOilsDicId()))
.collect(Collectors.toMap(OilDicVo::getOilsDicId, OilDicVo::getName))
StringBuilder stringBuilder = new StringBuilder();
for (String name : map.values()) {
stringBuilder.append("/").append(name);
}
return stringBuilder.toString();
复制代码
使用lambda表达式筛选可用结果后,转换成Map后,直接对value值进行join处理
List<String> ids = ...;
List<OilDicVo> oilDicVos = ...;
Map<String,String> map = oilDicVos.stream()
.filter(oilDicVo -> strings.contains(oilDicVo.getOilsDicId()))
.collect(Collectors.toMap(OilDicVo::getOilsDicId, OilDicVo::getName))
return map.values().stream().collect(Collectors.joining("/"));;
复制代码
使用lambda一次性进行转换
List<String> ids = ...;
List<OilDicVo> oilDicVos = ...;
return oilDicVos.stream()
.filter(oilDicVo -> strings.contains(oilDicVo.getOilsDicId()))
.map(OilDicVo::getName)
.collect(Collectors.joining("/"));
复制代码
同一个项目里,遇见的这些类似的功能,代码大约都是其中某个版本。
你更喜欢哪种编码?
哪种编码有潜在危险?
哪种编码的性能最差?
哪种的性能最好?