java - equals on objects that contain different types of collections -


I have an object that looks like this (I am just using the equivalent of just my equals and hashod Method):

  Public class micro {private collection & lt; Integer & gt; Integers; Private collection & lt; String & gt; Wire; // Gates and Sets ... @ Override Public Boolean Parbar (object o) {if (o == this) {returns correct; } If (o == blank = getClass ()! = O.getClass ()) {return false; } MyClass = (MyClass) o; Return objects. Equal (integer, that.integers) & amp; Amp; Objects.equal (strings, strings); } @ Override Public Ent Hashod () {Objects Returning Objects (Strings); }}  

The issue I am running is a code that essentially does this (this would be a normal test case):

  / / Product is a new example of MyClass that the user ArrayLists MyClass expects = new MyClass (); Expected.Set Integer (Arrays.asList (1, 2, 3)); Expected.Setting (Arrays.asList ("a", "b")); // I do not normally create the actual object, // but I have included the code so that you understand what's happening // set a new example of MyClass using HighSets < Integer & gt; SomeNumbers = new hashset & lt; Integer & gt; (); SomeNumbers.add (1); SomeNumbers.add (2); SomeNumbers.add (3); Set up & lt; String & gt; Some strings = new hashets & lt; String & gt; (); SomeStrings.add ("A"); SomeStrings.add ("b"); MyClass real = new MyClass (); Actual.setIntegers (someNumbers); Actual.setIntegers (someStrings); Emphasis (expected, real);  

The problem I am having is that even though the contents of the collection are the same and that the compilation time is "collection" of those members, the run-time type evaluation of equality Is used so that this claim is unsuccessful. Looking at the source code for AbstractSet.equals and AbstractList.equals, they check to see if there is another object set or a list in order before evaluating the content.

I think that something makes sense because the order matters in a list and is not in one set, so, if the content is the same, then you can not compare them.

> In that case, I do not care what the underlying collection is - I just want to know that the content is the same and there is no difference in ordering. Is there any easy way to do this?

In this case the built-in archive does not make any difference to you, you switch to a pre-defined command store Can allow for iterations, make sure that two new collections are sorted, and then check the similarities.

An easy way to do this is to arrange your elements using arrays, and check the equality by using


Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -