[英]put and get for nested hashmap in java
我对Java真的很陌生,我正在尝试使用Hashmap实现某些东西。
下面的代码是我首先声明的:
private HashMap<String, TreeMap<Object, Object>> submissions = new HashMap<String, TreeMap<Object, Object>>();;
和,
public Submission add(String unikey, Date timestamp, Integer grade) {
// check the argument
if(unikey == null || timestamp == null || grade == null) {
throw new IllegalArgumentException("Null argument detected\n");
}
}
这就是我目前正在写的。 假设存在称为“人”,“数据”和“等级”的项目。 有人可以告诉我如何将它们放入嵌套的哈希图中吗? 我完成了另一个类MySubmissions中每个项目的getter和setter的编写。
Submission是用另一个类编写的接口,其中包含以下方法:
public String getPerson();
public Date getTime();
public Integer getGrade();
我想要实现的是,例如,
?.add("aaaa1234", df.parse("2016/09/03 09:00:00"), 10);
?.add("aaaa1234", df.parse("2016/09/03 16:00:00"), 20);
?.add("cccc1234", df.parse("2016/09/03 16:00:00"), 30);
?.add("aaaa1234", df.parse("2016/09/03 18:00:00"), 40);
谢谢!
(我真正想要实现的是,我想将数据添加到哈希图中。然后使用另一种名为getBestGrade的方法,我想获得列表中评分最高的人员,但是我只想知道如何存储到哈希图中首先使用put and get ...)
创建一个实体
public class Submission {
private Date timestamp;
private Integer grade;
public Date getTimestamp() {
return timestamp;
}
public void setTimestamp(Date timestamp) {
this.timestamp = timestamp;
}
public Integer getGrade() {
return grade;
}
public void setGrade(Integer grade) {
this.grade = grade;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Submission that = (Submission) o;
if (timestamp != null ? !timestamp.equals(that.timestamp) : that.timestamp != null) return false;
return grade != null ? grade.equals(that.grade) : that.grade == null;
}
@Override
public int hashCode() {
int result = timestamp != null ? timestamp.hashCode() : 0;
result = 31 * result + (grade != null ? grade.hashCode() : 0);
return result;
}
}
创建一个HashMap
private HashMap<String, Submission> map = new HasMap<>();
做添加
map.add("key", new Submission());
我认为他想知道如何为每个人存储多个提交。 您可以执行以下操作:
import java.util.Date;
import java.util.HashMap;
import java.util.TreeMap;
public final class BestGrade
{
private static final HashMap<String, TreeMap<Date, Integer>> SUBMISSIONS = new HashMap<String, TreeMap<Date, Integer>>();
private BestGrade()
{}
public static void main(final String[] args)
{
// How to add
add("Person1", new Date(), Integer.valueOf(1));
add("Person1", new Date(), Integer.valueOf(10));
add("Person1", new Date(), Integer.valueOf(20));
add("Person2", new Date(), Integer.valueOf(1));
add("Person3", new Date(), Integer.valueOf(30));
add("Person3", new Date(), Integer.valueOf(40));
// How to get best grade
final Integer bestGradePerson1 = getBestGrade("Person1");
final Integer bestGradePerson3 = getBestGrade("Person2");
final Integer bestGradePerson2 = getBestGrade("Person3");
System.out.println("Bestgrade Person1: " + bestGradePerson1);
System.out.println("Bestgrade Person2: " + bestGradePerson2);
System.out.println("Bestgrade Person3: " + bestGradePerson3);
}
public static void add(final String key, final Date timestamp, final Integer grade)
{
// TODO the same for timestamp and grade
if (key == null || key.trim().isEmpty()) {
throw new IllegalArgumentException("key must not be null");
}
// Get
TreeMap<Date, Integer> submission = SUBMISSIONS.get(key);
// Create your treemap if not already exists, before adding new value to avoid NullPointerException
if (submission == null) {
submission = new TreeMap<Date, Integer>();
SUBMISSIONS.put(key, submission);
}
submission.put(timestamp, grade);
}
public static Integer getBestGrade(final String key)
{
Integer bestGrade = null;
final TreeMap<Date, Integer> submission = SUBMISSIONS.get(key);
if (submission == null) {
// When no submission available, return null or any other value you wish to show there is no best grade
return bestGrade;
}
for (final Integer grade : submission.values()) {
if (bestGrade == null) {
bestGrade = grade;
}
// Set new grade when values is higher than before
else if (bestGrade.intValue() < grade.intValue()) {
bestGrade = grade;
}
}
return bestGrade;
}
}
我将仅描述如何使用地图地图-由您决定这是否是您真正想要使用的地图。 我将使用称为A
, B
, C
等的类-如果愿意,您可以替换自己的类,包括String
或Submission
。
在解决此问题之前,请确保您对单级Map
有深刻的理解HashMap
等如何需要equals()
和hashCode()
。
您可以像完成许多操作一样定义地图:
Map<A, ? extends Map<B,C>> mapOfMaps;
通常,为变量提供一种Map
类型,而不是HashMap
或TreeMap
-您通常不需要实现类的任何更具体的方法。 如果这样做,您总是可以更改的。 ? extends Map<>
? extends Map<>
部分允许您的地图包含Map
任意实现。
您可以这样实例化:
Map<A, ? extends Map<B,C>> mapOfMaps = new HashMap<>();
// or with explicit (unnecessary) type declarations:
Map<A, ? extends Map<B,C>> mapOfMaps = new HashMap<A, ? extends Map<B,C>>();
现在您有一个空的地图。 您可以向其中添加地图:
Map<B,C> map = new HashMap<>();
mapOfMaps.put(new A(1), map);
现在,您有了一个包含一个空地图的地图。 或者,您可以添加包含以下内容的地图:
Map<B,C> map = new HashMap<>();
map.put(b, c);
mapOfMaps.put(a, map);
可能是,您想在不知道Map<B,C>
是否存在的情况下添加项目。 这里没有捷径-您必须执行以下操作:
void addToMapOfMaps(A a, B b, C c) {
Map<B,C> map = mapOfMaps.get(a);
if(map == null) {
map = new HashMap<>();
mapOfMaps.put(a,map);
}
map.put(b,c);
}
请注意,如果多个线程同时执行此操作,则会出现问题。
同样,如果您只是阅读,则必须在两个级别上处理缺失的元素:
C get(A a, B b) {
Map<B,C> map = mapOfMaps.get(a);
if(map == null) {
return null;
}
return map.get(b);
}
(或更紧凑)
C get(A a, B b) {
Map<B,C> map = mapOfMaps.get(a);
return map == null ? null : map.get(b);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.