Java 如何在树形图中打印所有值?

Java 如何在树形图中打印所有值?,java,treemap,Java,Treemap,我有一个项目正在为我的Java类工作(显然),我一定错过了关于如何与树映射交互的讲座。我不知道我在用这个部件做什么,我也没有从谷歌那里找到很多帮助 对于程序中的第一种情况,我必须打印树映射的所有值。下面是提供给我的代码以及我使用它所做的工作。case A中的所有东西都是我的,但它不起作用。任何帮助都将不胜感激 import java.util.Scanner; import java.util.Set; import java.util.Map; import java.util.TreeMap

我有一个项目正在为我的Java类工作(显然),我一定错过了关于如何与树映射交互的讲座。我不知道我在用这个部件做什么,我也没有从谷歌那里找到很多帮助

对于程序中的第一种情况,我必须打印树映射的所有值。下面是提供给我的代码以及我使用它所做的工作。case 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;
这给了你钥匙


现在在该循环中,使用键(
treeKey
)从
treeMap
中获取每个值,并将其打印出来。

您可以在循环中使用
treeMap.get(treeKey)
来获取键的值。由于该值是一个字符串,您可以执行以下操作:

System.out.println("The value for the current key is " + (String)treeMap.get(treeKey));

在入口集而不是键集上迭代。您将获得一组
Map.Entry
,它们具有方便的
getKey()
getValue()
方法

也就是说,Java的标准映射实现有一个toString()的实现,可以满足您的需要。当然,我认为你只会因为重新实现它而得到分数,而不是因为巧妙地避免它

for (Map.Entry<K, V> entry : myMap.entrySet()) {
     System.out.println("Key: " + entry.getKey() + ". Value: " + entry.getValue());
}
for(Map.Entry:myMap.entrySet()){
System.out.println(“Key:+entry.getKey()+”。Value:+entry.getValue());
}

迭代一组键,然后检索每个值的效率低于迭代一组项(
getEntries()
)。在前一种情况下,
getValue()
每次都必须执行新的查找,而
getEntries()
可以简单地返回映射的全部内容

如果您试图在映射键上的迭代中检索值,静态分析器(如FindBugs)将向您发出警告。

您可以使用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);
}
Map tree=newtreemap();
树。put(“参数1”、“值1”);
树。put(“param2”、“value2”);
for(条目:tree.entrySet()){
String key=entry.getKey();
字符串值=entry.getValue();
System.out.printf(“%s:%s\n”,键,值);
}

不是专门关于树形图,而是关于一般的地图-我喜欢第一个答案,使用番石榴

使用Java 8+无需循环:lambda表达式使您能够以整洁的一行打印所有键和/或值:

map.forEach((key, value) -> System.out.println(key + " " + value));

+1用于使用家庭作业标签并发布到目前为止的代码。Ahhh。因此,我会以搜索后只打印一个的方式打印所有这些文件。我懂了。这太明显了。有时我想知道为什么我是计算机科学专业的学生,当我有这样的时刻时。@Lish-是的,我在我最新的编辑中提供了一个println示例,以便您可以看到如何做到这一点。希望有帮助。啊,是的。这是有道理的。entrySet是Set类的一部分吗?我不能给程序添加任何额外的类。不,这是地图的一部分:啊。有趣。如果我不能让其他东西发挥作用,我可能不得不尝试一下。我想尽量坚持教授的期望。不过谢谢你的提醒。如果我现在不使用它,我肯定会进一步研究它。除非您有比您描述的更多的约束,否则entrySet应该很好:您仍然可以完成大部分工作,并且它比为每个键调用
get(key)
更有效。我编辑我的文章是为了添加一个示例。在这种情况下,我需要将treeMap对象设置为另一种类型吗?如果我尝试使用该命令,会出现以下错误:
File:Z:\CS121\Project 7\prog7.java[line:79]错误:Z:\CS121\Project 7\prog7.java:79:找不到符号符号:方法getEntries()位置:class java.util.TreeMap
对不起。该方法称为
entrySet
。但老实说,你自己应该能找到的。Map类的API文档并不难找到。
map.forEach((key, value) -> System.out.println(key + " " + value));