转载

【每日鲜蘑】不同人的不同编码风格

每个人的思路不一样,写出的代码也不一样,我在工作中就遇到了一个简单的例子,与你分享下,你的写法是哪种呢?欢迎回复!

题目

(不考虑缓存方案)根据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#
复制代码

版本V1.1

版本V1.1.0

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();
复制代码

版本V1.1.1

增加 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();
复制代码

版本V1.2

版本V1.2.0

适用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();
复制代码

版本V1.2.1

增加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();
复制代码

版本V1.2.2

减少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();
复制代码

版本V1.2.3

直接使用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();
复制代码

版本V1.3

使用lambda表达式

版本V1.3.0

使用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();
复制代码

版本V1.3.1

使用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();
复制代码

版本V1.3.2

使用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("/"));;
复制代码

版本V1.3.3

使用lambda一次性进行转换

List<String> ids = ...;
List<OilDicVo> oilDicVos = ...;
return oilDicVos.stream()
    .filter(oilDicVo -> strings.contains(oilDicVo.getOilsDicId()))
    .map(OilDicVo::getName)
    .collect(Collectors.joining("/"));
复制代码

总结

同一个项目里,遇见的这些类似的功能,代码大约都是其中某个版本。

  • 你更喜欢哪种编码?

  • 哪种编码有潜在危险?

  • 哪种编码的性能最差?

  • 哪种的性能最好?

原文  https://juejin.im/post/5efd7dbdf265da22a77c4746
正文到此结束
Loading...