/
GermanezZ
/
JavaExercises
Обзор
Документация
Войти
/
GermanezZ
/
JavaExercises
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
AllMetanitLessons/Collections/Map/Hash.java
103 строки
3 KB
germa
Уроки по Metanit
04 дек 2024, 20:39
04 дек 2024, 20:39
48d575a
Код
Авторство
О чём код?
package AllMetanitLessons.Collections.Map; import org.jetbrains.annotations.NotNull; import java.util.*; public class Hash { public static void main(String[] args) { HashMap<Integer, String> states = new HashMap<>(); states.put(1,"Russia"); states.put(2,"Germany"); states.put(3,"Rome"); String first = states.get(1); // поскольку значение STRING мы делаем переменную стринг System.out.printf("First state is: %s\n",first); Set<Integer> set = states.keySet(); System.out.println("Keys: " + set); Collection<String> strings = states.values(); System.out.println("Values: " + strings); states.replace(2,"USA"); states.remove(3); for (Map.Entry<Integer, String> item : states.entrySet()){ System.out.printf("Key: %d, Value: %s \n", item.getKey(), item.getValue()); } HashMap<Integer, Personn> people = new HashMap<>(); people.put(1,new Personn("Tom")); people.put(2,new Personn("Bob")); people.put(3,new Personn("Tim")); people.put(4,new Personn("Bill")); people.put(5,new Personn("Milly")); people.replace(1,new Personn("Tommy")); for (Map.Entry<Integer, Personn> item : people.entrySet()){ System.out.printf("ID: %d, Name: %s, Frase: %s\n", item.getKey(), item.getValue().getName(), item.getValue().Frase()); } HashMap<Personn,String> characterPeople = new HashMap<>(); characterPeople.put(new Personn("Bob"),"Аккуратный"); characterPeople.put(new Personn("Tom"),"Опрятный"); characterPeople.put(new Personn("Ruby"),"Усидчивый, вежливый"); characterPeople.put(new Personn("Milly"),"Стрессоустойчивый"); characterPeople.put(new Personn("Den"),"Экономный"); for (Map.Entry<Personn, String> map : characterPeople.entrySet()){ System.out.printf("Name: %s, Character: %s \n", map.getKey().getName(),map.getValue()); } characterPeople.replace(new Personn("Ruby"),"Нет особых качеств"); characterPeople.remove(new Personn("Ruby")); Set<Personn> peoples = characterPeople.keySet(); for (Personn s : peoples){ System.out.printf("Name: %s \n", s.getName()); } Collection<String> stringss = characterPeople.values(); System.out.print("Качества: "); for (String s : stringss){ System.out.print(s + ", "); } } } class Personn implements Comparable<Personn>{ private final String name; public Personn(String name) { this.name = name; } public String getName() { return name; } public String Frase(){ return "I'm " + name + " and it's my legacy"; } @Override public int compareTo(@NotNull Personn o) { return name.compareTo(o.getName()); } }