To use a composite key in Java Map, specify a class that holds all the fields you want to use in the composite key in the Map key. At this time, override the following method of the class used for the key.
Consider the following class that holds sales, and manage this class with a Map that uses a composite key.
Composite key class
package hoge;
import java.time.LocalDate;
/**Composite key*/
public class CompositeKey {
/**Store number*/
private final Integer shopNo;
/**Sales date*/
private final LocalDate salesDate;
public CompositeKey(Integer storeNo, LocalDate salesDate) {
this.shopNo = storeNo;
this.salesDate = salesDate;
}
}
Since the equals method and hashCode method are defined in the Object class that inherits from all classes, this operation will override the method of the Object class.
Composite key class
package hoge;
import java.time.LocalDate;
/**Composite key*/
package hoge;
import java.time.LocalDate;
/**Composite key*/
public class CompositeKey {
/**Store number*/
private final Integer shopNo;
/**Sales date*/
private final LocalDate salesDate;
public CompositeKey(Integer storeNo, LocalDate salesDate) {
this.shopNo = storeNo;
this.salesDate = salesDate;
}
/* (Non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((salesDate == null) ? 0 : salesDate.hashCode());
result = prime * result + ((shopNo == null) ? 0 : shopNo.hashCode());
return result;
}
/* (Non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@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 (salesDate == null) {
if (other.salesDate != null)
return false;
} else if (!salesDate.equals(other.salesDate))
return false;
if (shopNo == null) {
if (other.shopNo != null)
return false;
} else if (!shopNo.equals(other.shopNo))
return false;
return true;
}
}
Store sales data in HashMap and get the value with the compound key.
Store number | Sales date | Sales amount |
---|---|---|
1 | 2017/01/01 | 100 |
2 | 2017/01/02 | 200 |
3 | 2017/01/03 | 300 |
Sales class
package hoge;
import java.time.LocalDate;
/**Sales record*/
public class SalesRecord {
/**Store number*/
private final Integer shopNo;
/**Sales date*/
private final LocalDate salesDate;
/**Sales amount*/
private final Integer sales;
public SalesRecord(Integer storeNo, LocalDate salesDate, Integer sales) {
this.shopNo = storeNo;
this.salesDate = salesDate;
this.sales = sales;
}
public Integer getStoreNo() {
return shopNo;
}
public LocalDate getSalesDate() {
return salesDate;
}
public Integer getSales() {
return sales;
}
/**for test*/
@Override
public String toString() {
return "SalesRecord [storeNo=" + shopNo + ", salesDate=" + salesDate + ", sales=" + sales + "]";
}
}
Main class
package hoge;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TestMain {
public static void main(String[] args) {
//test data
List<SalesRecord> salesRecords = new ArrayList<>();
salesRecords.add(new SalesRecord(1,LocalDate.of(2017,1,1),100));
salesRecords.add(new SalesRecord(2,LocalDate.of(2017,1,2),200));
salesRecords.add(new SalesRecord(3,LocalDate.of(2017,1,3),300));
//Generate HashMap
Map<CompositeKey,SalesRecord> map = new HashMap<>();
for (SalesRecord record : salesRecords) {
map.put(new CompositeKey(record.getStoreNo(), record.getSalesDate()), record);
}
//Confirm that Value can be retrieved from Key.
System.out.println(map.get(new CompositeKey(2,LocalDate.of(2017,1,2))));
}
}
Try to get the record from the sales amount data stored in HashMap with the following composite key.
Store number | Sales date |
---|---|
2 | 2017/01/02 |
console
SalesRecord [storeNo=2, salesDate=2017-01-02, sales=200]
It was confirmed that the specified record could be retrieved.
・ GitHub Use compound key in Java Map
The code created for testing is stored above. However, since the code posted includes the Main class, it can also be executed by copy and paste.
Recommended Posts