Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JAVA HashMap 2D,无法获得制作2D HashMap的正确方法,我的意思是将一个HashMap转换成另一个HashMap_Java_Hashmap_Nested - Fatal编程技术网

JAVA HashMap 2D,无法获得制作2D HashMap的正确方法,我的意思是将一个HashMap转换成另一个HashMap

JAVA HashMap 2D,无法获得制作2D HashMap的正确方法,我的意思是将一个HashMap转换成另一个HashMap,java,hashmap,nested,Java,Hashmap,Nested,我想把学生的名字和科目做成一个黑板,每个学生在每个科目上都有一个分数(或者没有。他可以离开考试,不写,然后他的案例就空了)。我只想使用HashMaps。我的意思是,会是这样的: HashMap<String,HashMap<String,String>> bigBoard = new HashMap<String,HashMap<String,String>>(); (我将使用所有键/值作为字符串,这样会更简单。) 在实现类之后(例如类

我想把学生的名字和科目做成一个黑板,每个学生在每个科目上都有一个分数(或者没有。他可以离开考试,不写,然后他的案例就空了)。我只想使用HashMaps。我的意思是,会是这样的:

HashMap<String,HashMap<String,String>> bigBoard = 
    new HashMap<String,HashMap<String,String>>();
(我将使用所有键/值作为字符串,这样会更简单。)

在实现类之后(例如类名为String2D),我们应该这样使用它

public static void main(String[] args) {

    String2D map2D = new String2D(); 
    map2D.put("Daniel Doster", "Practical Mathematics", "1.3"); 
    map2D.put("Daniel Doster", "IT Systeme", "3.7"); 
    map2D.put("Micky Mouse", "Finance", "5");
    map2D.put("Minnie Mouse", "IT Systeme", "1.7");
    map2D.put("Minnie Mouse", "Finance", "n/a");
    map2D.put("Dagobert Duck", "Practical Mathematics", "4.0");
    map2D.put("Dagobert Duck", "Finance", "1.0");
    System.out.println(map2D); 
} 

不会看到“HashMap”。。不允许使用和数组

您可以使用此类:

public class BiHashMap<K1, K2, V> {

private final Map<K1, Map<K2, V>> mMap;

public BiHashMap() {
    mMap = new HashMap<K1, Map<K2, V>>();
}

/**
 * Associates the specified value with the specified keys in this map (optional operation). If the map previously
 * contained a mapping for the key, the old value is replaced by the specified value.
 * 
 * @param key1
 *            the first key
 * @param key2
 *            the second key
 * @param value
 *            the value to be set
 * @return the value previously associated with (key1,key2), or <code>null</code> if none
 * @see Map#put(Object, Object)
 */
public V put(K1 key1, K2 key2, V value) {
    Map<K2, V> map;
    if (mMap.containsKey(key1)) {
        map = mMap.get(key1);
    } else {
        map = new HashMap<K2, V>();
        mMap.put(key1, map);
    }

    return map.put(key2, value);
}

/**
 * Returns the value to which the specified key is mapped, or <code>null</code> if this map contains no mapping for
 * the key.
 * 
 * @param key1
 *            the first key whose associated value is to be returned
 * @param key2
 *            the second key whose associated value is to be returned
 * @return the value to which the specified key is mapped, or <code>null</code> if this map contains no mapping for
 *         the key
 * @see Map#get(Object)
 */
public V get(K1 key1, K2 key2) {
    if (mMap.containsKey(key1)) {
        return mMap.get(key1).get(key2);
    } else {
        return null;
    }
}

/**
 * Returns <code>true</code> if this map contains a mapping for the specified key
 * 
 * @param key1
 *            the first key whose presence in this map is to be tested
 * @param key2
 *            the second key whose presence in this map is to be tested
 * @return Returns true if this map contains a mapping for the specified key
 * @see Map#containsKey(Object)
 */
public boolean containsKeys(K1 key1, K2 key2) {
    return mMap.containsKey(key1) && mMap.get(key1).containsKey(key2);
}

public void clear() {
    mMap.clear();
}

}
然后像这样创建和使用它:

BiHashMap<String,String,String> bigBoard = new BiHashMap<String,String,String>();
BiHashMap bigBoard=新的BiHashMap();

但是,为了提高性能,您可能希望将不同的成绩存储在一个数组中(假设您有一组固定的课程)

我不认为嵌套hashmap是一种可行的方法。创建一个学生类和主题类

public class Student{
    private ArrayList<Subject> SubjectList = new ArrayList<Subject>();
    private String name;

    public Student(String name){
        this.name=name;
    }
    public void addSubject(Subject s){
        SubjectList.add(s);
    }
    public String getName(){
        return this.name;
    }
    //...add methods for other operations
}
public class Subject{
    private ArrayList<double > GradeList = new ArrayList<double>();
    private String name;

    public Subject(String name){
        this.name=name;
    }
    public void addGrade(double s){
        GradeList.add(s);
    }
    //...add methods for other operations
}
公共班级学生{
private ArrayList SubjectList=新建ArrayList();
私有字符串名称;
公立学生(字符串名称){
this.name=name;
}
公共科目(科目s){
主题列表。添加;
}
公共字符串getName(){
返回此.name;
}
//…为其他操作添加方法
}
公共课科目{
private ArrayList GradeList=新建ArrayList();
私有字符串名称;
公共主题(字符串名称){
this.name=name;
}
公共交通等级(双s){
成绩表。添加(s);
}
//…为其他操作添加方法
}
然后可以将学生实例存储在hashmap中

public static void main(String[] args){
    HashMap<Students> hm = new HashMap<Students>();
    Student s = new Student("Daniel Dolter");
    Subject sub = new Subject("Mathematics");
    sub.addGrades(1.3);
    s.addSubject(sub);
    hm.put(s.getName(),s);
}
publicstaticvoidmain(字符串[]args){
HashMap hm=新的HashMap();
学生s=新生(“丹尼尔·多特”);
sub科目=新科目(“数学”);
分项工程(1.3);
s、 增补科目(附属科目);;
hm.put(s.getName(),s);
}

对于Java 8,如果默认值为空,则可以使用ComputeFabSent插入默认值。 因此,您可以简单地将其用作2d贴图的类型:

Map<RowType, Map<ColumnType, ValueType>> map = new WhateverMap<>();
Map Map=newwhatevermap();
假设所有类型都是int:

int get(int x, int y) 
  return map.computeIfAbsent(x, (key)->new WhateverMap<>()).computeIfAbsent(y,(key)->0);
}

void put(int x, int y, int value) 
  return map.computeIfAbsent(x, (key)->new WhateverMap<>()).put(y,value);
}
intget(intx,inty)
返回map.computeIfAbsent(x,(key)->newwhatevermap()).computeIfAbsent(y,(key)->0);
}
无效放置(整数x,整数y,整数值)
返回map.computeIfAbsent(x,(key)->newwhatevermap()).put(y,value);
}

请注意,它不是原子的。因此,即使Vermap是什么,这也不是线程安全的。

您可以使用Google Guava的
集合。这与艾布拉罕的回答相似。值
V
由行
R
和列
C
键入。它是使用HashMap的更好的替代方法,HashMap很快变得不可读且难以使用


有关更多信息,请参阅他们的网站。

谢谢Victor!!但是我不允许在我的类中使用任何数组。我必须实现该类..:它必须只是类中的哈希映射。所以我需要“也许”BiHashMap的实现…-)@Zelleb我想我给你的代码正是你想要的
String2D
,只要用
String
替换
K1
K2
,和
V
。如果是这样的话,请选择我的答案。这也是我的选择,尽管在科目列表中,没有简单的方法可以查找特定科目的成绩。您可以添加一个公共的double[]getGrades(String subjectName)方法来辅助这一点。谢谢大家的回答。但是我需要在一个类中完成所有的事情,并且只使用HashMaps。。您可以在这里查看testclass
publicstaticvoidmain(最终字符串…忽略){final StringMap2D map2D=newtruestringmap2d();map2D.put(“丹尼尔·多斯特”,“实用数学”,“1.3”);map2D.put(“丹尼尔·多斯特”,“IT系统”,“3.7”);map2D.put(“迈克·缪斯”,“金融”,“5”);map2D.put(“我的缪斯”,“金融”,“不适用”);map2D.put(“Dago-Dichie”、“实用数学”、“4.0”);map2D.put(“Dago-Dichie”、“Finance”、“1.0”);System.out.println(map2D);}
main方法在实现StingMap2D(我必须实现才能获得2D HashMap的类)后测试我的类。应该是这样的。。然后,有了印刷品,我应该有你能在第一篇文章上看到的黑板。没有。。这是一个例子,比如在实现完成后我们应该如何使用这个类(我正在询问实现的方法)。你的解决方案对于作业或玩具应用程序应该可以很好地工作,但是如果你想实现一个真实世界的应用程序,请查看其他答案。特别是使用嵌套hashmap并没有真正遵循面向对象编程的原则。
int get(int x, int y) 
  return map.computeIfAbsent(x, (key)->new WhateverMap<>()).computeIfAbsent(y,(key)->0);
}

void put(int x, int y, int value) 
  return map.computeIfAbsent(x, (key)->new WhateverMap<>()).put(y,value);
}