Peter
Peter

Reputation: 131

Java collection and MapKey

I am new to java. I have a database table which have following composite key:

Code
Reference_Number_1 (decimal)
Reference_Number_2 (decimal)
Time

Above keys make each row a unique row - no dupliactes. I need to create a class to load this table to a java collection and create a static method which will take above four arguments as key and return the entity from java collection.

I am thinking of loading the table into a HashMap but I am not sure how to define the MapKey. Should I convert Reference_Number_1, Reference_Number_2 and Time in to a string and then concatenate these four fields? Or there is another way/collection to load this table. Thanks, Pete

Upvotes: 1

Views: 168

Answers (2)

Jonathan Schneider
Jonathan Schneider

Reputation: 27747

Encapsulate these four fields in an object:

public class Key {
    String code;
    float reference1;
    float reference2;
    Date time;

    public Key(String code, float ref1, float ref2, Date time) {
        ...
    }

    // implement equals() and hashCode()
}

Then define a Map as follows (not sure what type the "entity" is, so we will just pretend there is a class called Entity:

Map<Key, Entity> lookup = new HashMap<Key, Entity>();

Upvotes: 2

BalusC
BalusC

Reputation: 1109122

Create another class which holds those 4 fields as properties and implement/autogenerate equals() and hashCode() according the contract (important! otherwise it can't be used as a proper Map key) and finally use it as (composite) key for the Map.

Here's what Eclipse has autogenerated for me (the equals() is open for improvement, it's somewhat verbose):

public class CompositeKey {

    private String code;
    private BigDecimal referenceNumber1;
    private BigDecimal referenceNumber2;
    private Date time;

    public CompositeKey(String code, BigDecimal referenceNumber1, BigDecimal referenceNumber2, Date time) {
        this.code = code;
        this.referenceNumber1 = referenceNumber1;
        this.referenceNumber2 = referenceNumber2;
        this.time = time;
    }

    public String getCode() {
        return code;
    }

    public BigDecimal getReferenceNumber1() {
        return referenceNumber1;
    }

    public BigDecimal getReferenceNumber2() {
        return referenceNumber2;
    }

    public Date getTime() {
        return time;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        CompositeKey other = (CompositeKey) obj;
        if (code == null) {
            if (other.code != null)
                return false;
        }
        else if (!code.equals(other.code))
            return false;
        if (referenceNumber1 == null) {
            if (other.referenceNumber1 != null)
                return false;
        }
        else if (!referenceNumber1.equals(other.referenceNumber1))
            return false;
        if (referenceNumber2 == null) {
            if (other.referenceNumber2 != null)
                return false;
        }
        else if (!referenceNumber2.equals(other.referenceNumber2))
            return false;
        if (time == null) {
            if (other.time != null)
                return false;
        }
        else if (!time.equals(other.time))
            return false;
        return true;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((code == null) ? 0 : code.hashCode());
        result = prime * result + ((referenceNumber1 == null) ? 0 : referenceNumber1.hashCode());
        result = prime * result + ((referenceNumber2 == null) ? 0 : referenceNumber2.hashCode());
        result = prime * result + ((time == null) ? 0 : time.hashCode());
        return result;
    }

}

Upvotes: 6

Related Questions