Last active
August 16, 2020 09:54
-
-
Save kryvoboker/d00460169141882d4d67aee8c7100bb7 to your computer and use it in GitHub Desktop.
HomeWork8(Хэш таблицы Map)
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; | |
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); | |
} | |
public 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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment