0

I have implemented HashMap for storing hotel booking entry.But I'm getting null values on .get(object) method even it contains all keys and returning keys correctly.I already override equals() & hashCode() methods in two different class (bookingSeason & entry) because bookingSeason class is used in some other class also and it works correctly but in entry class it does not work.

public class Entry {
String code;
List<BookingSeason> booking=new ArrayList<>();  

public Entry(String code) {
    this.code=code;
}

 @Override
public boolean equals(Object o) {

    if(o==null)
        return false;
    if(!(o instanceof Entry))
        return false;
    Entry room=(Entry) o;
    return this.code.equals(room.code)&&
            this.booking.equals(room.booking);
}

@Override
public int hashCode() {
   return Objects.hash(code,booking);
}
}
public class BookingSeason {
LocalDate startDate;
LocalDate endDate;
public BookingSeason(LocalDate startDate,LocalDate endDate) {
    this.startDate=startDate;
    this.endDate=endDate;
}

@Override
public boolean equals(Object object)
{
    if(object==this)
        return true;
    if(!(object instanceof BookingSeason))
        return false;
    BookingSeason bS=(BookingSeason) object;
    return Objects.equals(startDate,bS.startDate)&& Objects.equals(
            endDate,bS.endDate);
}

@Override
public int hashCode() {
    return Objects.hash(startDate,endDate);
}
}
public class Hotel {
List<BookingSeason> bookPeriod=new ArrayList<>();
HashMap<Long,Entry> roomEntry =new HashMap<>();
long num;
Entry newRoom=new Entry();
for(int i=101;i<=199;i++) {
        num=i;
        newRoom.code="A";
        newRoom.code=newRoom.code.concat(String.valueOf(i));
        roomEntry.put(num,new Entry(newRoom.code));
        System.out.println(roomEntry.get(i));
    }

}

2 Answers2

2
roomEntry.put(num,new Entry(newRoom.code));

uses the long value num to enter the new Entry object into the hashmap.

System.out.println(roomEntry.get(i));

uses the int value i to try to get the Entry object.

But, since

Long.valueOf(11).equals(Integer.valueOf(11)) == false

it will not found the entry. You need to pass a long/Long value to the get method. Therefore, either using

 System.out.println(roomEntry.get(num));

or using

System.out.println(roomEntry.get((long) i));

will solve your problem.

For reference see also What are the reasons why Map.get(Object key) is not (fully) generic

Community
  • 1
  • 1
Thomas Kläger
  • 17,754
  • 3
  • 23
  • 34
0

Solution:

Simply change the for loop from this:

for(int i=101;i<=199;i++) {

to this:

for(long i=101;i<=199;i++) {

Explanation:

What you have done wrong here is that you are trying to pass an int into the get method of HashMap<Long, Entry>.

Since the HashMap uses longs as keys, whenever the get method sees you try to use some other type of incompatible value, like int, it just returns null!

Although the integer i you passed into get has the same hash code as the long num, the get method treats them as "not the same".

Why?

Think about what would happen if get allowed you to use an object that is of unrelated type to the key type of the hash map to access a value:

class A {
    @Override
    public int hashCode() { return 1; }
}

class B {
    @Override
    public int hashCode() { return 1; }
}

// in some method
HashMap<A, Object> map = new HashMap<>();
map.put(new A(), "some stuff");
map.get(new B()); // returns "someStuff"! makes no sense, right?
Sweeper
  • 213,210
  • 22
  • 193
  • 313