Everyone has different ideas and different codes. I came across a simple example in my work. I would like to share with you what is your writing method? Welcome to reply!
The title
(Regardless of caching schemes) Get the desired string from IDS and the dictionary table OilDicVo :name is a string separated by /.
parameter
List<String> ids = Lists.newArrayList("A001"."A002"."A004"."A007"); List<OilDicVo> oilDicVos = ... ;Copy the code
oilDicVos
structure
[{"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 #"}]Copy the code
Expected output string
89# /92# /98# /85#
Copy the code
Version V1.1
Version 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();
Copy the code
Version V1.1.1
Add break to skip the inner loop
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();
Copy the code
Version V1.2
Version V1.2.0
Map is applicable instead of loop.
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();
Copy the code
Version V1.2.1
Increases the initial size of the 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();
Copy the code
Version V1.2.2
Reduce the size of the 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();
Copy the code
Version V1.2.3
Loop directly using 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();
Copy the code
Version V1.3
Using lambda expressions
Version V1.3.0
Use lambda expressions to convert directly to maps
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();
Copy the code
Release V1.3.1
After using lambda expressions to filter the available results, it is converted to a 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();
Copy the code
Version V1.3.2
After using lambda expressions to filter the available results, values are directly joined after being converted to a 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))return map.values().stream().collect(Collectors.joining("/"));Copy the code
Version version 1.3.3
Use lambda for one-time conversions
List<String> ids = ... ; List<OilDicVo> oilDicVos = ... ;return oilDicVos.stream()
.filter(oilDicVo -> strings.contains(oilDicVo.getOilsDicId()))
.map(OilDicVo::getName)
.collect(Collectors.joining("/"));
Copy the code
conclusion
In the same project, these similar functions encountered, the code is probably one of the versions. Which do you prefer? Which has worse performance? Which has higher performance?