-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSem5HomeWork2.java
More file actions
56 lines (52 loc) · 2.53 KB
/
Copy pathSem5HomeWork2.java
File metadata and controls
56 lines (52 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class Sem5HomeWork2 {
// Написать программу, которая найдёт и выведет повторяющиеся имена с количеством повторений. Отсортировать по убыванию популярности.
public static ArrayList<String> getName(String [] list){
ArrayList<String> listName = new ArrayList<>();
for (String el : list) {
String [] elList = el.split(" ");
listName.add(elList[0]);
}
return listName;
}
public static Map<String, Integer> getMap(ArrayList<String> name) {
Map<String, Integer> mapName = new HashMap<>();
for (int i = 0; i < name.size(); i++) {
int count = 1;
for (int j = i + 1; j < name.size(); j++){
if (name.get(i).equals(name.get(j))) count += 1;
}
if (mapName.containsKey(name.get(i)) == false) mapName.put(name.get(i), count);
}
return mapName;
}
public static void nameRepeat(Map<String, Integer> map){
for(var item: map.entrySet()){
if (item.getValue() > 1) System.out.printf("%s: %d \n", item.getKey(), item.getValue());
}
}
public static void sortName(Map<String, Integer> map){
Map<Integer, ArrayList<String>> sortMap = new HashMap<>();
ArrayList<Integer> listCount = new ArrayList<>();
for(var item: map.entrySet()) {
if (listCount.contains(item.getValue()) == false) listCount.add(item.getValue());
}
listCount.sort(null);
for (int i = listCount.size()-1; i > -1; i--){
for (var item: map.entrySet()){
if (listCount.get(i) == item.getValue()) System.out.printf("%s : %d \n", item.getKey(), item.getValue());
}
}
}
public static void main(String[] args) {
String[] emploees = new String[] {"Иван Иванов", "Иван Петров", "Сергей Козлов", "Евгений Петров", "Сергей Васильев", "Иван Смирнов", "Андрей Петров"};
ArrayList<String> emploeesName = getName(emploees);
Map<String, Integer> mapName = getMap(emploeesName);
System.out.println("Повторяющиеся имена: ");
nameRepeat(mapName);
System.out.println("Имена, отсортированные по убыванию популярности: ");
sortName(mapName);
}
}