You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Enum与Guava Multimap时IDE无法找到已声明的映射问题

问题描述

我母语并非英语,若表述不清请见谅。我正在完成一项使用Enum和Google Guava Multimap的大学作业:此前的作业实现了以单词为键、释义为值的字典,本次需要添加词性信息。为此我创建了两个ListMultimap:一个存储单词与释义的映射,另一个存储单词与词性的映射。我试图在iterate方法中调用getPartOfSpeech方法获取词性并打印,但IDE提示无法找到符号partOfSpeechDict。请问我哪里出错了?是否允许创建两个Guava Multimap?

代码如下:

public class Test1
{
    private enum Entry
    {
        BOOK("Book","noun", "A written work published in printed or electric form."),
        BOOK2("Book","verb","To arrange for someone to have a seat on a plane."),
        BOOKABLE("Bookable","adjective","Can be ordered in advance."),
        BOOKCASE("Bookcase","noun","A piece of furniture with shelves."),
        BOOKBINDER("Bol okBinder","noun","A person who fastens the pages of books."),
        CSC2201("CSC220","noun","Data Structures."),
        CSC2202("CSC220","adjective","Ready to create complex data structures."),
        CSC2203("CSC220","verb","To create data structures.");
        private String key;
        private String partOfSpeech;
        private String definition;
        private Entry() {}
        Entry(String key, String partOfSpeech, String definition)
        {
            this.key = key;
            this.partOfSpeech = partOfSpeech;
            this.definition = definition;
        }
        public String getKey()
        {
            return this.key.toUpperCase();
        }
        public String getPartOfSpeech()
        {
            return this.partOfSpeech;
        }
        public String getDefinition()
        {
            return this.definition;
        }
        public String toString()
        {
            return this.key+" ["+ this.partOfSpeech+"] :  "+this.definition;
        }
    }
    public static void main(String[]args)
    {
        ListMultimap<String, String> dictionary = ArrayListMultimap.create();
        dictionary.put(Entry.BOOK.getKey(),Entry.BOOK.getDefinition());
        dictionary.put(Entry.BOOK2.getKey(), Entry.BOOK2.getDefinition());
        dictionary.put(Entry.BOOKABLE.getKey(), Entry.BOOKABLE.getDefinition());
        dictionary.put(Entry.BOOKCASE.getKey(), Entry.BOOKCASE.getDefinition());
        dictionary.put(Entry.BOOKBINDER.getKey(), Entry.BOOKBINDER.getDefinition());
        dictionary.put(Entry.CSC2201.getKey(), Entry.CSC2201.getDefinition());
        dictionary.put(Entry.CSC2202.getKey(), Entry.CSC2202.getDefinition());
        dictionary.put(Entry.CSC2203.getKey(), Entry.CSC2203.getDefinition());

        ListMultimap<String,String> partOfSpeechDict = ArrayListMultimap.create();
        partOfSpeechDict.put(Entry.BOOK.getKey(),Entry.BOOK.getPartOfSpeech());
        partOfSpeechDict.put(Entry.BOOK2.getKey(), Entry.BOOK2.getPartOfSpeech());
        partOfSpeechDict.put(Entry.BOOKABLE.getKey(), Entry.BOOKABLE.getPartOfSpeech());
        partOfSpeechDict.put(Entry.BOOKCASE.getKey(), Entry.BOOKCASE.getPartOfSpeech());
          partOfSpeechDict.put(Entry.BOOKBINDER.getKey(),Entry.BOOKBINDER.getPartOfSpeech());
        partOfSpeechDict.put(Entry.CSC2201.getKey(), Entry.CSC2201.getPartOfSpeech());
        partOfSpeechDict.put(Entry.CSC2202.getKey(), Entry.CSC2202.getPartOfSpeech());
        partOfSpeechDict.put(Entry.CSC2203.getKey(), Entry.CSC2203.getPartOfSpeech());

        System.out.println("- DICTIONARY JAVA Standard -----");
        System.out.println("-----      powered by Google Guava -");
        String searchKey = "null";
        String endCode = "!q";
        do
        {
            Scanner scan = new Scanner(System.in);
            System.out.print("Search:  ");
            searchKey = scan.nextLine();
            searchKey = searchKey.toUpperCase();
            if(dictionary.containsKey(searchKey))
            {
                char[]charArray = searchKey.toCharArray();
                for(int i=1;i<charArray.length;i++)
                    charArray[i]=Character.toLowerCase(charArray[i]);
                String str = new String(charArray);
                System.out.println("   |");
                iterate(dictionary,str);
                System.out.println("   |");
            }
            else if(searchKey.equalsIgnoreCase(endCode))
            {
                break;
            }
            else
            {
                System.out.println("   |");;
                System.out.println("\t<Not found>");
                System.out.println("   |");
            }
        }while(!searchKey.equalsIgnoreCase(endCode));
        System.out.println("\n-----THANK YOU-----");
    }
    public static <K,V> void iterate(ListMultimap<String, String> alMultimap,String str)
    {
        for(Map.Entry<String,String> entry : alMultimap.entries())
        {
            if(str.equalsIgnoreCase(entry.getKey()))
            {
                char[]charArray = entry.getKey().toCharArray();
                for(int i=1;i<charArray.length;i++)
                    charArray[i]=Character.toLowerCase(charArray[i]);
                String str2 = new String(charArray);
                System.out.print("\t"+str2+" ["+getPartOfSpeech(partOfSpeechDict, str2)+"] : "+entry.getValue()+"\n");
//IDE said that error is java : cannot find symbol(partOfSpeechDict). However, I made it.
            }
        }
    }
    public static <K,V> String getPartOfSpeech(ListMultimap<String, String> alMultimap,String str)
    {
        for(Map.Entry<String,String> entry : alMultimap.entries())
        {
            if(str.equalsIgnoreCase(entry.getKey()))
            {
                return entry.getValue();
            }
        }
        return null;
    }
}

另外,我曾上传错误信息但因Stack Overflow判定问题重复被降权,已删除该内容。


问题解答

1. 错误原因:变量作用域限制

partOfSpeechDict是在main方法内声明的局部变量,而iterate是独立的静态方法,它无法直接访问main方法的局部变量——这就是IDE提示“找不到符号”的核心原因。

2. 修复方案

把partOfSpeechDict作为参数传递给iterate方法即可:

步骤1:修改iterate方法的参数列表

public static <K,V> void iterate(ListMultimap<String, String> alMultimap, String str, ListMultimap<String, String> partOfSpeechDict)
{
    for(Map.Entry<String,String> entry : alMultimap.entries())
    {
        if(str.equalsIgnoreCase(entry.getKey()))
        {
            char[]charArray = entry.getKey().toCharArray();
            for(int i=1;i<charArray.length;i++)
                charArray[i]=Character.toLowerCase(charArray[i]);
            String str2 = new String(charArray);
            System.out.print("\t"+str2+" ["+getPartOfSpeech(partOfSpeechDict, str2)+"] : "+entry.getValue()+"\n");
        }
    }
}

步骤2:修改main方法中的调用语句

// 原调用语句
// iterate(dictionary,str);
// 修改为
iterate(dictionary, str, partOfSpeechDict);

3. 关于多个Guava Multimap的问题

完全允许创建多个Guava Multimap,没有任何限制。不过从你的业务场景来看,可以优化实现:

  • 可以自定义一个包含词性和释义的类,用单个ListMultimap<String, 自定义类>存储,避免维护两个独立的Multimap,降低数据不一致的风险
  • 或者直接将Entry枚举作为Multimap的值,这样能直接获取词性和释义,代码会更简洁

内容的提问来源于stack exchange,提问作者Lion98

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 16:50:30