-5

How i can swap value and key from hashmap and user give stringbuilder from keyboard and replace one word from stringbuilder with new value in java?Ι want to make a reverse trasnlation slang internet's dictionary.For example if I had given this string "laugh and lound " after tsanlation I want string "loI".I had create a hasmap which is my dictionary and contains this values{fyi", "for your information"); ("dae", "Does anyone else"); ("thx","thank you"); ("omg","Oh my god"); ("lol","laughing out lound");.Also if user givving input the string "laugh out and lound" take to oytput "lol".

My code is: package javaapplication6;

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

public class 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);





}

}

but I have problem with 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  

It can not find the input which are match with reversedHashMap key.

2 Answers2

1

You can loop through any map (mapToConvert) which holds Objects (Types). These types can be Strings, Integers etc.

By looping through each entry in the entry set, you can simultaneously pull out both the key entry.getKey() and the value that it's paired with entry.getValue(). You can then create your new map used the value as the key and the key as the value.

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);
}

Edit: for clarity.

Sam
  • 1,234
  • 3
  • 17
  • 32
  • I've added a bit of explanation, on @TobySpeight 's recommendation. If there's anything I can explain further, please ask and I'll respond when I can. – Sam Nov 02 '17 at 15:51
  • Yes i have write this for to my programm but i have problem with for which check if input is same with reservesdhasmap key and it don't find sth until the input keyboard is same with new key with revesed hasmap.If you want i have edit my post and you can see my code where I have write. – waterfall100 Nov 02 '17 at 23:55
  • I think the issue is that you're using `containsKey` which looks for the exact match. for example 'laugh out loud' is now your key, and you're splitting the phrase, so you're looking for key 'laugh'. Try looping through the key set, and looking for a key which contains one of your words. – Sam Nov 03 '17 at 09:25
  • And what can i do for this which I want? – waterfall100 Nov 03 '17 at 11:44
0

Presuming that there are no duplicate values in the 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 will be a HashMap with keys and values swapped from given.

Jonathan Coustick
  • 1,127
  • 9
  • 19