インスタンスの等価性が含まれるリストの「弱い」等価性から派生するクラスに equals メソッドを実装したい、つまり、リスト要素の同じ順序は必要ありませんが、java.util.List.equals(Object)
(以下の javadoc を参照できます) 要求同じ順序。
では、リストに対して順序に依存しない等価性チェックを実行する最良の方法は何ですか?
リストを新しいリストにラップし、それらをソートしてから、そこで equals を実行することを考えました。
または別のアプローチ(この質問は時代遅れになります):代わりにTreeSetを使用してください。この方法では、要素の順序は等しい要素を持つセットで常に同じになります。
/**
* Compares the specified object with this list for equality. Returns
* <tt>true</tt> if and only if the specified object is also a list, both
* lists have the same size, and all corresponding pairs of elements in
* the two lists are <i>equal</i>. (Two elements <tt>e1</tt> and
* <tt>e2</tt> are <i>equal</i> if <tt>(e1==null ? e2==null :
* e1.equals(e2))</tt>.) In other words, two lists are defined to be
* equal if they contain the same elements in the same order. This
* definition ensures that the equals method works properly across
* different implementations of the <tt>List</tt> interface.
*
* @param o the object to be compared for equality with this list
* @return <tt>true</tt> if the specified object is equal to this list
*/
boolean equals(Object o);
私は答えを知っていて、タブを閉じました。その後、この種の状況で何をすべきかについてのメタの投稿を読みました。しかし、私の質問は SO によってキャッシュされたので、とにかく投稿します。たぶん、誰かが将来同じ「問題」を抱えているかもしれません。誰も投稿しない場合は、回答を投稿します。