【发布时间】:2013-08-07 17:41:17
【问题描述】:
我需要创建一个链表并用容器中的“容器”和“元素”填充它。
这是我的代码:
public List<WebElement> getExpectedElements(){
List<WebElement> list = new LinkedList<WebElement>(Arrays.asList(
inetConnection,
wiredConnection,
phonesConnection,
usbConnection,
wifiConnection
));
list.addAll(inetConnection.getExpectedElements());
list.addAll(wiredConnection.getExpectedElements());
list.addAll(phonesConnection.getExpectedElements());
list.addAll(usbConnection.getExpectedElements());
list.addAll(wifiConnection.getExpectedElements());
return list;
}
在 java 中有什么方法可以让它变得更好(更简洁、DRY 等)?
【问题讨论】:
-
你能把列表传递给填充它的对象吗?
-
“更好”是什么意思?
-
@SotiriosDelimanolis 这与该对象的职责不符...至少在我的对象模型中...
-
@yashaka 您只需要更改
List返回的List的初始化方式,例如,inetConnection.getExpectedElements()。 -
@RobertHarvey 我的意思是更短,更简洁,更干......下面的python代码至少会更短:) ........
list = [[con] + con.getExpectedElements for con in [inetCon, wiredCon, phonesCon, usbCon, wifiCon]]; return reduce(lambda x,y: x+y, list) #or: return sum(list, [])...... ...也许这并不完美,但比主题中的java代码更好:)
标签: java list collections linked-list