Java 调用HashMap.get(对象)时在HashMap对象中获取空值
我已经实现了用于存储酒店预订条目的HashMap。但是我在.get(object)方法上得到了空值,即使它正确地包含所有键和返回键。我已经在两个不同的类(bookingSeason和entry)中重写了equals()&hashCode()方法因为bookingSeason类也在其他类中使用,并且它工作正常,但在entry类中它不工作Java 调用HashMap.get(对象)时在HashMap对象中获取空值,java,null,hashmap,Java,Null,Hashmap,我已经实现了用于存储酒店预订条目的HashMap。但是我在.get(object)方法上得到了空值,即使它正确地包含所有键和返回键。我已经在两个不同的类(bookingSeason和entry)中重写了equals()&hashCode()方法因为bookingSeason类也在其他类中使用,并且它工作正常,但在entry类中它不工作 public class Entry { String code; List<BookingSeason> booking=new ArrayList
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));
}
}
公共类条目{
字符串代码;
列表预订=新建ArrayList();
公共条目(字符串代码){
这个。代码=代码;
}
@凌驾
公共布尔等于(对象o){
如果(o==null)
返回false;
如果(!(输入的实例))
返回false;
进入室=(进入)o;
返回此.code.equals(room.code)&&
此。预订。等于(房间。预订);
}
@凌驾
公共int hashCode(){
返回对象。散列(代码、预订);
}
}
公务舱预订季{
本地日期开始日期;
LocalDate-endDate;
公共预订季节(LocalDate开始日期、LocalDate结束日期){
这个.startDate=startDate;
this.endDate=endDate;
}
@凌驾
公共布尔等于(对象)
{
if(object==this)
返回true;
if(!(预订季节的对象实例))
返回false;
BookingSeason bS=(BookingSeason)对象;
返回Objects.equals(startDate,bS.startDate)和&Objects.equals(
结束日期,bS.结束日期);
}
@凌驾
公共int hashCode(){
返回Objects.hash(startDate,endDate);
}
}
公务舱酒店{
List bookPeriod=new ArrayList();
HashMap roomEntry=新建HashMap();
长数;
Entry newRoom=新条目();
对于(int i=101;i
使用长值num
将新的Entry
对象输入hashmap
System.out.println(roomEntry.get(i));
使用int值i
尝试获取条目
对象
但是,自从
Long.valueOf(11).equals(Integer.valueOf(11)) == false
它将找不到该条目。您需要将long/long值传递给get方法。因此,使用
System.out.println(roomEntry.get(num));
或使用
System.out.println(roomEntry.get((long) i));
这会解决你的问题
参考另见
使用长值num
将新的Entry
对象输入hashmap
System.out.println(roomEntry.get(i));
使用int值i
尝试获取条目
对象
但是,自从
Long.valueOf(11).equals(Integer.valueOf(11)) == false
它将找不到该条目。您需要将long/long值传递给get方法。因此,使用
System.out.println(roomEntry.get(num));
或使用
System.out.println(roomEntry.get((long) i));
这会解决你的问题
有关参考信息,请参见解决方案:
只需从以下内容更改for循环:
for(int i=101;i<=199;i++) {
解决方案:
只需从以下内容更改for循环:
for(int i=101;i<=199;i++) {
在for()环孔中,显示您发现返回(n您意外)null
值的代码的位置。事实上,您的问题不符合a的完整性部分。在a for()中,您发现返回(n您意外)null
值的代码在哪里loopWell,展示给大家看。事实上,你的问题不符合a的完整性部分