/
GermanezZ
/
JavaExercises
Обзор
Документация
Войти
/
GermanezZ
/
JavaExercises
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
AllMetanitLessons/Collections/Map/TreeMaps.java
137 строк
5 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 TreeMaps { public static void main(String[] args) { /*TreeMap<Integer, String> states = new TreeMap<>(); states.put(10,"Germany"); states.put(2,"Spain"); states.put(14,"France"); states.put(3,"Italy"); System.out.println(states.get(2)); for (Map.Entry<Integer,String> item : states.entrySet()){ System.out.printf("Id: %d,\tState: %s\n",item.getKey(),item.getValue()); } System.out.println("---------------------------------------"); Set<Integer> keys = states.keySet(); System.out.println(keys); System.out.println("---------------------------------------"); Collection<String> values = states.values(); System.out.println(values); System.out.println("---------------------------------------"); Map<Integer,String> afterMap = states.tailMap(4,false); System.out.println(afterMap); System.out.println("---------------------------------------"); Map<Integer,String> beforeMap = states.headMap(10,true); System.out.println(beforeMap); System.out.println("---------------------------------------"); Map.Entry<Integer,String> lastItem = states.lastEntry(); System.out.printf("Last item has key %d value %s \n",lastItem.getKey(), lastItem.getValue()); Map<String, Person> people = new TreeMap<String, Person>(); people.put("1240i54", new Person("Tom")); people.put("1564i55", new Person("Bill")); people.put("4540i56", new Person("Nick")); for(Map.Entry<String, Person> item : people.entrySet()){ System.out.printf("Key: %s Value: %s \n", item.getKey(), item.getValue().getName()); }*/ TreeMap<Integer, Person> map = new TreeMap<>(); map.put(1, new Person("German")); map.put(2, new Person("Tom")); map.put(3, new Person("Artjom")); map.put(4, new Person("Den")); map.put(5, new Person("Dima")); map.replace(4,new Person("Bob")); Set<Integer> set = map.keySet(); System.out.println(set); Collection<Person> people = map.values(); for (Person person : people){ System.out.printf("Name: %s\n",person.getName()); } for (Map.Entry<Integer, Person> item : map.entrySet()){ System.out.printf("ID: %d Name: %s\n", item.getKey(),item.getValue().getName()); } System.out.println("--------------------------"); Map.Entry<Integer,Person> first = map.firstEntry(); System.out.printf("First: Id: %d Name: %s\n",first.getKey(),first.getValue().getName()); Map.Entry<Integer,Person> last = map.lastEntry(); System.out.printf("Last: Id: %d Name: %s\n",last.getKey(),last.getValue().getName()); Integer firstkey = map.firstKey(); System.out.printf("First Id: %d \n",firstkey); Integer lastkey = map.lastKey(); System.out.printf("Last: Id: %d \n",lastkey); System.out.println("--------------------------"); Map.Entry<Integer,Person> higher = map.higherEntry(2); System.out.printf("Higher than Bob: Id: %d Name: %s\n",higher.getKey(),higher.getValue().getName()); Map.Entry<Integer,Person> lower = map.lowerEntry(2); System.out.printf("Lower than Bob: Id: %d Name: %s\n",lower.getKey(),lower.getValue().getName()); Map.Entry<Integer,Person> celling = map.ceilingEntry(2); Map.Entry<Integer,Person> floor = map.floorEntry(2); System.out.printf("Celling than Bob: Id: %d Name: %s\n",celling.getKey(),celling.getValue().getName()); System.out.printf("Floor than Bob: Id: %d Name: %s\n",floor.getKey(),floor.getValue().getName()); System.out.println("--------------------------"); Map<Integer,Person> tail = map.tailMap(3,true); System.out.println("TailSet: "); for (Map.Entry<Integer,Person> item : tail.entrySet()){ System.out.printf("ID: %d Name: %s\n", item.getKey(),item.getValue().getName()); } Map<Integer,Person> sub = map.subMap(1,true,3,true); System.out.println("SubSet: "); for (Map.Entry<Integer,Person> item : sub.entrySet()){ System.out.printf("ID: %d Name: %s\n", item.getKey(),item.getValue().getName()); } Map<Integer,Person> head = map.headMap(4,true); System.out.println("HeadSet: "); for (Map.Entry<Integer,Person> item : head.entrySet()){ System.out.printf("ID: %d Name: %s\n", item.getKey(),item.getValue().getName()); } Integer higherKey = map.higherKey(2); } } class Person { private final String name; public Person(String name) { this.name = name; } public String getName() { return name; } }