-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain1.java
More file actions
37 lines (29 loc) · 1.17 KB
/
Copy pathMain1.java
File metadata and controls
37 lines (29 loc) · 1.17 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
package Collections.Map;
import java.util.*;
public class Main1 {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(1, "string 1");
map.put(2, "string 2");
map.put(3, "string 3");
String string1 = map.get(1);
String string2 = map.get(2);
String string3 = map.get(3);
for(Map.Entry<Integer, String> entry: map.entrySet()) {
Integer key = entry.getKey();
String value = entry.getValue();
}
// Как отсортировать значения мапы
List<Map.Entry<Integer, String>> valuesList = new ArrayList(map.entrySet());
List<Map.Entry<Integer, String>> list = null;
Collections.sort(list, new Comparator<Map.Entry<Integer, String>>() {
@Override
public int compare(Map.Entry<Integer, String> o1, Map.Entry<Integer, String> o2) {
return o1.getValue().compareTo(o2.getValue());
}
});
for (Map.Entry<Integer, String> entry : map.entrySet()) {
System.out.println(entry.getKey() + " - " + entry.getValue());
}
}
}