Добавлено пятое задание ЛР2 - HashMap

This commit is contained in:
Victor 2013-10-04 18:38:15 +03:00
parent 2233d948a0
commit 8162f435b9
2 changed files with 32 additions and 2 deletions

View File

@ -14,7 +14,7 @@ public class LR_2 implements ILabRab {
"Двумерный массив строк",
"Эллипсы и круги. Массив",
"Эллипсы и круги 2. Сортировка списка",
"5"
"Эллипсы и круги 3. HashMap"
};
@Override
@ -33,7 +33,7 @@ public class LR_2 implements ILabRab {
LR_2_Tasks.getInstance().task4();
break;
case 4:
LR_2_Tasks.getInstance().task5();
break;
}
}

View File

@ -4,7 +4,9 @@ import java.awt.Point;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*
@ -62,6 +64,24 @@ public class LR_2_Tasks {
printList(copy);
}
public void task5() {
makeObjects();
HashMap<Ellipse, String> map = new HashMap<>();
map.put(ellipse1, "Эллипс Первый");
map.put(ellipse2, "Эллипс Иоанн Второй");
map.put(ellipse3, "Эллипс Третий");
map.put(circle1, "Круг один");
map.put(circle2, "Круг 2. Атака клонов");
printMap(map);
System.out.println("\nВыводим ellipse1");
printMapEntry(ellipse1, map.get(ellipse1));
System.out.println("\обавляем другой ellipse1 и выводим");
map.put(ellipse1, "Снова Эллипс Первый");
printMapEntry(ellipse1, map.get(ellipse1));
}
private void makeObjects() {
ellipse1 = new Ellipse(
new Point(2, 2), new Point(8, 2),
@ -86,6 +106,16 @@ public class LR_2_Tasks {
}
}
private void printMap(Map<Ellipse, String> map) {
for (Map.Entry<Ellipse, String> entry : map.entrySet()) {
printMapEntry(entry.getKey(), entry.getValue());
}
}
private void printMapEntry(Ellipse key, String value) {
System.out.println( String.format("%f\t- %s", key.getSquare(), value) );
}
private void userSort(List<Ellipse> list, Comparator<Ellipse> comparator) {
final int size = list.size();
for (int i = 0; i < size - 1; i++) {