【问题标题】:Swap value and key from hasmap [closed]从哈希映射交换值和键[关闭]
【发布时间】:2018-04-14 21:55:50
【问题描述】:

我如何从 hashmap 交换值和键,用户从键盘给出 stringbuilder 并用 java 中的新值替换 stringbuilder 中的一个单词?我想制作一个反向翻译俚语互联网词典。例如,如果我给了这个字符串“笑和大声“在tsanlation之后我想要字符串“loI”。我创建了一个hasmap,它是我的字典并包含这些值{fyi”,“供您参考”);(“dae”,“其他人”);( "thx","thank you"); ("omg","Oh my god"); ("lol","laughing out lound");. 另外如果用户输入字符串“laugh out and lound” oytput“大声笑”。

我的代码是: 包javaapplication6;

      import java.util.*; 
       import java.util.ArrayList; 
        import java.util.HashMap;
      import java.util.Scanner;

公共类 JavaApplication6 {

/**
 * @param args the command line arguments
 */

public static void main(String[] args) {



                       String threeWords;

                        //create stringbuilder
                        StringBuilder acronym = new StringBuilder();

                        Scanner scan = new Scanner(System.in);

                       //o xrhsths dinei eisodo thn protash poy thelei na metafrasei
                        System.out.println("Eisigage thn protasi sou pou theleis na metafraseis:\n ");
                        threeWords = scan.nextLine();
                        threeWords = threeWords.toLowerCase(); //Changing it to lower case
                          acronym.append(threeWords);
                           System.out.println(" To string pou eisigages einai: " + acronym +"\n");
                        //dhmiougroum ena pinaka pou periexei mia mia thn lejei jexorista apo thn protash pou 
                        //edose o xrhsths oste na mporoume na broume pio eukola thn leji pou einai gia metafrash sto
                        //lejiko
                        String[] threeWordsArray = threeWords.split(" ");

                        //dhmiougria lejikou me thn slang tou internet me thn xrisi hashmap
                        HashMap<String,String> dictionary = new HashMap <String,String>(10);

               //Prosthiki lejeon  sto lejiko mas

                        dictionary.put("fyi", "for your information");
                        dictionary.put("dae", "Does anyone else");
                        dictionary.put("thx","thank you");
                        dictionary.put("omg","Oh my god");
                        dictionary.put("lol","laughing out lound");



                        HashMap<String, String> reversedHashMap = new HashMap<String, String>();
                               for (String key : dictionary.keySet()){
                                 reversedHashMap.put(dictionary.get(key), key);
                                 } 


                            System.out.println("reversedHasmap"+reversedHashMap);
                            for(String word : threeWordsArray) {


                           if (reversedHashMap.containsKey(word)) {

                            String definition = reversedHashMap.get(word);
            System.out.println("h metafrash einai: \n" + definition);

                            int index=acronym.indexOf(word);
                            System.out.println("index:"+index);
                            acronym.insert(index,definition);
                         }
            else {
            System.err.println("Word not found");

                            }//end if

                        }//end for  

                       System.out.println("To string metafrasmeno einai: " + acronym);





}

}

但我对 for 有疑问 for(String word : threeWordsArray) {

                           if (reversedHashMap.containsKey(word)) {

                            String definition = reversedHashMap.get(word);
            System.out.println("h metafrash einai: \n" + definition);

                            int index=acronym.indexOf(word);
                            System.out.println("index:"+index);
                            acronym.insert(index,definition);
                         }
            else {
            System.err.println("Word not found");

                            }//end if

                        }//end for  

找不到与 reversedHashMap 键匹配的输入。

【问题讨论】:

  • 你到底想要什么,我不明白?到目前为止你做了什么?
  • 什么?显示一些输入 + 输出以及到目前为止您尝试过的内容。
  • 将值放在和之前,然后将其返回键,但直到 StringBuilder 成为键盘之后。最简单!
  • 什么是逆向字典?展示您的代码以及您想要实现的目标以及您尝试过的内容,而不是尝试解释它。

标签: java dictionary hashmap stringbuilder


【解决方案1】:

您可以遍历任何包含对象(类型)的地图(mapToConvert)。这些类型可以是字符串、整数等。

通过遍历条目集中的每个条目,您可以同时提取键 entry.getKey() 以及它与 entry.getValue() 配对的值。然后,您可以创建新地图,使用值作为键,键作为值。

HashMap<Type,Type> newMap = new HashMap<>();
for(Map<Type,Type> entry : mapToConvert.entrySet()){
  Type key = entry.getKey();
  Type values = entry.getValue();
   newMap.put(values, key);
}

编辑:为清楚起见。

【讨论】:

  • 根据@TobySpeight 的建议,我添加了一些解释。如果有什么我可以进一步解释的,请询问,我会尽快回复。
  • 是的,我已经为我的程序写了这个,但是我有问题,检查输入是否与 reservedhasmap 键相同,并且直到输入键盘与新键相同时才找到hasmap.If 你想要我编辑我的帖子,你可以在我写的地方看到我的代码。
  • 我认为问题在于您正在使用 containsKey 来寻找完全匹配的内容。例如,“大声笑”现在是您的关键,而您正在拆分短语,因此您正在寻找关键“笑”。尝试遍历键集,并寻找包含您的单词的键。
  • 我可以为此做些什么?
【解决方案2】:

假设HashMap中没有重复值

HashMap<Object, Object> given = new HashMap<>();
HashMap<Object, Object> result = new HashMap<>();
for (String i : ex.keySet()){
    result.put(given.get(i), i);
}

result 将是一个 HashMap,其键和值从 given 交换。

【讨论】:

    猜你喜欢
    • 2013-12-11
    • 2015-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-25
    • 1970-01-01
    • 1970-01-01
    • 2011-04-04
    相关资源
    最近更新 更多