我只是想知道您的意見,即將所有Collections函數輸出更改爲Iterable類型。Iterable vs Iterator作爲返回行爲(最佳實踐?)
這在我看來很可能在Java中最常用的代碼的今天,每個人都總是返回一個列表/設置/地圖中的時候99%,但shouldn't是標準的返回類似
public final Iterable<String> myMethod() {
return new Iterable<String>() {
@Override
public Iterator<String> iterator() {return myVar.getColl();}
};
}
這一切都很糟糕嗎?你知道所有的DAO類,這東西會像
Iterable<String> getName(){}
Iterable<Integer> getNums(){}
Iterable<String> getStuff(){}
代替
List<String> getName(){}
List<Integer> getNums(){}
Set<String> getStuff(){}
畢竟,次99%,你會在for循環中使用它...
你有什麼想法?
有了一套,你通常也想測試一個項目的存在 - 這需要實際的設置。 –
你是對的,這是愚蠢的放置在那裏,特別是HashSet,誰沒有秩序。 – Whimusical