1)addRecord(userId, projectId, price)表示用户userId 给工程 projectId 投标 price。若记录中有 userId 给工程ProjectId 投标,则不加入该记录
2)updateRecord(userId, projectId, price)表示更新用户userId 给工程 projectId 的投标price。若记录中没有 userId 给工程 projectId的投标,则不更新
3)removeRecore(userId, projectId)表示删除用户 userId给工程 projectId的投标
4)queryRecord(projectId, price)表示查询工程projectId中投标金额大于price的结果中,price最小的一条记录。若大于price的最小price有多条记录,则返回最早投标的那条记录。注意:addRecord和updateRecord都会更新记录的时间。
一、Map的实现类HashMap、LinkedHashMap、TreeMap什么时候使用?
二、TreeMap中的对象按照Key排序、对象按照Value排序
// 根据key值降序排序
private static Comparator<Integer> comparator = new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
if (o1 < o2) {
return 1;
} else if (o1 > o2) {
return -1;
} else {
return 0;
}
}
};
// 在定义TreeMap时指定比较器
private static Map<Integer, Integer> map = new TreeMap<>(comparator);
Map<Project, Integer> resultMap = new LinkedHashMap<>();
// 自定义比较器,根据Entry中的key值升序排序
private static Comparator<Map.Entry<Project, Integer>> comparator = new Comparator<Map.Entry<Project, Integer>>() {
@Override
public int compare(Map.Entry<Project, Integer> o1, Map.Entry<Project, Integer> o2) {
if(o1.getValue() < o2.getValue()) {
return -1;
} else if(o1.getValue() > o2.getValue()) {
return 1;
} else {
return 0;
}
}
};
// 将Map转换为List
List<Map.Entry<Project, Integer>> list = new ArrayList<>(resultMap.entrySet());
// 使用Collections.sort对list中的对象按照Value值进行排序
Collections.sort(list, comparator);
// 遍历取出最小value值对应的key
for (Map.Entry<Project, Integer> mapping : list) {
result = mapping.getKey().getUserId();
break;
}
三、如果HashMap中的Key是对象,如何实现添加key重复的对象?
class Project {
int userId;
int projectId;
public Project(int userId, int projectId) {
this.userId = userId;
this.projectId = projectId;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Project)) {
return false;
}
Project project = (Project) o;
return getUserId() == project.getUserId() &&
getProjectId() == project.getProjectId();
}
@Override
public int hashCode() {
return Objects.hash(userId, projectId);
}
}
public class Three {
public static void main(String[] args) {
addTender(1, 1, 10);
addTender(2, 2, 20);
addTender(2, 2, 30);
updateTender(1, 1, 40);
updateTender(1, 3, 40);
removeTender(1, 1);
removeTender(1, 3);
addTender(1, 2, 16);
addTender(3, 2, 10);
addTender(4, 2, 40);
queryTender(2, 15);
queryTender(5, 10);
/*addTender(1, 1, 10);
addTender(2, 1, 20);
updateTender(1, 1, 20);
queryTender(1, 10);*/
}
// 存储记录
private static HashMap<Project, Integer> record = new LinkedHashMap<>();
// 定义Key对象
static class Project {
int userId;
int projectId;
public Project(int userId, int projectId) {
this.userId = userId;
this.projectId = projectId;
}
// 重写equals方法
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Project)) {
return false;
}
Project project = (Project) o;
return getUserId() == project.getUserId() &&
getProjectId() == project.getProjectId();
}
@Override
public int hashCode() {
return Objects.hash(userId, projectId);
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public int getProjectId() {
return projectId;
}
public void setProjectId(int projectId) {
this.projectId = projectId;
}
}
public static void addTender(int userId, int projectId, int price) {
Project project = new Project(userId, projectId);
if (!record.containsKey(project)) {
record.put(project, price);
}
}
public static int updateTender(int userId, int projectId, int price) {
int updateResult = -1;
Project project = new Project(userId, projectId);
if(record.containsKey(project)) {
// update采用先remove再add的方式,确保时间更新,即在record中位置更新到最后
updateResult = removeTender(userId, projectId);
addTender(userId, projectId, price);
}
return updateResult;
}
public static int removeTender(int userId, int projectId) {
Project project = new Project(userId, projectId);
int deleteResult = -1;
if (record.containsKey(project)) {
deleteResult = record.get(project);
record.remove(project);
}
return deleteResult;
}
// 自定义按value值升序的比较器
private static Comparator<Map.Entry<Project, Integer>> comparator = new Comparator<Map.Entry<Project, Integer>>() {
@Override
public int compare(Map.Entry<Project, Integer> o1, Map.Entry<Project, Integer> o2) {
if(o1.getValue() < o2.getValue()) {
return -1;
} else if(o1.getValue() > o2.getValue()) {
return 1;
} else {
return 0;
}
}
};
public static int queryTender(int projectId, int price) {
Map<Project, Integer> resultMap = new LinkedHashMap<>();
for (Map.Entry<Project, Integer> entry : record.entrySet()) {
if (entry.getKey().getProjectId() == projectId) {
if (entry.getValue() <= price) {
continue;
} else {
resultMap.put(entry.getKey(), entry.getValue());
}
}
}
int result = -1;
if (!resultMap.isEmpty()) {
// Map按照Value进行排序
List<Map.Entry<Project, Integer>> list = new ArrayList<>(resultMap.entrySet());
Collections.sort(list, comparator);
for (Map.Entry<Project, Integer> mapping : list) {
result = mapping.getKey().getUserId();
break;
}
}
return result;
}
}