Java - HashMap cannot use new object as search key - java

What I am trying to do here is to fill a HashMap with objects Obj using the key Key and when I finish I want to have access to those values according to any any of the possible keys. I have written the following code and what happened is that although the first show really shows the values I want, the second one raises a NullPointerException.
import java.util.*;
public class My{
public static void main(String[] args){
Map<Key,Obj> myMap = new HashMap<Key,Obj>();
Obj ob1 = new Obj("Nick",19);
Obj ob2 = new Obj("George",17);
Key key1 = new Key(1,2);
Key key2 = new Key(2,1);
myMap.put(key1,ob1);
myMap.put(key2,ob2);
myMap.get(key1).show();
myMap.get(new Key(1,2)).show();
}
I can tell that somehow Java cannot tell that new Key(1,2) is equal with key1, but I cannot think of how can I overcome this issue.
public class Obj{
private String name;
private int age;
Obj(String name, int age){
this.name = name;
this.age = age;
}
public void show(){
System.out.println(name + " " + age);
}
}
These are the classes I use
import java.* ;
public class Key{
public int x,y;
Key(int x, int y){
this.x = x;
this.y = y;
}
public boolean equals(Key d){
if ((this.x == d.x)&&(this.y == d.y)){
return true;
}
else{
return false;
}
}
}

You have two problems in your implementation of Key. First, the equals' method signature is wrong - it should be public boolean equals(Object). This error would have been easily noticeable if you had used the #Override annotation. Second, you should also override the hashCode() method:
#Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) {
return false;
}
Key key = (Key) o;
return x == key.x && y == key.y;
}
#Override
public int hashCode() {
return Objects.hash(x, y);
}

Related

java: issue while access value from TreeMap in multithreading

I have use TreeMap to store key value.
For key using custom object.
But once I have faced very strange issue, I am not able to get value which I have set earlier(with same key).
below is my code
public final class TestOptions implements Cloneable {
private Map<StorageSystemOptionKey, Object> options = new TreeMap<StorageSystemOptionKey, Object>();
private static final class StorageSystemOptionKey implements Comparable<StorageSystemOptionKey> {
/** Constant used to create hashcode */
private static final int HASH = 31;
private final Class<? extends StorageRepository> storageRepositoryClass;
/** The option name */
private final String name;
private StorageSystemOptionKey(Class<? extends StorageRepository> storageRepositoryClass, String name) {
this.storageRepositoryClass = storageRepositoryClass;
this.name = name;
}
public int compareTo(StorageSystemOptionKey o) {
int ret = storageRepositoryClass.getName().compareTo(o.storageRepositoryClass.getName());
if (ret != 0) {
return ret;
}
return name.compareTo(o.name);
}
#Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final StorageSystemOptionKey that = (StorageSystemOptionKey) o;
if (!storageRepositoryClass.equals(that.storageRepositoryClass)) {
return false;
}
if (!name.equals(that.name)) {
return false;
}
return true;
}
#Override
public int hashCode() {
int result;
result = storageRepositoryClass.hashCode();
result = HASH * result + name.hashCode();
return result;
}
}
void setOption(Class<? extends StorageRepository> fileSystemClass, String name, Object value) {
options.put(new StorageSystemOptionKey(fileSystemClass, name), value);
}
Object getOption(Class<? extends StorageRepository> fileSystemClass, String name) {
StorageSystemOptionKey key = new StorageSystemOptionKey(fileSystemClass, name);
return options.get(key);
}
boolean hasOption(Class<? extends StorageRepository> fileSystemClass, String name) {
StorageSystemOptionKey key = new StorageSystemOptionKey(fileSystemClass, name);
return options.containsKey(key);
}
public int compareTo(TestOptions other) {
if (this == other) {
return 0;
}
int propsSz = options == null ? 0 : options.size();
int propsFkSz = other.options == null ? 0 : other.options.size();
if (propsSz < propsFkSz) {
return -1;
}
if (propsSz > propsFkSz) {
return 1;
}
if (propsSz == 0) {
return 0;
}
int hash = options.hashCode();
int hashFk = other.options.hashCode();
if (hash < hashFk) {
return -1;
}
if (hash > hashFk) {
return 1;
}
return 0;
}
#Override
public Object clone() {
TestOptions clone = new TestOptions();
clone.options = new TreeMap<StorageSystemOptionKey, Object>(options);
return clone;
}
}
calling method to set and get like
public abstract Class<? extends StorageRepository> getStorageRepositoryClass();
public Class<? extends StorageRepository> getStorageRepositoryClass() {
return MyImpl.class;
}
TestOptions opt =new TestOptions(); // shared accross all Threads
Object getProperty(String name) {
return opt.getOption(getStorageRepositoryClass(), name);
}
void setProperty(String name, Object value) {
opt.setOption(getStorageRepositoryClass(), name, value);
}
Using set and get method in multi-threaded application.
queries:
I am calling set/get in multiple time then also I was not able to get value which was set earlier(same key)
Is this due to Treeset implementation is not synchronized
or problem with hashCode, equals or compareTo method implementation?
On a quick glance your compareTo(), equals() and hashCode() look fine. Note that TreeMap will mostly use compareTo() to find elements so that method needs to be correct (your's looks technically correct).
However, TreeMap and TreeSet (as well as other basic collections and maps) are not thread-safe and thus concurrent modifications can cause all kinds of unexpected behavior. We once had a case where 2 threads were trying to add a single element to a hashmap and the threads ended up in an endless loop because the internal list to resolve clashes produced a cycle (due to the concurrent put).
So either use the ConcurrentXxxx maps and collections or synchronize access to yours.
TreeSet is not synchronized. I belive ConcurrentSkipListMap might be better.
Check also your hashCode, equals implementation

hashCode changes each time the object is created [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
There is something that escapes me about hashCode. I know that if i override
equals, then i must override hashCode too. I also know that if two objects are
equal, then the hashCode of the two objects must be equal. If the fields that are checked in the equals method don't change, than the hashCode should not change, right?
If so, then i can't understand why each time i create an instance of the object below, i get a different hashCode:
public class Effect {
private long timestamp;
private int damage;
private SquareType squareType;
public Effect(long timestamp, int damage, SquareType squareType) {
this.timestamp = timestamp;
this.damage = damage;
this.squareType = squareType;
}
public long getTimestamp() {
return timestamp;
}
public int getDamage() {
return damage;
}
public SquareType getSquareType() {
return squareType;
}
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Effect effect = (Effect) o;
if (getDamage() != effect.getDamage()) return false;
return getSquareType() == effect.getSquareType();
}
#Override
public int hashCode() {
int result = getDamage();
result = 31 * result + (getSquareType() != null ? getSquareType().hashCode() : 0);
return result;
}
#Override
public String toString() {
String ret = "Effect hashcode: " + hashCode();
return ret;
}
}
In the code i create this kind of objects continuously over time. The only field that changes each time is "timestamp", but the two other fields don't change (unless there is a specific event). What happens is that the hashCode value is always different, even if "damage" and "SquareType" are the same.
I don't use "timestamp" in my equals and hashCode, so i can't understand why i get this behavior.
Update
This is SquareType:
public enum SquareType {
FIRE, WIND, WATER, EARTH
}
Update 2
For example, if i create 10 instances of Effect, i iterate over them and i
print them (toString() returns the hashCode value) i get 10 different values.
If two instances of Effect have the same "damage" and "SquareType" then they must be equal and have the same hashCode.
Update 3
The effects are created like this:
#Override
public void friendlyFire(BaseBullet bullet, BaseSquare square) {
square.notifyFriendlyFire(new Effect(TimeUtils.millis(),
square.getDamage(), square.getSquareType()), new MyKey(square.getUniqueID()));
}
The only Effect's field that changes is the timestamp, and i don't use it in equals and hashCode.
public void notifyFriendlyFire(Effect newEffect, MyKey key) {
// System.out.println("The map contains the key? " + effectMap.containsKey(key));
if(effectMap.containsKey(key)) {
Effect oldEffect = effectMap.get(key);
System.out.println(newEffect);
if(!oldEffect.equals(newEffect)) {
System.out.println("old effect changed!");
// remove the old effect
removeEffect(oldEffect);
// update the map with the new effect
effectMap.put(key, newEffect); //
// apply the new effect
applyEffect(newEffect);
}
}
else {
// new effect
effectMap.put(key, newEffect);
applyEffect(newEffect);
}
}
The check "if(!oldEffect.equals(newEffect))" is always true, even if damage and type are the same.
Update 4
I've found the bug. Damage increases continuously. Now i've just to figure out why...
Here's my attempt at approximating your implementation:
package cruft;
import java.util.Date;
/**
* Equals and hashCode test
* Creation date 1/16/2016.
* #link https://stackoverflow.com/questions/34826585/hashcode-changes-each-time-the-object-is-created
*/
public class OverrideDemo {
private long timestamp;
private int damage;
private SquareType squareType;
public OverrideDemo(int damage, SquareType squareType) {
this(damage, squareType, new Date().getTime());
}
public OverrideDemo(int damage, SquareType squareType, long timestamp) {
if (squareType == null) throw new IllegalArgumentException("square type cannot be null");
this.timestamp = timestamp;
this.damage = damage;
this.squareType = squareType;
}
public long getTimestamp() {
return timestamp;
}
public int getDamage() {
return damage;
}
public SquareType getSquareType() {
return squareType;
}
#Override
public boolean equals(Object o) {
if (this == o) { return true; }
if (o == null || getClass() != o.getClass()) { return false; }
OverrideDemo that = (OverrideDemo) o;
if (damage != that.damage) { return false; }
return squareType == that.squareType;
}
#Override
public int hashCode() {
int result = damage;
result = 31 * result + squareType.hashCode();
return result;
}
#Override
public String toString() {
return "OverrideDemo{" +
"timestamp=" + timestamp +
", damage=" + damage +
", squareType=" + squareType +
'}';
}
}
enum SquareType { FIRE, WIND, WATER, EARTH }
Here's a Junit test that shows how the methods behave. All tests pass; I think this implementation is correct.
package cruft;
import org.junit.Assert;
import org.junit.Test;
/**
* Junit test demonstrates testing equals and hashcode contract
* Created by Michael
* Creation date 1/16/2016.
* #link https://stackoverflow.com/questions/34826585/hashcode-changes-each-time-the-object-is-created
*/
public class OverrideDemoTest {
#Test
public void testEquals_Null() {
OverrideDemo x = new OverrideDemo(5, SquareType.EARTH);
Assert.assertFalse(x.equals(null));
}
#Test
public void testEquals_Reflexive() {
OverrideDemo x = new OverrideDemo(5, SquareType.EARTH);
Assert.assertTrue(x.equals(x));
}
#Test
public void testEquals_Symmetric() {
OverrideDemo x = new OverrideDemo(5, SquareType.EARTH);
OverrideDemo y = new OverrideDemo(5, SquareType.EARTH);
Assert.assertTrue(x.equals(y));
Assert.assertTrue(y.equals(x));
Assert.assertTrue(x.hashCode() == y.hashCode());
}
#Test
public void testEquals_Transitive() {
OverrideDemo x = new OverrideDemo(5, SquareType.EARTH);
OverrideDemo y = new OverrideDemo(5, SquareType.EARTH);
OverrideDemo z = new OverrideDemo(5, SquareType.EARTH);
Assert.assertTrue(x.equals(y));
Assert.assertTrue(y.equals(z));
Assert.assertTrue(z.equals(x));
Assert.assertTrue(x.hashCode() == y.hashCode());
Assert.assertTrue(y.hashCode() == z.hashCode());
Assert.assertTrue(z.hashCode() == x.hashCode());
}
#Test
public void testEquals_DifferentDamage_NotEqual() {
OverrideDemo x = new OverrideDemo(5, SquareType.EARTH);
OverrideDemo y = new OverrideDemo(10, SquareType.EARTH);
Assert.assertFalse(x.equals(y));
Assert.assertFalse(y.equals(x));
Assert.assertFalse(x.hashCode() == y.hashCode());
}
#Test
public void testEquals_DifferentSquareType_NotEqual() {
OverrideDemo x = new OverrideDemo(10, SquareType.EARTH);
OverrideDemo y = new OverrideDemo(10, SquareType.FIRE);
Assert.assertFalse(x.equals(y));
Assert.assertFalse(y.equals(x));
Assert.assertFalse(x.hashCode() == y.hashCode());
}
}

Java: HashSet multiple types

I have a program that I have to use a HashSet for. My question arises from the fact that HashSets mainly contain one object, but if I wish to send information to the other class, it takes three objects: one string, one int, and one boolean.
The assignment says that I must use a HashSet
Constructor I am trying to send information to:
public Magic (String name, int size, boolean isVisible)
I have a class that is supposed to be sending sets of spells containing name, size, and isVisible.
Magic.go() class:
public void go()
{
int i = 0;
while (i < size) {
if (isVisible == true) {
System.out.println(name + "!");
}
i++;
}
}
Just create an object which contains all the three fields like this:
import java.util.Objects;
public class NameSizeVisible {
private final String name;
private final int size;
private final boolean isVisible;
public NameSizeVisible(String name, int size, boolean isVisible) {
this.name = name;
this.size = size;
this.isVisible = isVisible;
}
public String getName() {
return name;
}
public int getSize() {
return size;
}
public boolean isVisible() {
return isVisible;
}
#Override
public int hashCode() {
return Objects.hash(name,size,isVisible);
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
NameSizeVisible other = (NameSizeVisible) obj;
if (isVisible != other.isVisible)
return false;
if (!Objects.equals(name, other.name))
return false;
if (size != other.size)
return false;
return true;
}
}
You can use a HashSet that stores Objects. So you would have:
HashSet<Object> set = new HashSet<>();
set.add(name);
set.add(size);
set.add(isVisible);
Then when you access the objects you just need to cast them to their respective types:
String name = "";
int size = 0;
boolean isVisible = false;
for (Object o : set) {
if (o instanceof String) {
name = (String) o;
} else if (o instanceof int) {
size = (int) o;
} else {
isVisible = (boolean) o;
}
}

sorting collection wrt primitive value

suppose, I have a student class with roll number and name. I want to sort it out wrt roll number. I tried the following .Here is my code:
package CollectionDemo;
import java.util.*;
class student1 implements Comparable<student1>{
int rollNo;
String name;
student1(int rollNo,String name){
this.rollNo=rollNo;
this.name=name;
}
#Override
public boolean equals(Object o){
if((o instanceof student1) && (((student1)o).rollNo == rollNo)){
return true;
}
else
{
return false;
}
}
#Override
public int hashCode(){
return 1;
}
public int compareTo(student1 s) {
return s.rollNo;
}
public String toString(){
return "["+rollNo+","+name+"]";
}
}
public class treeSetDemo {
public static void main(String... a){
Set<student1> set=new TreeSet<student1>();
set.add(new student1(102,"Anu"));
set.add(new student1(101,"Tanu"));
set.add(new student1(103,"Minu"));
System.out.println("elements:"+set);
}
}
o/p: elements:[[102,Anu], [101,Tanu], [103,Minu]]
so, its not sorting:( how to make it correct .
thanks for your help.
================================================
thanks for all your help. The following code runs fine, but now I want to know how it works, if i comment out equals and hashcode method.
package CollectionDemo;
import java.util.*;
class student1 implements Comparable<student1>{
int rollNo;
String name;
student1(int rollNo,String name){
this.rollNo=rollNo;
this.name=name;
}
/* #Override
public boolean equals(Object o){
if((o instanceof student1) && (((student1)o).rollNo == rollNo)){
return true;
}
else
{
return false;
}
}
#Override
public int hashCode(){
return 1;
}
*/
public int compareTo(student1 s) {
System.out.println("hello:"+(this.rollNo-s.rollNo));
return this.rollNo-s.rollNo;
}
public String toString(){
return "["+rollNo+","+name+"]";
}
}
public class treeSetDemo {
public static void main(String... a){
Set<student1> set=new TreeSet<student1>();
set.add(new student1(102,"Anu"));
set.add(new student1(101,"Tanu"));
set.add(new student1(103,"Minu"));
System.out.println("elements:"+set);
}
}
OP:
run:
hello:-1
hello:1
elements:[[101,Tanu], [102,Anu], [103,Minu]]
BUILD SUCCESSFUL (total time: 0 seconds)
you have to change compareTo method in bellow way
public int compareTo(student1 s) {
if(s.rollNo == this.rollNo){
return 0;
}else if(s.rollNo > this.rollNo){
return -1;
}else{
return 1;
}
}
- If you want to sort on the basis of only one attribute, then go with java.lang.Comparable<T> Intereface, along with Collections.sort(List l).
- But if you aim is to sort it on the basis of more then one attribute then go for java.util.Comparator<T> along with Collections.sort(List l, Comparator c).
Eg:
import java.util.Comparator;
public class Fruit implements Comparable<Fruit>{
private String fruitName;
private String fruitDesc;
private int quantity;
public Fruit(String fruitName, String fruitDesc, int quantity) {
super();
this.fruitName = fruitName;
this.fruitDesc = fruitDesc;
this.quantity = quantity;
}
public String getFruitName() {
return fruitName;
}
public void setFruitName(String fruitName) {
this.fruitName = fruitName;
}
public String getFruitDesc() {
return fruitDesc;
}
public void setFruitDesc(String fruitDesc) {
this.fruitDesc = fruitDesc;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public int compareTo(Fruit compareFruit) {
int compareQuantity = ((Fruit) compareFruit).getQuantity();
//ascending order
return this.quantity - compareQuantity;
//descending order
//return compareQuantity - this.quantity;
}
public static Comparator<Fruit> FruitNameComparator
= new Comparator<Fruit>() {
public int compare(Fruit fruit1, Fruit fruit2) {
String fruitName1 = fruit1.getFruitName().toUpperCase();
String fruitName2 = fruit2.getFruitName().toUpperCase();
//ascending order
return fruitName1.compareTo(fruitName2);
//descending order
//return fruitName2.compareTo(fruitName1);
}
};
}
I think this implementation is close to recommended:
#Override
public int compareTo(Object other) {
if(other == null || !(other instanceOf student)){
throw new IllegalArgumentException();
}
student s = (student) other;
if(this.rollNo > s.rollNo){
return 1;
} else if (this.rollNo < s.rollNo){
return -1;
} else {
return 0;
}
}
If you are using Comparable interface then your compareTo() method should return the comparison not equals method , Google comparable example.
Check this link
In your compareTo method, you are just returning the value of the object you are comparing to. You need to return the difference, of the attribute of the invoking instance and passed instance.
So, change your compareTo method to the below one: -
#Override
public int compareTo(student1 s) {
return this.rollNo - s.rollNo;
}
NOTE: - Only sign is important for Collections.sort, so you don't really need an if-else block to return -1, 0, or 1. Just return the difference. That's all.
P.S : -
Your hashcode implementation is a very poor one. It will put every instances in the same bucket.
#Override
public int hashCode(){
return 1; // All the instances will have the same hashcode.
}
Ideally, you should use only those attributes to calculate the hashCode which you have used to compare your two instances, here its rollNo.
So, rather than returning simply a value 1, you can have some equations, that calculates your hashcode, taking into to consideration your rollNo and a large prime number also.
You can go through Effective Java - Item#9 for more explanation of this topic.
Now, that your code is working fine, lets move to your 2nd doubt.
equals and hashCode methods are not used when you want to compare two objects that will be used while sorting. We override equals and hashCode methods in order to check whether an instance is equal to another instance later on.
So, compareTo method is not concerned with whether you have ocerrided equals ad hashCode method or not. And you can also infer from name as to what the two methods does, and can they be related or not.
Moreover, equals method is defined in Object class, whereas compareTo method is declared in Comparable interface. So, they are not interrelated.
Check the documentation of these methods: - Object#equals, Object#hashCode, and Comparable#compareTo

Why are custom objects not equivalent keys for a HashMap?

I'm having trouble using my own class as a key for a HashMap
public class ActorId {
private final int playerId;
private final int id;
ActorId(int playerId, int id) {
this.playerId = playerId;
this.id = id;
}
public boolean equals(ActorId other) {
return this.id == other.id && this.playerId == other.playerId;
}
public int hashCode() {
int hash = 1;
hash = hash * 31 + playerId;
hash = hash * 31 + id;
return hash;
}
public String toString() {
return "#" + playerId + "." + id;
}
public int getPlayerId() {
return playerId;
}
}
Here is a failing JUnit test
import static org.junit.Assert.*;
import java.util.Map;
import org.junit.Test;
public class ActorIdTest {
#Test
public final void testAsMapKey() {
ActorId a = new ActorId(123, 345);
ActorId b = new ActorId(123, 345);
assertTrue(a.equals(b));
assertEquals(a.hashCode(), b.hashCode());
// Works with strings as keys
Map<String, String> map1 = new java.util.HashMap<String, String>();
map1.put(a.toString(), "test");
assertEquals("test", map1.get(a.toString()));
assertEquals("test", map1.get(b.toString()));
assertEquals(1, map1.size());
// But not with ActorIds
Map<ActorId, String> map2 = new java.util.HashMap<ActorId, String>();
map2.put(a, "test");
assertEquals("test", map2.get(a));
assertEquals("test", map2.get(b)); // FAILS here
assertEquals(1, map2.size());
map2.put(b, "test2");
assertEquals(1, map2.size());
assertEquals("test2", map2.get(a));
assertEquals("test2", map2.get(b));
}
}
You need to change
public boolean equals(ActorId other) {
....
}
to
public boolean equals(Object other) {
....
}
Tip of the day: Always use #Override annotation.
If you had used the #Override annotation, the compiler would have caught the error and said:
The method equals(ActorId) of type ActorId must override or implement a supertype method
Your code is correct, but you also need to override the equals method inherited from Object.
Add this to your ActorId class:
#Override
public boolean equals(Object other) {
if(other == null || other.getClass() != getClass())
return false;
return equals((ActorId)other);
}
You definitely must override the method equals(Object), and for certain implementation of a Map (HashMap) it is also necesary that you overrdide the method hashCode().
I had the same problem, and without the custom hashCode implementation the equals method of the class "ActorId" was never called.
By default Java invokes boolean equals(Object obj);
So, you login is correct but if you want to OVERRIDE equals() use Object as a parameter and and check the class by instanceOf or getClass() and do a class casting.
if (obj instanceOf ActorId) {
ActorId other = (ActorId)obj;
... compare fields
}

Categories

Resources