我真的是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 detectedn");
}
}
这是我目前正在写的。假设有称为"人","数据"one_answers"等级"的项目。有人可以告诉我如何将它们放在嵌套的哈希图中吗?我完成了另一个名为MySubmissions中的每个类项目的Getter和Setter。
提交是在另一个类中编写的接口,该界面包含以下方法:
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);
谢谢!
(我确切想要实现的是,我想将数据添加到hashmap中。然后使用另一种称为GetBestgrade的方法,我想在列表中获得最佳的分数,但我只想知道如何存储首先使用put和获取hashmap ...)
创建一个实体
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
有一定的了解 - equals()
和hashCode()
对于HashMap
等是必需的。
您可以像完成的地图一样定义地图:
Map<A, ? extends Map<B,C>> mapOfMaps;
通常,给变量一种Map
而不是HashMap
或TreeMap
的类型 - 通常您不需要任何更具体的实现类方法。如果这样做,您总是可以更改。? 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);
}