Generating hashCode from multiple fields? [duplicate] - java

This question already has answers here:
Creating a hashCode() Method - Java
(4 answers)
Closed 9 years ago.
How do I generate a hashCode from two fields in my class?
For example, I want Pair classes with the same objects V to have the same hashCode:
public class Pair<V> {
V from, to;
}
Should I multiply their hashCodes together? Add them? Multiply them with a prime?

One way to do it is adding the hash code of the first field to hash code of the second field, multiplied by a small prime number, like this:
public int hashCode() {
return 31 * from.hashCode() + to.hashCode();
}

Related

Java HashSet remove item by its hash code [duplicate]

This question already has answers here:
Remove object from ArrayList with some Object property
(5 answers)
Remove objects from an ArrayList based on a given criteria
(10 answers)
How to remove specific object from ArrayList in Java?
(17 answers)
Closed 1 year ago.
I have a class with its own hashCode() method. I am adding this class to a HashSet. How can I remove an item by its hashCode, without knowing the object itself?
For example, if I have the following code
HashSet<Data> set = new HashSet<>();
set.add(new Data(10, 5));
...
class Data {
public int importantVal;
public int notImportantVal;
//... constructor ...
#Override
public int hashCode() {
return importantVal;
}
}
and I knew the importantVal of a Data object, but not the object itself. How would I remove it? set.remove(10) does not work.
Best solution I can think of is to also override equals() to return if importantVal is the same, and then do set.remove(new Data(10, anyPlaceholderValue))

How to Access Variable's Memory Address In Java? [duplicate]

This question already has answers here:
hashCode uniqueness
(7 answers)
What is an object's hash code if hashCode() is not overridden?
(12 answers)
Does hashcode number represent the memory address? [duplicate]
(9 answers)
How can I get the memory location of a object in java?
(4 answers)
Closed 3 years ago.
I wrote this code and want to see memory location of 2 objects that i create from one class and make instance of one specific variable.
public class StaticFields {
int a = 12;
int b = 235;
public static void main(String[] args) {
StaticFields obj = new StaticFields();
int a = obj.a;
StaticFields obj2 = new StaticFields();
int c = obj2.a;
System.out.println(System.identityHashCode(a));
System.out.println(System.identityHashCode(c));
}
}
why the "identityHashCode" of "a" and "c" is the same ?
Thanks.
Both the integers carry the same value, 12.
Since Integers are cached (for values up to 127 from -128), the hash code value of both objects returned are same.
This is not true for b since its value is greater than 127.

How to sort list of objects? [duplicate]

This question already has answers here:
How to sort a List/ArrayList?
(21 answers)
Closed 4 years ago.
I have a list of objects, each object has a method that returns distance from player to said object like that
object.distance(player)
now i need to sort that list from loqwest distance to furthest
You can use interface java.lang.Comparable in java
Example (pseudo code) assuming object.distance(player) returns an Integer value
class Distance implements Comparable<Distance>{
/**
* Compare a given Distance with this object.
*/
public int compareTo(Distance o) {
return this.distance(player).compareTo(o.distance(o.player));
}
}
now you can sort your list like
Collections.sort(YourListOfDistance)
here some reference
When should a class be Comparable and/or Comparator?

HashCode implementation for generic objects [duplicate]

This question already has answers here:
HashCode for Generic objects in Java
(3 answers)
Closed 4 years ago.
I've a Node class with this parameters:
public class Node < T > {
private T value;
private int priority;
}
I know that I need to override the hashcode method but I don't know how to do it because value is a generic object. I read that it could be done by using the address of the object, but is not recommended because the JVM can change the address of the object during the execution of the program.
You should combine value.hashCode() with the priority value in order to calculate a Node's hashCode().
For example:
#Override
public int hashCode()
{
return 31 * value.hashCode() + priority;
}
This relies on users of your Node class overriding hashCode() in the relevant classes (the classes used as types of Node value).
Note that this is the way JDK collection classes implement hashCode. For example, the default implementation of hashCode() for Sets is the sum of the hashCode()s of the Set's elements.

Java toString method difference [duplicate]

This question already has answers here:
Java int to String - Integer.toString(i) vs new Integer(i).toString()
(11 answers)
Closed 6 years ago.
I enjoy CodeFights at the moment and at the end of my last fight i found something interesting. The code in those two cases (mine and the opponent) was said to be correct. Is there a difference between this source code:
return Integer.toString(Character.getNumericValue(ch1) + Character.getNumericValue(ch2));
and this one:
return new Integer(Character.getNumericValue(ch1)+ Character.getNumericValue(ch2)).toString();
What is the key that i am missing?
From javadoc https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html
String toString()
Returns a String object representing this Integer's value.
static String toString(int i)
Returns a String object representing the specified integer.
Integer's toString method is implemented as Integer.toString(value), so the second answer merely has a redundant instantiation.
#Override
public String toString() {
return Integer.toString(value);
}

Categories