4

我想制作一个学生姓名和科目的委员会,每个学生在每个科目都有一个成绩(或者没有......他可以离开考试而不写它,然后他的案例将是空的)。我只想使用 HashMaps。我的意思是,它会是这样的:

HashMap<String,HashMap<String,String>> bigBoard = 
    new HashMap<String,HashMap<String,String>>();

但我认为,我没有正确的想法,因为对于每个科目,都会有很多成绩(值),所以这是不可能的。我必须为每个学生制作地图吗?与他的主题?但随后将不会安排输出表。你有提议吗?例如,我想要一张看起来像这样的桌子。

Column-Key →
  Rowkey↓      Mathematics         Physics       Finance

Daniel Dolter         1.3           3.7 

Micky Mouse                                      5

Minnie Mouse                        1.7          n/a

Dagobert Duck         4.0                        1.0

(我会将所有键/值用作字符串,这样会更简单。)

在我们的类实现之后(例如类名是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”..并且不允许使用数组

4

4 回答 4

7

你可以使用这个类:

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>();

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

于 2012-04-24T14:09:16.993 回答
0

在 Java 8 中,如果它为空,则可以使用computeIfAbsent插入默认值。因此,您可以简单地将其用作 2d-map 的类型:

Map<RowType, Map<ColumnType, ValueType>> map = new WhateverMap<>();

假设所有类型都是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);
}

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

于 2014-04-15T13:29:38.747 回答
0

我不认为嵌套的哈希图是要走的路。创建一个学生类和主题类。

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
}

然后,您可以将 Student 实例存储在哈希图中。

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);
}
于 2012-04-24T14:13:08.273 回答
0

您可以使用 Google Guava 的Table<R, C, V>集合。这类似于eabraham的回答。值V由行R和列作为键C。它是一个更好的替代方案,HashMap<R, HashMap<C, V>>它会很快变得不可读且难以使用。

有关更多信息,请参阅他们的GitHub Wiki

于 2019-04-09T17:06:07.313 回答