Skip to content

Instantly share code, notes, and snippets.

@ProZhar
Created December 6, 2015 11:10
Show Gist options
  • Select an option

  • Save ProZhar/e60a2109bb6d1fccbd74 to your computer and use it in GitHub Desktop.

Select an option

Save ProZhar/e60a2109bb6d1fccbd74 to your computer and use it in GitHub Desktop.
com.javarush.test.level09.lesson11.home09
package com.javarush.test.level09.lesson11.home09;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/* Десять котов
Создать класс кот – Cat, с полем «имя» (String).
Создать словарь Map(<String, Cat>) и добавить туда 10 котов в виде «Имя»-«Кот».
Получить из Map множество(Set) всех имен и вывести его на экран.
*/
public class Solution
{
public static void main(String[] args)
{
Map<String, Cat> map = createMap();
Set<Cat> set = convertMapToSet(map);
printCatSet(set);
}
public static Map<String, Cat> createMap()
{
//напишите тут ваш код
Map<String, Cat> cats = new HashMap<>(10);
for (int i = 0; i < 10; i++)
{
cats.put("Vaska"+i, new Cat("Vaska" + i));
}
return cats;
}
public static Set<Cat> convertMapToSet(Map<String, Cat> map)
{
//напишите тут ваш код
Set<Cat> cats = new HashSet<>();
for (Map.Entry<String, Cat> entry : map.entrySet())
{
cats.add(entry.getValue());
}
return cats;
}
public static void printCatSet(Set<Cat> set)
{
for (Cat cat:set)
{
System.out.println(cat);
}
}
public static class Cat
{
private String name;
public Cat(String name)
{
this.name = name;
}
public String toString()
{
return "Cat "+this.name;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment