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

(すべてのキー/値を文字列として使用します。その方が簡単です。)

クラスの実装後 (たとえば、class-name は 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);
}

アトミックではないことに注意してください。したがって、WhateverMap であっても、これはスレッドセーフではありません。

于 2014-04-15T13:29:38.747 に答える
0

Google Guava のTable<R, C, V>コレクションを使用できます。イーブラハムの答えに似ています。値は、行とV列によってキー付けされます。これは、すぐに判読できなくなり、操作が困難になる使用に代わる優れた方法です。RCHashMap<R, HashMap<C, V>>

詳細については、GitHub Wikiを参照してください。

于 2019-04-09T17:06:07.313 に答える
0

ネストされたハッシュマップは道だとは思いません。Student クラスと Subject クラスを作成します。

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
}

次に、Students インスタンスをハッシュマップに格納できます。

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 に答える