【发布时间】:2015-01-20 18:49:39
【问题描述】:
我正在重构一些代码,其中一部分看起来像这样 -
public void addElementPairsToConfig(final Config config, final Element element1, final Element element2, final Element...elements) throws Exception {
config.addDependency(element1, element2);
Element currentElement = element2;
for (int i = 0; i < elements.length; i++) {
config.addDependency(currentElement, elements[i]);
currentElement = elements[i];
}
}
我认为它应该是这样的 -
public void addElementPairsToConfig(final Config config, final Element...elements) throws Exception {
for (Pair<Element, Element> pair : elements.getNextPair()) {
config.addDependency(pair.getFirst(), pair.getLast());
}
}
我知道Apache Commons 有一个Pair 类,我将使用它,但找不到执行此操作的List util 方法,是否存在。我不介意自己写一个,但我宁愿不重新发明轮子,而是使用来自Apache Commons 或Guava 的东西,如果它存在的话。
在 ruby 世界中,我会使用 slice 之类的东西。
【问题讨论】:
标签: java collections refactoring guava apache-commons