Created
August 16, 2020 10:45
-
-
Save kryvoboker/953cd22bcfed8de093ee38a6e817c300 to your computer and use it in GitHub Desktop.
HomeWork8(Хэш таблицы Map)(2)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.FileInputStream; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.ObjectInputStream; | |
import java.io.ObjectOutputStream; | |
import java.io.Serializable; | |
import java.util.ArrayList; | |
import java.util.LinkedHashMap; | |
import java.util.Map; | |
import java.util.Scanner; | |
public class Main implements Serializable { | |
private static final long serialVersionUID = 1L; | |
public static void main(String[] args) { | |
// ArrayList<String> listForTranslate = new ArrayList<>(); | |
// Map<String, String> text = new LinkedHashMap<>(); | |
// text.put("Вони", "They"); | |
// text.put("йшли", "walked"); | |
// text.put("по", "along"); | |
// text.put("берегу моря", "the seashore"); | |
// text.put("тримаючись", "holding"); | |
// text.put("за руки", "hands"); | |
// for (Map.Entry<String, String> m : text.entrySet()) { //добавляю значения и ключи в список | |
// listForTranslate.add(m.getValue()); | |
// } | |
// | |
// try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("English.in"))) { | |
// oos.writeObject(listForTranslate); | |
// } catch (IOException e) { | |
// System.out.println("ERROR save text!!!"); | |
// } | |
// | |
// ArrayList<String> listReadForTranslate = null; | |
// | |
// try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("English.in"))) { | |
// listReadForTranslate = (ArrayList<String>) ois.readObject(); | |
// } catch (IOException | ClassNotFoundException e) { | |
// System.out.println("ERROR load text!!!"); | |
// } | |
// System.out.println(listReadForTranslate); | |
//// listReadForTranslate.remove(0); //проверяю переводчик на работоспособность | |
//// listReadForTranslate.remove(2); | |
//// System.out.println("------------"); | |
//// System.out.println(listReadForTranslate); | |
// System.out.println("------------"); | |
// | |
// ArrayList<String> listAfterTranslate = translateText(listReadForTranslate, text); //перевожу текст | |
//// System.out.println(listAfterTranslate); | |
//// System.out.println("---------"); | |
// | |
// try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("Ukrainian.out"))) { | |
// oos.writeObject(listAfterTranslate); | |
// } catch (IOException e) { | |
// System.out.println("ERROR save text!!!"); | |
// } | |
// | |
// ArrayList<String> translatedText = null; | |
// | |
// try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("Ukrainian.out"))) { | |
// translatedText = (ArrayList<String>) ois.readObject(); | |
// } catch (IOException | ClassNotFoundException e) { | |
// System.out.println("ERROR load text!!!"); | |
// } | |
// System.out.println(translatedText); | |
// System.out.println("---------"); | |
// Map<String, String> textTwo = enterKeyAndValue(text); //добавляю новые ключи и значения | |
// System.out.println(textTwo); | |
// System.out.println("---------"); | |
// | |
// try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("Ukrainian_English.in"))) { | |
// oos.writeObject(textTwo); | |
// } catch (IOException e) { | |
// System.out.println("ERROR save text!!!"); | |
// } | |
Map<String, String> textThree = null; | |
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("Ukrainian_English.in"))) { | |
textThree = (Map<String, String>) ois.readObject(); | |
} catch (IOException | ClassNotFoundException e) { | |
System.out.println("ERROR load text!!!"); | |
} | |
System.out.println(textThree); | |
ArrayList<String> newListForTranslate = new ArrayList<>(); | |
for (Map.Entry<String, String> m : textThree.entrySet()) { //добавляю новые значения и ключи в новый список | |
newListForTranslate.add(m.getValue()); | |
} | |
// newListForTranslate.remove(6); //снова проверяю переводчик на работоспособность | |
// System.out.println(newListForTranslate); | |
System.out.println("---------"); | |
ArrayList<String> newListAfterTranslate = translateText(newListForTranslate, textThree); //перевожу текст | |
System.out.println(newListAfterTranslate); | |
} | |
private static ArrayList<String> translateText(ArrayList<String> listForTranslate, Map<String, String> mp) { | |
ArrayList<String> listAfterTranslate = new ArrayList<>(); | |
for (int i = 0; i < listForTranslate.size(); i += 1) { | |
for (Map.Entry<String, String> m : mp.entrySet()) { | |
if (listForTranslate.get(i).equals(m.getValue())) { | |
listAfterTranslate.add(m.getKey()); | |
} | |
} | |
} | |
return listAfterTranslate; | |
} | |
private static Map<String, String> enterKeyAndValue(Map<String, String> mp) { | |
Scanner sc = new Scanner(System.in); | |
System.out.println("Please enter KEY in Ukrainian for translation into English"); | |
String key = sc.nextLine(); | |
System.out.println("Please enter VALUE in English for translation into Ukrainian"); | |
String value = sc.nextLine(); | |
mp.put(key, value); | |
return mp; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment