Is there any way to implement equals and hashCode using Nashorn javascript?
Arseniy Kovalchuk
ars.kov at gmail.com
Wed Aug 20 15:17:38 UTC 2014
Good day.
I’ve faced with the following problem. I’d like to use java.util.HashMap
and/or java.util.PriorityQueue in Nashorn script, where I need to use
particular custom object as a key in the HashMap, and also use
HashMap.containsKey() to check if there is a key in the Map (another option
is to check if the object in a Collection.contains(Object o)).
So, obviously, I need to implement equals and hashCode in my object basing
on some field values. I'd like to highlight, that it would be very
important to not code custom Java class nor interface :). In other words,
is it possible to implement this only in JavaScript?
For example:
var PriorityQueue = java.util.PriorityQueue;var HashMap =
java.util.HashMap;var Integer = java.lang.Integer;
// Sample 1// Doesn't work, equals and hashCode are not being
invokedfunction Vertex1(from, cost) {
this.from = from;
this.cost = cost;
this.equals = function(other) { return this.from == other.from; }
this.hashCode = function() { return Integer.hashCode(this.from); }}
var hm = new HashMap();
hm.put(new Vertex1(1, 10), 10);
hm.put(new Vertex1(1, 20), 21);// Prints size is 2, but I'd like to see 1
print("HashMap size: " + hm.size());// Prints false
print("HashMap1 contains: " + hm.containsKey(new Vertex1(1, 20)));
Another way when methods are being invoked, but there is no obvious way to
access `other` object's `from` field in the equals method!
var HashMap = java.util.HashMap;var JInteger = java.lang.Integer;var
JObject = Java.extend(java.lang.Object);
var createVertex = (function() {
var
_equals = function(other) {
print(this + ' vs ' + other);
return this._from === other.from;
};
_hashCode = function() {
var hashCode = JInteger.hashCode(this._from);
print(hashCode);
return hashCode;
};
return function(from, cost) {
return new JObject() {
_from : from,
_cost : cost,
equals : _equals,
hashCode : _hashCode,
}
}})();
var JSVertex = function(from, cost) {
return new JObject() {
_from : from,
_cost : cost,
equals : function(other) {
print(this + ' vs ' + other);
return this._from === other._from;
},
hashCode : function() {
var hashCode = JInteger.hashCode(this._from);
print(hashCode);
return hashCode;
}
}}
var v1 = JSVertex(1, 10);var v2 = JSVertex(1, 20);//var v1 =
createVertex(1, 10);//var v2 = createVertex(1, 20);var v3 =
createVertex(1, 20);
print(v1.class === v2.class); // returns true
print(v2.class === v3.class); // returns truevar hm = new HashMap();
hm.put(v1, 10);
hm.put(v2, 21);
print("HashMap size: " + hm.size()); // Prints 2, but I'd like to see 1
print("HashMap contains: " + hm.containsKey(v3)); // Prints false
Please refer to the stackoverflow question for details
http://stackoverflow.com/questions/25379340/nashorn-java-collections-how-to-implement-equals-and-hashcode-in-pure-javascri
Sincerely,
Arseny Kovalchuk.
--
С Уважением,
Арсений Ковальчук
*ars.kov at gmail.com <ars.kov at gmail.com>*
*+375 29 666-16-16*
More information about the nashorn-dev
mailing list