简体   繁体   English

EJB HashMap如何实现比较两个对象的方法

[英]EJB HashMap How to implement a method that compares two objects

This is an EJB project. 这是一个EJB项目。 I need to implement a function that compares two or more objects and I can add or remove only one object once I click "Add" or "Remove" button. 我需要实现一个比较两个或多个对象的函数,单击“添加”或“删除”按钮后,只能添加或删除一个对象。 Therefore, my idea about it is to store objects in the HashMap, then compare them and take the best. 因此,我的想法是将对象存储在HashMap中,然后对其进行比较并取得最佳效果。 But when I run my methods, the HashMap is null. 但是,当我运行我的方法时,HashMap为null。 Methods cannot add objects in it. 方法无法在其中添加对象。 How can make the HashMap is operated. 如何使HashMap操作。 My codes is attached below. 我的代码附在下面。

@Stateful
public class ComparePropertySessionBean implements ComparePropertySessionBeanRemote{

public HashMap<Integer, Double> map = new HashMap<Integer, Double>();

@Override
public int getBestPerRoom() {
    Iterator<Integer> iterator = map.keySet().iterator();
    int i = 0;
    double ave = 10000000000.00;
    if (iterator.hasNext()) {
        Integer key = iterator.next();
        if (map.get(key) <  ave) {
            i = key;
            ave = map.get(key);
        }
    }

    return i;
}

@Override
public void addCompareProperty(int propertyId, double price, int noofbedrooms) {

    if (!map.containsKey(propertyId)) {
        map.put(propertyId, price/noofbedrooms);

    }

}

@Override
public void removeCompareProperty(int propertyId) {
    if (map.containsKey(propertyId)) {
        map.remove(propertyId);
    }
}

} }

map itself could only be null here if some other code outside this class was setting it to null. 如果此类之外的其他代码将其设置为null,则map本身只能为null。 (Which is unlikely, but you should really make the map field private on principle). (这不太可能,但是原则上您应该真正将map字段private )。

It would be good to see the stack trace, but the big problem I'm seeing is a logic error in getBestPerRoom() : This method works if the map has at least one entry, but if it's empty it will return 0. If your code then tries to call the other methods with this as a propertyId , they will get a null entry and (if they aren't checking carefully) will get a NPE. 最好查看堆栈跟踪,但是我看到的最大问题是getBestPerRoom()的逻辑错误:如果映射至少具有一个条目,则此方法有效,但是如果映射为空,则它将返回0。然后,代码尝试使用此方法作为propertyId调用其他方法,它们将获得一个null条目,并且(如果未仔细检查)将获得NPE。
To avoid these kinds of problems, you need to properly handle the case that no entries are present. 为避免此类问题,您需要正确处理不存在任何条目的情况。

More generally though, there are much better ways to structure this code - OO principles suggest you should use a Property class to encapsulate all the information about the different properties. 但是,更一般而言,有更好的方法来构造此代码-OO原则建议您应该使用Property类来封装有关不同属性的所有信息。 You can then sort them by price by writing a Comparator (or just store them in a TreeSet). 然后,您可以通过编写Comparator按价格对它们进行排序(或仅将它们存储在TreeSet中)。
This would also allow you to easily store your Properties in a database using JPA, if you're writing an EJB application. 如果您正在编写EJB应用程序,这还使您可以使用JPA轻松地将Properties存储在数据库中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM