Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_4/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package ru.gb.Dmitrieva.HomeWorkApp.Lesson_4;

import java.util.*;

public class Main {
public static void main(String[] args) {

System.out.println("\n###Задание 1###");

String[] words = new String[]{
"команда", "банк", "сцена", "население", "свобода",
"музыка", "правда", "свобода", "память", "команда",
"память", "свобода", "договор", "дерево", "банк",
"свобода", "музыка", "большинство", "сцена", "музыка"
};
List<String> wordsList = Arrays.asList(words);
System.out.println("Все слова: " + wordsList);

Set<String> wordsSet = new HashSet<>(wordsList);
System.out.println("Уникальные слова: " + wordsSet);

for (String word : wordsSet) {
int number = Collections.frequency(wordsList, word);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

хитро!

System.out.println(word + " : " + number);
}

System.out.println("\n###Задание 2###");

Phonebook phonebook = new Phonebook();
System.out.println("-Добавляем Иванова.");
phonebook.add("Иванов", "+79261425312");
System.out.println("1) Иванов - " + phonebook.get("Иванов"));
System.out.println("1) Сидоров - " + phonebook.get("Сидоров"));

System.out.println("\n-Добавляем Сидорова.");
phonebook.add("Сидоров", "+79157437375");
System.out.println("2) Иванов - " + phonebook.get("Иванов"));
System.out.println("2) Сидоров - " + phonebook.get("Сидоров"));

System.out.println("\n-Добавляем второго Иванова.");
phonebook.add("Иванов", "+79062431890");
System.out.println("3) Иванов - " + phonebook.get("Иванов"));
System.out.println("3) Сидоров - " + phonebook.get("Сидоров"));
}
}
35 changes: 35 additions & 0 deletions src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_4/Phonebook.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package ru.gb.Dmitrieva.HomeWorkApp.Lesson_4;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;

public class Phonebook {
private HashMap<String, HashSet<String>> phonebook;

public Phonebook() {
this.phonebook = new HashMap<>();
}

public HashSet<String> get(String surname) {
if (phonebook.containsKey(surname))
return phonebook.get(surname);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{ }

return new HashSet<>(Collections.singletonList(""));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тут просто HashSet()

}

public boolean add(String surname, String phone) {
if (phonebook.containsKey(surname)) {
HashSet<String> numberSet = phonebook.get(surname);
if (!numberSet.contains(phone)) {
numberSet.add(phone);
} else {
return false;
}
} else {
HashSet<String> numberSet = new HashSet<>(1);
numberSet.add(phone);
phonebook.put(surname, numberSet);
}
return true;
}
}