Created
May 18, 2017 08:07
-
-
Save fermopili/84e9e2817ce4bb2ae85466607372b44e to your computer and use it in GitHub Desktop.
com.javarush.task.task35.task3505 List to Map
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.task.task35.task3505; | |
public interface Convertable<Key> { | |
Key getKey(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.task.task35.task3505; | |
public class ConvertableBook implements Convertable<String> { | |
private String name; | |
public ConvertableBook(String name) { | |
this.name = name; | |
} | |
@Override | |
public String getKey() { | |
return name; | |
} | |
@Override | |
public String toString() { | |
return "ConvertableBook{" + | |
"name='" + name + '\'' + | |
'}'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.task.task35.task3505; | |
public class ConvertableUser implements Convertable<Integer> | |
{ | |
private Integer id; | |
private String name; | |
public ConvertableUser(Integer id, String name) | |
{ | |
this.id = id; | |
this.name = name; | |
} | |
@Override | |
public Integer getKey() | |
{ | |
return id; | |
} | |
@Override | |
public String toString() | |
{ | |
return "ConvertableUser{" + | |
"id=" + id + | |
", name='" + name + '\'' + | |
'}'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.task.task35.task3505; | |
import java.awt.*; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
public class ConvertableUtil | |
{ | |
public static <T extends Convertable> Map convert(List<T> list) | |
{ | |
Map result = new HashMap<> ( ); | |
for (T item : list) | |
{ | |
result.put (item.getKey (), item); | |
} | |
return result; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.task.task35.task3505; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Map; | |
/* | |
List to Map | |
*/ | |
public class Solution { | |
public static void main(String[] args) { | |
List<ConvertableUser> users = new ArrayList<>(); | |
users.add(new ConvertableUser(234, "First User")); | |
users.add(new ConvertableUser(235, "Second User")); | |
users.add(new ConvertableUser(236, "Third User")); | |
Map<Integer, ConvertableUser> newMap = ConvertableUtil.convert(users); | |
System.out.println(newMap); | |
//{236=ConvertableUser{id=236, name='Third User'}, | |
// 235=ConvertableUser{id=235, name='Second User'}, | |
// 234=ConvertableUser{id=234, name='First User'}} | |
////////////////////////////////////////////////////////// | |
List<ConvertableBook> books = new ArrayList<>(); | |
books.add(new ConvertableBook("First Book")); | |
books.add(new ConvertableBook("Second Book")); | |
books.add(new ConvertableBook("Third Book")); | |
Map<String, ConvertableBook> bookMap = ConvertableUtil.convert(books); | |
System.out.println(bookMap); | |
//{Third Book=ConvertableBook{name='Third Book'}, | |
// First Book=ConvertableBook{name='First Book'}, | |
// Second Book=ConvertableBook{name='Second Book'}} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
taskKey="com.javarush.task.task35.task3505" | |
List to Map | |
Реализуй логику метода convert в классе ConvertableUtil, который должен возвращать словарь, | |
значениями которого являются элементы переданного cписка, | |
а ключами являются объекты, полученные вызовом интерфейсного метода getKey. | |
Элементы cписка должны наследоваться от Convertable, который в свою очередь параметризирован каким-то ключом. | |
Например, ConvertableBook параметризирован String, т.е. ключ в результирующем словаре должен иметь тип - String | |
ConvertableUser параметризирован Integer, т.е. ключ в результирующем словаре должен иметь тип - Integer | |
Значения в словаре должны совпадать с элементами Списка. | |
Смотрите метод main для примера. | |
Расставьте в методе ConvertableUtil.convert дженерик типы. | |
Требования: | |
1. В множестве значений словаря возвращенного методом convert класса ConvertableUtil должны содержаться все элементы конвертируемого списка. | |
2. Размер словаря возвращенного методом convert класса ConvertableUtil должен быть равен размеру конвертируемого списка. | |
3. В множестве ключей словаря возвращенного методом convert класса ConvertableUtil должны содержаться ключи конвертируемого списка, полученные с помощью вызова метода getKey на каждом объекте списка. | |
4. Конвертация должна проходить в соответствии с условием задачи, ключи получаем с помощью метода getKey, а значением служат сами объекты списка. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment