1
lambdaj has the selectUnique method that throws an exception if there is more than one item satisfying the condition expressed by the given hamcrest Matcher. Since you don't have any particular condition to be matched, you need a Matcher that always returns true (it doesn't seem to me that hamcrest provides such a Matcher out of the box, but it is trivial to implement it), or maybe you would like to check that the (only) object in the list is at least not null, so you could achieve this result with:
lambdaj有selectUnique方法,如果有多個項滿足給定的hamcrest Matcher表示的條件,則拋出異常。由於您沒有任何特定條件需要匹配,因此您需要一個始終返回true的匹配器(在我看來,Hamcrest不提供開箱即用的Matcher,但實現它是微不足道的),或者也許您想檢查列表中的(唯一)對象是否至少不為null,因此您可以通過以下方式實現此結果:
selectUnique(list, Matchers.notNullValue());