假設你必須裝滿狗......我們想知道一個月後狗失去了多少重量。所以一些代碼在這裏:如何以優雅的方式在Java中找到兩個相等的對象? (循環套裝)
Set<Dog> dogsJanuary = getDogsInJanuaryFromDB();
Set<Dog> dogsFebruary = getDogsInFebruaryFromDB();
現在我需要遍歷所有的狗對象,看看狗是否已經減肥。我是這樣做的:
for(Dog d:dogsJanuary)
for(Dog g:dogsFebruary)
if(d.equals(g)) // Assume dog name is unique and equals check if name.equals(name)
boolean dogLostWeight = compareDogs(d,g)
有沒有更好的辦法?
爲什麼downvoted,downvoter? –