16

私は Java クラスのために取り組んでいるプロジェクトを (明らかに) 持っていますが、TreeMap と対話する方法に関する講義を見逃していたに違いありません。この部分で何をしているのかわかりませんし、Google からの助けもあまり得られません。

プログラムの最初のケースでは、TreeMap のすべての値を出力する必要があります。以下は、提供されたコードと、それを使用して行った作業です。A の場合のすべてが私のものですが、機能していません。どんな助けでも大歓迎です。

import java.util.Scanner;
import java.util.Set;
import java.util.Map;
import java.util.TreeMap;
import java.io.File;
import java.io.FileNotFoundException;

public class prog7 {
 public static void main(String args[])
 throws FileNotFoundException
 {
Scanner kb=new Scanner(System.in);

/*here, add code to declare and create a tree map*/
TreeMap treeMap = new TreeMap();

/*here, add code to declare a variable and
 let it be the key set of the map
 */
String key;

//temporary variables
String tempWord;
String tempDef;

//the following code reads data from the file glossary.txt
//and saves the data as entries in the map
Scanner infile=new Scanner(new File("glossary.txt"));

while(infile.hasNext())
{
  tempWord=infile.nextLine();
  tempDef=infile.nextLine();

  /*here, add code to add tempWord and tempDef
   as an entry in the map
   */
  treeMap.put(tempWord, tempDef);

}
infile.close();

while(true)
{
  System.out.println();
  System.out.println();

  //show menu and prompt message
  System.out.println("Please select one of the following actions:");
  System.out.println("q - Quit");
  System.out.println("a - List all words and their definitons");
  System.out.println("b - Enter a word to find its definition");
  System.out.println("c - Add a new entry");
  System.out.println("d - Delete an entry");
  System.out.println("Please enter q, a, b, c or d:");

  String selection=kb.nextLine();  //read user's selection
  if (selection.equals("")) continue; //if selection is "", show menu again

  switch (selection.charAt(0))
  { 
    case 'q':
      System.out.println("\nThank you.");
      return;

      /*write code for the cases 'a','b','c' and 'd'
       so that the program runs as in the sample run
       */

    case 'a':
       for (String treeKey : treeMap.keySet())
          System.out.println(treeKey);


    break;
4

7 に答える 7

20

keySetではなくentrySetを繰り返し処理します。Map.Entry<K, V>あなたは便利getKey()getValue()方法を持っているセットを手に入れます。

とは言うものの、Javaの標準Map実装には、必要なことを実行するtoString()の実装があります。もちろん、それを巧みに回避するためではなく、それを再実装するためのポイントしか得られないと思います...

for (Map.Entry<K, V> entry : myMap.entrySet()) {
     System.out.println("Key: " + entry.getKey() + ". Value: " + entry.getValue());
}
于 2011-04-22T15:55:37.330 に答える
9

entrySet()を使用できます。Javaのすべてのマップにはこのメソッドがあります。

Map<String, String> tree = new TreeMap<String, String>();
tree.put("param1", "value1");
tree.put("param2", "value2");
for (Entry<String, String> entry : tree.entrySet()) {
    String key = entry.getKey();
    String value = entry.getValue();

    System.out.printf("%s : %s\n", key, value);
}
于 2011-04-22T16:00:47.217 に答える
2
for (String treeKey : treeMap.keySet())

それはあなたに鍵を与えます。

そのループでtreeMap、キー ( treeKey) を使用して各値を取得し、出力します。

于 2011-04-22T15:51:49.153 に答える
1

treeMap.get(treeKey)ループ内で使用して、キーの値を取得できます。この値は文字列であるため、次のようにすることができます。

System.out.println("The value for the current key is " + (String)treeMap.get(treeKey));
于 2011-04-22T15:53:20.777 に答える
0

キーのセットを反復処理してから各値を取得することは、エントリのセットを反復処理するよりも効率的ではありません(getEntries())。前者の場合、getValue()は毎回新しいルックアップを実行する必要がありますgetEntries()が、マップのコンテンツ全体を返すことができます。

FindBugsなどの静的アナライザーは、マップのキーの反復内で値を取得しようとすると、警告を表示します。

于 2011-04-22T16:00:34.083 に答える
0

特にTreeMapsについてではなく、Maps全般について-Guavaを使用したこの最初の回答が好きです:Java Mapをカスタムキー=値文字列に変換します

于 2015-10-20T02:43:03.543 に答える