1 Pip Equals

0 views
Skip to first unread message

Kathrine Selvage

unread,
7:00 AM (6 hours ago) 7:00 AM
to opencog

I'm sorry if this is a dumb question (especially for someone who has developed in java for over 4 years) or if it has been answered (I've searched stack overflow and despite the simplicity of the problem, I couldn't find a duplicate) but I created two priority queues and wanted to check if they contain exactly the same elements in exactly the same order. However, I was returned false when I called .equals() on two seemingly equal priority queues. I would expect this much from a direct comparison (==) but not from the .equals() method. I've looked on java docs and they don't provide an explanation for this behavior.

I have overridden equals methods before, so if this is something I must do, I can handle that, but I was just very taken aback by the results. I know I could convert my PriorityQueue to a string or an array, and then compare them, but that defeats the entire point of using a PriorityQueue (not to mention all the excess space it will take up).

1 pip equals


Download File >>> https://bytlly.com/2zO3N6



The Iterator provided in method iterator() and the Spliterator provided in method spliterator() are not guaranteed to traverse the elements of the priority queue in any particular order. If you need ordered traversal, consider using Arrays.sort(pq.toArray()).

I'm not asking about a sure-way, always right approach, but of something to aid in the development process. Currently I have to step into the equals() calls (usually a tree of them) until one of them is false, then step into it, ad nauseam.

I thought about using the object graph, outputting it to xml and comparing the two objects. However, XMLEncoder requires default constructors, jibx requires pre-compilation, x-stream and simple api are not used in my project. I don't mind copying a single class, or even a package, into my test area and using it there, but importing a whole jar for this just isn't going to happen.

Edit: I know adding jars is the normal way of doing things. I know jars are reusable units. However, the bureaucracy needed (at my project) for this doesn't justify the results - I'd keep on debugging and stepping into.

BTW, the hamcrest basic jar is 32K (including source!) giving you the option of reviewing the code and saying to your bosses "I'll stand by this as my own code" (which I presume is your import problem).

You could use aspects to patch "equal" on the classes in your object graph and make them log the object state to a file when they return false. To log the object state you could use something like beanutils to inspect the object and dump it. This is one jar-based solution which can be easily used in your workspace

If the hierarchy of the objects stored in your tree is simple enough, you could place conditional breakpoints on your classes "equal" implementation which only triggers when "equal" returns false which would limit the number of times you have to step into ... you can use this wherever you have debugger access. eclipse handles this just fine.

I know, the last bit looks mad... but the difference is that you can put a breakpoint on the "return false". If you then use breakableEquals in all your deep equality comparisons then you can break as soon as you hit the first "return false".

This doesn't help much if you're comparing lots of primitive values, admittedly... but it might be helpful. I can't say I've ever actually used this, but I can't see why it wouldn't work. It will be a bit less efficient, of course - so if you're dealing with high-performance code you may want to change it afterwards.

As for your specific problem, do you have a lot of different classes that use many different properties to determine equality, or do you just a have deeply nested object graph of essentially the same classes? In the latter case, it would be very easy to just strucutre the equals() methods so that you can put breakpoints on the "return false" statements. In the former case, this might be too much work, I suppose. But then, an XML-based comparison may not work either, since it will show differences between semantically equal objects (e.g. Sets and Maps).

Given that your project can't add a jar, it seems quite beyond a SO answer to give a whole implementation of a solution that other projects take a substantial amount of code to accomplish (and nicely include in a Jar for you).

I know adding jars is the normal way of doing things. I know jars are reusable units. However, the bureaucracy needed (at my project) for this doesn't justify the results - I'd keep on debugging and stepping into.

One way around this is to include a library like Spring (which pulls in loads of other jar) I have seen Spring project which didn't actually use any Spring jar just so they could use any jar which is bundled with it.

commons-jxpath might be useful to quickly examine the object tree. I don't fully understand the issue of including jars, but you can just use it in your own project in whatever IDE you use that presumably lets you use expressions during debugging.

A custom class loader reads the class file and instruments each method with tracing code. The class loader also adds a static field to each class. This field has two states, 'on' and 'off'. The tracing code checks this field prior to printing. The command line options access and modify this static field to control tracing output.

Part of the problem is that you don't know what equals is actually looking at. An object could have many diffrent fields and still claim it was equal to some other object, it may even be a diffrent type. (for example, a custom URL class might return true for a string that equals it's external form).

So I don't think it's possible without bytecode instrumentation where you can actually modify the classes equals() function to see what fields it accesses. Even then, it would still be extreemly difficult to 'truly' know why a function returned false. But hopefully it would be a simple issue of comparing the fields that are actually accessed in equals()

The correct way to get the hash code of an array in java and in kotlin is to use Arrays.hashCode(a). Otherwise you use the hash implementation of java.lang.Object which is based on the memory location (I think).

Not really. As I explained you calculate the hash of arrays differently so the hash values of the underlying data is the same. The problem is that the equality check is not consistent with the hash calculation for arrays. Either the hashCode implementation should use array.hashCode() instead of Arrays.hashCode(array) or the equals method should be changed to Arrays.equals(a, b).

It does not return a constant for all hashes. It just uses a hash function which is based on the elements in the array, which are in your example the same. If you change your example to use 1 instead of 0 in the array you get a different hash.

Compares two Double objects numerically. There are two ways in which comparisons performed by this method differ from those performed by the Java language numerical comparison operators () when applied to primitive double values:
Double.NaN is considered by this method to be equal to itself and greater than all other double values (including Double.POSITIVE_INFINITY).
0.0d is considered by this method to be greater than -0.0d.
This ensures that the natural ordering of Double objects imposed by this method is consistent with equals.

Almost like that. The reason why data and value classes use generic equals for any type including arrays and floating point numbers is to avoid unexpected equality behavior changes when you make your data/value class more generic. For example, if you had a data class like

@jen_lasser I tried to follow your recommendation and instead of using segment that says page contains X OR page contains Y I built segment with page contains any of X, Y. However I am getting different results. DO you know why?

OK, thanks @jen_lasser I will have a look at a free moment how to test it one by one.
Can I just clarify 1 thing once I have your attention
If I use any of operator in a visits segment for pages, it should return all the visits which included pages with any of the phrases listed in the operator.
If I use equals any of operator in a visits segment for pages, I need to list the exact pages names that I have in mind. I see when I am suing the key phrases like in 'any of' it returns 0.

Methods are available for deep, shallow, or referential equality comparison. In addition, you can opt into support for circular objects, or performing a "strict" comparison with unconventional property definition, or both. You can also customize any specific type comparison based on your application's use-cases.

Map objects support complex keys (objects, Arrays, etc.), however the spec for key lookups in Map are based on SameZeroValue. If the spec were followed for comparison, the following would always be false:

Creates a custom equality comparator that will be used on nested values in the object. Unlike deepEqual and shallowEqual, this is a factory method that receives the default options used internally, and allows you to override the defaults as needed. This is generally for extreme edge-cases, or supporting legacy environments.

Create a custom equality comparator. This allows complete control over building a bespoke equality method, in case your use-case requires a higher degree of performance, legacy environment support, or any other non-standard usage. The recipes provide examples of use in different use-cases, but if you have a specific goal in mind and would like assistance feel free to file an issue.

NOTE: Map implementations compare equality for both keys and value. When using a custom comparator and comparing equality of the keys, the iteration index is provided as both indexOrKeyA and indexOrKeyB to help use-cases where ordering of keys matters to equality.

Some recipes have been created to provide examples of use-cases for createCustomEqual. Even if not directly applicable to the problem you are solving, they can offer guidance of how to structure your solution.

4a15465005
Reply all
Reply to author
Forward
0 new messages