我已经使用Java 8实现了以下代码。
Map<String, String> coMap = getHashMap();
String newCoName = coMap.entrySet()
.stream()
.filter(coEntry -> coEntry.getValue().equals(newcoId))
.map(coEntry -> coEntry.getKey())
.collect(Collectors.joining());
String oldCoName = coMap.entrySet()
.stream()
.filter(coEntry -> coEntry.getValue().equals(oldcoId))
.map(coEntry -> coEntry.getKey())
.collect(Collectors.joining());
现在。我想知道这样做的任何更好的方法,而不是重复两次相同的代码行。
比重复两次相同代码的更大问题是两次执行相同的代码。
运行单个Stream
管道以产生您的输出将更有效:
Map<String,String> keysByValue =
coMap.entrySet()
.stream()
.collect(Collectors.groupingBy(Map.Entry::getValue,
Collectors.mapping(Map.Entry::getKey,
Collectors.joining())));
这将为您提供原始Map
的每个值(不仅是您的原始代码正在搜索的两个值),即具有该值的关节键。
然后您可以从Map
提取所需的数据:
String newCoName = keysByValue.get(newcoId);
String oldCoName = keysByValue.get(oldcoId);
样品输入和输出:
Map<String,String> coMap = new HashMap<> ();
coMap.put("a","foo");
coMap.put("b","foo");
coMap.put("c","bar");
coMap.put("d","bar");
Map<String,String> keysByValue = ... // same as the code above
String newValueKeys = keysByValue.get("foo");
String oldValueKeys = keysByValue.get("bar");
System.out.println (newValueKeys);
System.out.println (oldValueKeys);
输出:
ab
cd
由于整个差异是一个ID,因此简单的方法适合您。
String getName(int id) { // supposed id is an integer
return coMap.entrySet()
.stream()
.filter(coEntry -> coEntry.getValue().equals(id))
.map(coEntry -> coEntry.getKey())
.collect(Collectors.joining());
}
其他方式与功能接口Predicate
一起使用,您的条件过滤器将是动态
public static Predicate<Map.Entry> getPredicate(String col) {
return p -> p.getValue().equals(col);
}
public static String getName(HashMap<String, String> coMap, Predicate<Map.Entry> predicate) {
return coMap.entrySet()
.stream()
.filter(predicate)
.map(coEntry -> coEntry.getKey())
.collect(Collectors.joining());
}
呼叫您的代码:
getName(coMap, getPredicate(newcoId));
getName(coMap, getPredicate(oldcoId));
大多数IDE可以使用"提取方法"重构为您重构:http://refactoring.com/catalog/extractmethod.html或者您可以手工做
您可以使用此辅助方法:
public static String join(Map<String, String> map, String value) {
return map.keySet().stream()
.filter(key -> Objects.equals(map.get(key), value))
.collect(Collectors.joining());
}
在这里使用该方法的一些示例代码:
Map<String, String> coMap = new HashMap<>();
coMap.put("A", null);
coMap.put("B", "V");
coMap.put("C", "V");
coMap.put("D", "Z");
coMap.put("E", "Z");
coMap.put("F", null);
System.out.println("Vs: " + join(coMap, "V"));
System.out.println("Zs: " + join(coMap, "Z"));
和这里的输出:
Vs: BC
Zs: DE