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

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

题目

(不考虑缓存方案)根据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

本站部分文章源于互联网,本着传播知识、有益学习和研究的目的进行的转载,为网友免费提供。如有著作权人或出版方提出异议,本站将立即删除。如果您对文章转载有任何疑问请告之我们,以便我们及时纠正。

PS:推荐一个微信公众号: askHarries 或者qq群:474807195,里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多

转载请注明原文出处:Harries Blog™ » 【每日鲜蘑】不同人的不同编码风格

赞 (0)
分享到:更多 ()

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址