/
GermanezZ
/
JavaExercises
Обзор
Документация
Войти
/
GermanezZ
/
JavaExercises
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
AllMetanitLessons/Collections/equalsToStringHashCode/Prog.java
78 строк
2 KB
germa
Уроки по Metanit
04 дек 2024, 20:39
04 дек 2024, 20:39
48d575a
Код
Авторство
О чём код?
package AllMetanitLessons.Collections.equalsToStringHashCode; import java.util.*; public class Prog{ public static void main(String[] args) { Map<Integer,String> states = new HashMap<>(); states.put(1,"Germany"); states.put(2,"Spain"); states.put(3,"France"); states.put(4,"Italy"); System.out.println(states.get(2)); Set<Integer> keys = states.keySet(); // получим весь набор ключей Collection<String> values = states.values(); // получить набор всех значений System.out.println(keys); System.out.println("----------------------"); System.out.println(values); System.out.println("-----------------------"); states.replace(1,"Poland"); //заменить элемент states.remove(1); for (Map.Entry<Integer,String> item : states.entrySet()){ System.out.printf("Key: %d\tValue: %s\n",item.getKey(),item.getValue()); } System.out.println("-----------------------------------------------------"); Map<String, Person> people = new HashMap<>(); people.put("001A",new Person("Tom")); people.put("001B",new Person("Bill")); people.put("002A",new Person("Nick")); for (Map.Entry<String,Person> item : people.entrySet()){ System.out.printf("Id: %s \t Name: %s\n", item.getKey(),item.getValue().getName()); } } } class Person { private final String name; public Person(String name) { this.name = name; } public String getName() { return name; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Person person = (Person) o; return Objects.equals(name, person.name); } @Override public int hashCode() { return Objects.hash(name); } }