Skip to content
This repository was archived by the owner on Dec 28, 2024. It is now read-only.

Commit 49046ee

Browse files
authored
Merge pull request #599 from EgorBurov246/features/lab28
Лабораторная работа №28
2 parents a407b0b + e074d4e commit 49046ee

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<artifactId>23K0815</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0815-p28</artifactId>
12+
<description>Массивы</description>
13+
</project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package ru.mirea.practice.s0000001.task1;
2+
3+
import java.util.HashSet;
4+
import java.util.TreeSet;
5+
6+
public abstract class Main {
7+
public static void main(String[] args) {
8+
// Шаг 1: Создание HashSet и добавление элементов
9+
HashSet<String> hashSet = new HashSet<>();
10+
hashSet.add("Banana");
11+
hashSet.add("Apple");
12+
hashSet.add("Orange");
13+
14+
// Шаг 2: Преобразование HashSet в TreeSet
15+
TreeSet<String> treeSet = new TreeSet<>(hashSet);
16+
17+
// Шаг 3: Вывод элементов TreeSet
18+
System.out.println("Элементы TreeSet: " + treeSet);
19+
}
20+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package ru.mirea.practice.s0000001.task2;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public abstract class NameMap {
7+
8+
public static void main(String[] args) {
9+
Map<String, String> nameMap = createMap();
10+
11+
int sameFirstNameCount = getSameFirstNameCount(nameMap);
12+
int sameLastNameCount = getSameLastNameCount(nameMap);
13+
14+
System.out.println("Количество людей с одинаковыми именами: " + sameFirstNameCount);
15+
System.out.println("Количество людей с одинаковыми фамилиями: " + sameLastNameCount);
16+
}
17+
18+
public static Map<String, String> createMap() {
19+
Map<String, String> nameMap = new HashMap<>();
20+
nameMap.put("Иванов", "Иван");
21+
nameMap.put("Петров", "Петр");
22+
nameMap.put("Сидоров", "Сидор");
23+
nameMap.put("Кузнецов", "Иван");
24+
nameMap.put("Смирнов", "Алексей");
25+
nameMap.put("Васильев", "Петр");
26+
nameMap.put("Попов", "Сидор");
27+
nameMap.put("Зайцев", "Иван");
28+
nameMap.put("Федоров", "Алексей");
29+
nameMap.put("Морозов", "Петр");
30+
return nameMap;
31+
}
32+
33+
public static int getSameFirstNameCount(Map<String, String> nameMap) {
34+
Map<String, Integer> firstNameCount = new HashMap<>();
35+
36+
for (String firstName : nameMap.values()) {
37+
firstNameCount.put(firstName, firstNameCount.getOrDefault(firstName, 0) + 1);
38+
}
39+
40+
int count = 0;
41+
for (int occurrences : firstNameCount.values()) {
42+
if (occurrences > 1) {
43+
count += occurrences;
44+
}
45+
}
46+
return count;
47+
}
48+
49+
public static int getSameLastNameCount(Map<String, String> nameMap) {
50+
Map<String, Integer> lastNameCount = new HashMap<>();
51+
52+
for (String lastName : nameMap.keySet()) {
53+
lastNameCount.put(lastName, lastNameCount.getOrDefault(lastName, 0) + 1);
54+
}
55+
56+
int count = 0;
57+
for (int occurrences : lastNameCount.values()) {
58+
if (occurrences > 1) {
59+
count += occurrences;
60+
}
61+
}
62+
return count;
63+
}
64+
}

students/23K0815/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,6 @@
3636
<module>23K0815-p25</module>
3737
<module>23K0815-p26</module>
3838
<module>23K0815-p27</module>
39+
<module>23K0815-p28</module>
3940
</modules>
4041
</project>

0 commit comments

Comments
 (0)